将多个查询作为单个结果集返回到相同的数据库列

时间:2014-02-28 17:16:20

标签: sql sql-server datetime aggregate-functions

因此,我正在尝试创建一份报告,该报告将为我提供销售订单的计数,并将其与之前的日期范围进行比较。遗憾的是,我不确定如何回归结果,因为每个计算都是针对同一个表列运行的。

理想情况下,我的输出看起来像这样,包括NULL值

partner    Today    LastYear    TwoYear
-------    ------   --------    --------
zzz        10       15          4
yyy        2                    4
xxx        3        1           2

我有基本的想法:

DECLARE @currentDay DATETIME
SET @currentDay =  DATEDIFF(day,0,GETDATE()) -- Gives it 00:00:00.000 for time

-- Todays orders
SELECT count(s.po_id) as 'Orders Today',c.tp_name
FROM [EDI_001].[dbo].[303v850h] as s
join [EDI_001].[dbo].[Trade] as c
on s.TP_PartID = c.TP_PartID
where s.ExportDate < @currentDay AND
      s.ExportDate > DATEADD(day,-1,@currentDay)
group by c.tp_name
order by c.tp_name;

-- Last Years Day's orders
SELECT count(s.po_id) as 'Orders Today',c.tp_name
FROM [EDI_001].[dbo].[303v850h] as s
join [EDI_001].[dbo].[Trade] as c
on s.TP_PartID = c.TP_PartID
where s.ExportDate < DATEADD(year,-1,@currentDay) AND
      s.ExportDate > DATEADD(year, -1,DATEADD(day,-1,@currentDay))
group by c.tp_name
order by c.tp_name;

我会继续前进并停在那里,因为您可以看到查询几乎相同,只是更改了where子句中的日期范围。我不知道的是如何将两个查询组合成一个结果集。同样,我的连接不会在任一查询中返回空集。我意识到它不会与当前使用的连接一起使用,但它没有在左外连接的不同结果中显示......但实际上一次出现一个问题,第一步是获得单个结果集。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

您正在寻找UNION运营商。 它用于组合两个或多个SELECT语句的结果集。

http://www.w3schools.com/sql/sql_union.asp

答案 1 :(得分:0)

您可以使用条件聚合:

SELECT  c.tp_name,
        Today = COUNT(CASE WHEN s.ExportDate > DATEADD(DAY,-1,@currentDay) THEN s.po_id END),
        LastYear = COUNT(CASE WHEN s.ExportDate > DATEADD(YEAR,-1,@currentDay) 
                            AND s.ExportDate < DATEADD(YEAR, -1,DATEADD(DAY, -1, @currentDay))THEN s.po_id END),
        TwoYear = COUNT(CASE WHEN s.ExportDate > DATEADD(YEAR,-2, @currentDay) 
                            AND s.ExportDate < DATEADD(YEAR, -2, DATEADD(DAY, -1, @currentDay))THEN s.po_id END),
FROM    [EDI_001].[dbo].[303v850h] as s
        JOIN [EDI_001].[dbo].[Trade] as c
            ON s.TP_PartID = c.TP_PartID
WHERE s.ExportDate < @currentDay AND
      s.ExportDate > DATEADD(YEAR, -2, DATEADD(DAY, -1, @currentDay))
GROUP BY c.tp_name
ORDER BY c.tp_name;

因此,您基本上将每个WHERE子句移动到COUNT中的CASE语句,因此您只计算符合条件的记录。

答案 2 :(得分:0)

DECLARE @currentDay DATETIME
SET     @currentDay = DATEDIFF(day,0,GETDATE()) -- Gives it 00:00:00.000 for time

SELECT  Sum(
            CASE 
            WHEN s.ExportDate Between DATEADD(day,-1,@currentDay) AND @currentDay 
            THEN 1 
            ELSE 0 
            END
            ) As Today,
        Sum(
            CASE 
            WHEN s.ExportDate Between DATEADD(year, -1,DATEADD(day,-1,@currentDay)) AND DATEADD(year,-1,@currentDay) 
            THEN 1 
            ELSE 0 
            END
            ) As LastYear,
        Sum(
            CASE 
            WHEN s.ExportDate Between DATEADD(year, -2,DATEADD(day,-1,@currentDay)) AND DATEADD(year,-2,@currentDay) 
            THEN 1 
            ELSE 0 
            END
            ) As TwoYear,
        c.tp_name
FROM    [EDI_001].[dbo].[303v850h] as s
JOIN    [EDI_001].[dbo].[Trade] as c
        on  s.TP_PartID = c.TP_PartID
GROUP   BY c.tp_name
ORDER   BY c.tp_name;