一个结果中有多个SQL查询

时间:2014-03-26 11:13:42

标签: sql sql-server tsql

我正在努力找出编写此查询的最佳方法,以便在可能的情况下使用更少的查询。我想知道数据透视表是否可能是正确的方法?

我的3个单独查询:

SELECT
ISNULL(SUM(ps.UnitsSold), 0) AS UnitsSold,
ISNULL(pg.[Description], 'Other') AS [Description]
FROM dbo.ProductSales ps
LEFT OUTER JOIN dbo.Product p ON ps.ProductID = p.ProductID
LEFT OUTER JOIN dbo.ProductGroupings pg ON p.[Asin] = pg.[Asin] 
WHERE (ps.OrderDate BETWEEN GETDATE() - 10 AND GETDATE() - 3) AND     ps.DistributionCentreID IN (3)
GROUP BY pg.[Description], ps.DistributionCentreID

SELECT
ISNULL(SUM(ps.UnitsSold), 0) AS UnitsSold,
ISNULL(pg.[Description], 'Other') AS [Description]
FROM dbo.ProductSales ps
LEFT OUTER JOIN dbo.Product p ON ps.ProductID = p.ProductID
LEFT OUTER JOIN dbo.ProductGroupings pg ON p.[Asin] = pg.[Asin] 
WHERE (ps.OrderDate BETWEEN GETDATE() - 17 AND GETDATE() - 10) AND     ps.DistributionCentreID IN (3)
GROUP BY pg.[Description], ps.DistributionCentreID

SELECT
ISNULL(SUM(ps.UnitsSold), 0) AS UnitsSold,
ISNULL(pg.[Description], 'Other') AS [Description]
FROM dbo.ProductSales ps
LEFT OUTER JOIN dbo.Product p ON ps.ProductID = p.ProductID
LEFT OUTER JOIN dbo.ProductGroupings pg ON p.[Asin] = pg.[Asin] 
WHERE (ps.OrderDate BETWEEN GETDATE() - 374 AND GETDATE() - 367) AND     ps.DistributionCentreID IN (3)
GROUP BY pg.[Description], ps.DistributionCentreID

这会产生与此类似的结果(第一个查询):

 UnitsSold Description

 4154      desc1
 764       desc2

等。

要考虑的事情,其中​​一个查询中可能不存在描述(产品组),因此我需要考虑到这一点。

理想情况下,我希望它看起来像这样:

Description UnitsSoldThisWeek UnitsSoldLastWeek UnitsSoldLastYear

Desc1       54                45                37

等..

由于查询错误导致的任何问题,问题或婊子都是可以接受的,我很乐意提高对SQL的理解。

谢谢,

迈克尔

3 个答案:

答案 0 :(得分:2)

使用一个查询和条件聚合执行此操作:

SELECT coalesce(pg.[Description], 'Other') AS [Description],
       sum(case when ps.OrderDate BETWEEN GETDATE() - 10 AND GETDATE() - 3 then UnitsSold
           end) as ThisWeek
       sum(case when ps.OrderDate BETWEEN GETDATE() - 17 AND GETDATE() - 10 then UnitsSold
                else 0
           end) as LastWeek,
       sum(case when ps.OrderDate BETWEEN GETDATE() - 374 AND GETDATE() - 367 then UnitsSold
                else 0
           end) as LastYear
FROM dbo.ProductSales ps LEFT OUTER JOIN
     dbo.Product p
     ON ps.ProductID = p.ProductID LEFT OUTER JOIN
     dbo.ProductGroupings pg
     ON p.[Asin] = pg.[Asin] 
WHERE ps.DistributionCentreID IN (3)
GROUP BY pg.[Description], ps.DistributionCentreID

答案 1 :(得分:1)

我稍微更改了您的查询以显示将您的3个查询组合在一起的不同技术。我可能没有在SUMS周围的括号,但这给你一个想法。还有更好的方法来做你正在尝试的“日期之间”的东西,但这不是问题!看看是否有帮助

SELECT
    ISNULL(SUM(ps.UnitsSold), 0) AS UnitsSold,
    ISNULL(pg.[Description], 'Other') AS [Description]
    SUM(CASE WHEN (ps.OrderDate BETWEEN GETDATE() - 10 AND GETDATE() - 3) AND ps.DistributionCentreID = 3 THEN 1 ELSE 0 END) AS UnitsSoldThisWeek 
    SUM(CASE WHEN (ps.OrderDate BETWEEN GETDATE() - 17 AND GETDATE() - 10) AND ps.DistributionCentreID = 3 THEN 1 ELSE 0 END) AS UnitsSoldThisWeek 
    SUM(CASE WHEN (ps.OrderDate BETWEEN GETDATE() - 374 AND GETDATE() - 367) AND ps.DistributionCentreID = 3 THEN 1 ELSE 0 END) AS UnitsSoldThisWeek 

FROM dbo.ProductSales ps
LEFT OUTER JOIN dbo.Product p ON ps.ProductID = p.ProductID
LEFT OUTER JOIN dbo.ProductGroupings pg ON p.[Asin] = pg.[Asin] 
GROUP BY pg.[Description], ps.DistributionCentreID

答案 2 :(得分:1)

;WITH CTE AS (
Select [UnitsSoldThisWeek],[UnitsSoldLastWeek],[UnitsSoldLastYear] FROM (
SELECT
ISNULL(SUM(ps.UnitsSold), 0) AS UnitsSold,
ISNULL(pg.[Description], 'Other') AS [Description]
FROM dbo.ProductSales ps
LEFT OUTER JOIN dbo.Product p ON ps.ProductID = p.ProductID
LEFT OUTER JOIN dbo.ProductGroupings pg ON p.[Asin] = pg.[Asin] 
WHERE (ps.OrderDate BETWEEN GETDATE() - 10 AND GETDATE() - 3) AND     ps.DistributionCentreID IN (3)
GROUP BY pg.[Description], ps.DistributionCentreID
UNION ALL
SELECT
ISNULL(SUM(ps.UnitsSold), 0) AS UnitsSold,
ISNULL(pg.[Description], 'Other') AS [Description]
FROM dbo.ProductSales ps
LEFT OUTER JOIN dbo.Product p ON ps.ProductID = p.ProductID
LEFT OUTER JOIN dbo.ProductGroupings pg ON p.[Asin] = pg.[Asin] 
WHERE (ps.OrderDate BETWEEN GETDATE() - 17 AND GETDATE() - 10) AND     ps.DistributionCentreID IN (3)
GROUP BY pg.[Description], ps.DistributionCentreID
UNION ALL
SELECT
ISNULL(SUM(ps.UnitsSold), 0) AS UnitsSold,
ISNULL(pg.[Description], 'Other') AS [Description]
FROM dbo.ProductSales ps
LEFT OUTER JOIN dbo.Product p ON ps.ProductID = p.ProductID
LEFT OUTER JOIN dbo.ProductGroupings pg ON p.[Asin] = pg.[Asin] 
WHERE (ps.OrderDate BETWEEN GETDATE() - 374 AND GETDATE() - 367) AND     ps.DistributionCentreID IN (3)
GROUP BY pg.[Description], ps.DistributionCentreID
)
PIVOT(MAX(UnitsSold)FOR Description IN([UnitsSoldThisWeek],[UnitsSoldLastWeek],[UnitsSoldLastYear])) )
)
Select [UnitsSoldThisWeek],[UnitsSoldLastWeek],[UnitsSoldLastYear] from CTE

我不太确定,因为缺乏数据,但我们也可以这样继续