Question

I'm trying to pare down the number of queries being run by a report to get a specific result. I've gotten it part way there, but I can't quite get it to where I need it to be. The database is from an order picking/packing system at a warehouse. The 3 tables in questions are the packing data, the user data, and the picking data. What I need is the total # of lines packed, in all batches, by an individual for a day, but there are no line counts stored in the database anywhere. I can get the line count from a count of all records in the pick table for each batch, but that doesn't break it out by who packed it. The packing table contains basic info such as Batch_Num, Cart_Num, Badge_Num, and some dates and error tracking that don't apply to this problem. From this, I can get a list of batches for each user and I can pull the line count for each one from the pick table, but I need to get those summed for each user.

SELECT     t1.Badge_Num,
           t1.Batch_Num,
           t2.UserDescription,
           (SELECT COUNT(Batch_Num) AS lines
                FROM Pick WHERE (Batch_Num = t1.Batch_Num)) AS totallines
FROM       Pack AS t1 WITH (NOLOCK) 
                FULL OUTER JOIN JenX.dbo.Users AS t2 
                      ON t1.Badge_Num = t2.UserName  
WHERE      (t1.DateCreated BETWEEN '02/04/2014' AND '02/05/2014')
GROUP BY   t2.UserDescription, t1.Badge_Num, t1.Batch_Num
ORDER BY   t2.UserDescription, t1.Badge_Num

Since I can't post an image yet, I'll enter the results I get here.

Badge_Num        Batch_Num        UserDescription        TotalLines
99900001         93943            Joe User               40
99900001         93394            Joe User               58
99900001         93944            Joe User               58
99900001         93930            Joe User               110
99900001         94322            Joe User               77
99900001         94101            Joe User               86
99900001         94691            Joe User               68
99944444         93010            Tom Packer             15
99944444         93001            Tom Packer             99
99944444         92972            Tom Packer             30
99944444         93003            Tom Packer             52
99944444         92972            Tom Packer             15

This gives me a list of batches from each user(blacked out the names, but they are there) with a line count for each batch, but for the life of me, I can not get those lines summed. I don't even need the batch #s listed on this, just the user name and the total lines is all that is really needed. Any suggestions would be greatly appreciated.

Was it helpful?

Solution

If I understand correctly, you want to modify this query to remove badge_num from the aggregation. You cannot because of the correlated subquery. Try this:

SELECT     t1.Badge_Num,
           t2.UserDescription,
           COUNT(p.Batch_Num) AS totallines
FROM       Pack t1 WITH (NOLOCK) left outer join
           JenX.dbo.Users t2 
           ON t1.Badge_Num = t2.UserName left outer join
           Pick p
           on p.Batch_Num = t1.Batch_Num
WHERE      (t1.DateCreated BETWEEN '02/04/2014' AND '02/05/2014')
GROUP BY   t2.UserDescription, t1.Badge_Num
ORDER BY   t2.UserDescription, t1.Badge_Num;

The effect of the full outer join is undone by the where clause.

OTHER TIPS

First I would rewrite your query using CTE

WITH    lines
          AS ( SELECT Batch_num
                   ,COUNT(*) AS lines
                FROM Pick
                GROUP BY Batch_num)
    SELECT t1.Badge_Num
           ,t1.Batch_Num
           ,t2.UserDescription
           ,l.lines AS totallines
        FROM Pack AS t1 WITH ( NOLOCK )
        LEFT OUTER JOIN lines AS l
            ON t1.Batch_Num = l.Batch_Num
        FULL OUTER JOIN JenX.dbo.Users AS t2
            ON t1.Badge_Num = t2.UserName
        WHERE ( t1.DateCreated BETWEEN '02/04/2014' AND '02/05/2014' )

next to get the SUM you would just add SUM and GROUP BY clause

WITH    lines
          AS ( SELECT Batch_num
                   ,COUNT(*) AS lines
                FROM Pick
                GROUP BY Batch_num)
    SELECT t1.Badge_Num
           ,t1.Batch_Num
           ,t2.UserDescription
           ,SUM(l.lines) AS totallines
        FROM Pack AS t1 WITH ( NOLOCK )
        LEFT OUTER JOIN lines AS l
            ON t1.Batch_Num = l.Batch_Num
        FULL OUTER JOIN JenX.dbo.Users AS t2
            ON t1.Badge_Num = t2.UserName
        WHERE ( t1.DateCreated BETWEEN '02/04/2014' AND '02/05/2014' )
        GROUP BY t1.Badge_Num
           ,t1.Batch_Num
           ,t2.UserDescription
        ORDER BY t2.UserDescription
           ,t1.Badge_Num
SELECT  lines.Badge_Num,
        u.UserDescription,
        lines.Batches,
        totallines = lines.Lines
FROM    (   SELECT  pa.Badge_Num,
                    Batches = COUNT(DISTINCT pa.Batch_Num),
                    Lines = COUNT(*)
            FROM    dbo.Pack pa
                    JOIN dbo.Pick pic ON pa.Batch_Num = pic.Batch_Num
            WHERE pa.DateCreated BETWEEN '2014-02-04' AND '2014-02-05'
            GROUP BY pa.Badge_Num
        ) lines
        LEFT JOIN JenX.dbo.Users u ON lines.Badge_Num = u.UserName
ORDER BY UserDescription, Badge_Num;

Edited 2014-02-05 10:11 +04 – I added the number of batches which, based on the accepted answer (but not, interestingly, the actual question) is what the OP wants.

Edited 2014-02-05 10:19 +04 – I added the dbo.Pick table, which I didn't notice until now. Oops!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top