tsqln00b
tsqln00b

Reputation: 355

T-SQL Group By causing data not to SUM

I have a query that I would like to total the purchases from all Vendors by Year. However, my results are not coming back summed by vendor like I want. I believe it is because I have to put IJDATE in the GROUP BY. Any suggestions how I can fix this?

SELECT
    YEAR(ij.IJDATE) AS YearPurchased,
    ISNULL(SUM(ij.IJCOST * ij.IJQTY), 0) AS YearlySpend,
    IJVEND AS Vendor
FROM
    dbo.S2K_IJ ij
WHERE
    ij.IJTYPE IN ('I', 'ID')
GROUP BY
    ij.IJDATE,
    ij.IJVEND
ORDER BY
    YearPurchased,
    Vendor

Upvotes: 0

Views: 28

Answers (1)

RBarryYoung
RBarryYoung

Reputation: 56755

Try changing your Group By to this:

GROUP BY
    YEAR(ij.IJDATE),
    ij.IJVEND

Upvotes: 1

Related Questions