Sql 表子集比较

Sql 表子集比较,sql,sql-server,tsql,sql-server-2012,Sql,Sql Server,Tsql,Sql Server 2012,我正在使用AdventureWorks数据库和SQL Server 2012 T-SQL食谱手册,在以下示例中遇到了麻烦: 我必须从Sales.SalesorsonQuotaHistory查看2007年和2008年的销售配额 SELECT sp.BusinessEntityID , SUM(s2008.SalesQuota) AS '2008' , SUM(S2007.SalesQuota) AS '2007' FROM Sales.SalesPerson sp LEFT OUTER JOIN

我正在使用AdventureWorks数据库和SQL Server 2012 T-SQL食谱手册,在以下示例中遇到了麻烦:

我必须从Sales.SalesorsonQuotaHistory查看2007年和2008年的销售配额

SELECT sp.BusinessEntityID
, SUM(s2008.SalesQuota) AS '2008'
, SUM(S2007.SalesQuota) AS '2007'
FROM Sales.SalesPerson sp
LEFT OUTER JOIN Sales.SalesPersonQuotaHistory s2008
  ON sp.BusinessEntityID = s2008.BusinessEntityID
    AND YEAR(s2008.QuotaDate) = 2008
LEFT OUTER JOIN Sales.SalesPersonQuotaHistory s2007
  ON sp.BusinessEntityID = s2007.BusinessEntityID
    AND YEAR(s2007.QuotaDate) = 2007
GROUP BY sp.BusinessEntityID
第一个结果是:

  BusinessEntityID 2008                  2007
---------------- --------------------- ---------------------
  274              1084000.00            1088000.00
  275              6872000.00            9432000.00
  276              8072000.00            9364000.00
  277              6644000.00            8700000.00
就像书上说的

但随后我尝试通过以下查询获取2008年销售配额:

SELECT sp.BusinessEntityID,
       SUM(spqh.SalesQuota) AS '2008'
FROM Sales.SalesPerson sp
  LEFT JOIN Sales.SalesPersonQuotaHistory spqh
   ON sp.BusinessEntityID = spqh.BusinessEntityID
     AND YEAR(spqh.QuotaDate) = 2008 
GROUP BY sp.BusinessEntityID
得到这个:

 BusinessEntityID 2008
---------------- ---------------------
 274              271000.00
 275              1718000.00
 276              2018000.00
 277              1661000.00
我做错了什么?我想我错过了一些关于左连接的东西,但我不知道是什么

这一个给出了相同的结果:

SELECT BusinessEntityID
, SUM(SalesQuota) AS '2008'
FROM Sales.SalesPersonQuotaHistory
WHERE YEAR(QuotaDate) = 2008 
GROUP BY BusinessEntityID


 BusinessEntityID 2008
 ---------------- ---------------------
 274              271000.00
 275              1718000.00
 276              2018000.00

我不认为你的第一个例子是正确的

SELECT sp.BusinessEntityID, 
       SUM(CASE WHEN YEAR(s.QuotaDate) = 2007 THEN s.SalesQuota ELSE 0 END) AS '2007',
       SUM(CASE WHEN YEAR(s.QuotaDate) = 2008 THEN s.SalesQuota ELSE 0 END) AS '2008'
FROM Sales.SalesPerson sp
LEFT JOIN Sales.SalesPersonQuotaHistory s ON sp.BusinessEntityID = s.BusinessEntityID
GROUP BY sp.BusinessEntityID

您的第一个查询可能是创建结果的笛卡尔乘积。相反,我将使用条件聚合来获取这两个值(应该与您的第二个查询相匹配):


如果未从
Sales.salesson
表中返回任何列,则可以将其排除,并使用
pivot
获得所需的结果:

select
    BusinessEntityID,
    [2008],
    [2007]
from    (
        select
            BusinessEntityID,
            year(QuotaDate) as SalesQuotaYear,
            sum(SalesQuota) as SalesQuota
        from Sales.SalesPersonQuotaHistory
        where year(QuotaDate) in(2007,2008)
        group by BusinessEntityID,
            year(QuotaDate)
        ) as t
    pivot   (
            sum(SalesQuota)
            for SalesQuotaYear in([2007],[2008])
            ) as p
select
    BusinessEntityID,
    [2008],
    [2007]
from    (
        select
            BusinessEntityID,
            year(QuotaDate) as SalesQuotaYear,
            sum(SalesQuota) as SalesQuota
        from Sales.SalesPersonQuotaHistory
        where year(QuotaDate) in(2007,2008)
        group by BusinessEntityID,
            year(QuotaDate)
        ) as t
    pivot   (
            sum(SalesQuota)
            for SalesQuotaYear in([2007],[2008])
            ) as p