Jeremy Miller Jeremy Miller - 5 months ago 7
SQL Question

How to Sum a result quantity in sql server with multiple records which several joins, does it need some subquery to do it?

I am wanting to get the SUM records of a query that I am writing

I have done simple SUM aggregates in the past, but I'm a bit rusty and wonder the best approach to getting the proper results.

Example of table with Quantity (imaps_inv)

ste_id ste_acct_id stk_id qty
1 1 001 5
1 2 001 10
1 3 001 15


So the query should NOT return 3 records, but only 1 record and so the query results SHOULD be

Site ID Stocking ID Qty
1 001 30


So you can see what I want to return

inv.qty
is the Quantity field I wish to SUM , and the query I wrote up based on the tables is below.

SELECT ste.ste_id, ty.ste_ty_id, inv.qty
FROM dbo.imaps_inv inv WITH(NOLOCK)
INNER JOIN dbo.imaps_ste ste WITH(NOLOCK)
ON inv.ste_id = ste.ste_id
INNER JOIN dbo.imaps_ste_ty ty
ON ty.ste_ty_id = ste.ste_ty_id
INNER JOIN dbo.imaps_stk_acct acct WITH(NOLOCK)
ON acct.ste_id = ste.ste_id
INNER JOIN dbo.imaps_stk_acct_ty aty WITH(NOLOCK)
ON aty.stk_acct_ty_id = acct.stk_acct_ty_id
WHERE
ste.inact_ind = 0
AND LTRIM(RTRIM(aty.stk_acct_ty_nm)) = 'Good'
AND acct.rsv_ind = 0
AND acct.inact_ind = 0


PLEASE LET ME KNOW WHAT I CAN PROVIDE TO MAKE IT MORE HELPFUL ( screenshots, schema etc... )

UPDATE,

well I was only getting 19 records and this co-worker said "oh, no.. you should be getting 16,000 records...

I said ok, i'm using this ste_id (site id) and when I sent him this query

SELECT distinct ste_id from [dbo].[imaps_inv] ( 20 records)


He said, to add in stk_id (stock #)

SELECT distinct ste_id, stk_id from [dbo].[imaps_inv] (15,910 records)


Therefore I updated my query to which I finally have the proper results and figured I would update my question with the combination of the persons answer along with me tweaking the code as other person certainly does not have the schema and records etc..

Final working query

SELECT ste.ste_id as 'Site ID', inv.stk_id as 'Stocking ID', SUM(inv.qty) Qty
FROM dbo.imaps_inv inv WITH(NOLOCK)
INNER JOIN dbo.imaps_ste ste WITH(NOLOCK)
ON inv.ste_id = ste.ste_id
INNER JOIN dbo.imaps_ste_ty ty
ON ty.ste_ty_id = ste.ste_ty_id
INNER JOIN dbo.imaps_stk_acct acct WITH(NOLOCK)
ON acct.ste_id = ste.ste_id
INNER JOIN dbo.imaps_stk_acct_ty aty WITH(NOLOCK)
ON aty.stk_acct_ty_id = acct.stk_acct_ty_id
WHERE
ste.inact_ind = 0
AND LTRIM(RTRIM(aty.stk_acct_ty_nm)) = 'Good'
AND acct.rsv_ind = 0
AND acct.inact_ind = 0
GROUP BY ste.ste_id, inv.stk_id

Answer

Use SUM:

SELECT  ste.ste_id, 
        SUM(inv.qty) Qty
FROM dbo.imaps_inv inv WITH(NOLOCK)
INNER JOIN dbo.imaps_ste ste WITH(NOLOCK)
    ON inv.ste_id = ste.ste_id 
INNER JOIN dbo.imaps_ste_ty ty
    ON ty.ste_ty_id = ste.ste_ty_id
INNER JOIN dbo.imaps_stk_acct acct WITH(NOLOCK)
    ON acct.ste_id = ste.ste_id
INNER JOIN dbo.imaps_stk_acct_ty aty WITH(NOLOCK)
    ON aty.stk_acct_ty_id = acct.stk_acct_ty_id
WHERE 
    ste.inact_ind = 0
AND LTRIM(RTRIM(aty.stk_acct_ty_nm)) = 'Good'
AND acct.rsv_ind = 0
AND acct.inact_ind = 0
GROUP BY ste.ste_id;
Comments