Unique part count & sum

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Johne5
    New Member
    • Sep 2013
    • 1

    Unique part count & sum

    I am trying to find a way to report the unique items in a column, how many times it appears, and sum the Qty for each.

    I have
    Part_No,Qty
    123AA,5
    345BB,20
    678AB,40
    123AA,20

    ...and am looking to output like:

    123AA,2,25
    345BB,1,20
    678AB,1,40

    from another post I found that the following will give me unique and sum, but not count?!

    Code:
    SELECT C.Part_No, Sum(C.Qty) AS Total, T2.N
    FROM
        (SELECT T.Part_No, Count(T.Part_No) AS N 
         FROM 
            (SELECT DISTINCT Part_No, Count(*) AS N 
             FROM Form_Data GROUP BY Part_No) AS T 
        GROUP BY T.Part_No) AS T2 
    INNER JOIN Form_Data AS C
    ON T2.Part_No = C.Part_No
    GROUP BY C.Part_No, T2.N;

    Any assistance would be greatly appreciated.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I don't see why you have that subquery in there, you don't need it. And if you want a count, then use the count(*) function like you did in the subquery.

    Comment

    Working...