Combining SQL Server Queries

后端 未结 1 519
余生分开走
余生分开走 2021-01-24 11:07

I am using SQL Server and I have two tables and I would like to combine into one query that I can use to fill a gridview.

Table1 dbo.Work
UID (PK,

相关标签:
1条回答
  • 2021-01-24 11:35

    You can JOIN them. Assuming that Work is the main table, it should be like this:

    SELECT A.*, B.Orders
    FROM (  SELECT  [Agent],
                    SUM(CASE WHEN [Date_Added] BETWEEN @startDate AND @endDate THEN 1 ELSE 0 END) AS 'New',
                    SUM(CASE WHEN [Date_Updated] BETWEEN @startDate AND @endDate THEN 1 ELSE 0 END) AS 'Worked',
                    SUM(CASE WHEN [Completed] = 'yes' AND [Date_Updated] BETWEEN @startDate AND @endDate THEN 1 ELSE 0 END) AS 'Completed',
                    SUM(CASE WHEN [Failure_Analysis] = 'yes' AND [Date_Updated] BETWEEN @startDate AND @endDate THEN 1 ELSE 0 END) AS 'FA'
            FROM Work 
            GROUP BY [Agent]) A
    LEFT JOIN (SELECT   [Agent]
                        SUM(CASE WHEN [Date] BETWEEN @startDate AND @endDate THEN 1 ELSE 0 END) AS 'Orders' 
                FROM Orders 
                GROUP BY [Agent]) B
    ON A.[Agent] = B.[Agent]
    
    0 讨论(0)
提交回复
热议问题