使用where和groupby子选择SQL查询

时间:2016-09-05 15:33:46

标签: sql sql-server tsql

当我在SQL Server中尝试下面的代码时,

SELECT       
dbo.Category.CatNo as Category, 
dbo.Category.Categaory as Name, 
(select SUM(dbo.SALES.SELLINC) where (dbo.SALES.DATE BETWEEN '2016-07-17' AND '2016-07-23')) AS ActualSales, 
(select SUM(dbo.SALES.SELLINC) where (dbo.SALES.DATE BETWEEN '2015-07-19' AND '2015-07-25')) AS LastYrVariance,
(select SUM(dbo.SALES.SELLINC) where (dbo.SALES.DATE BETWEEN '2016-01-01' AND '2016-09-05')) AS YrToDateActual,
(select SUM(dbo.SALES.SELLINC) where (dbo.SALES.DATE BETWEEN '2015-01-01' AND '2015-09-05')) AS LastYrToDateActual  
FROM dbo.Category INNER JOIN
     dbo.Dissection ON dbo.Category.CatNo = dbo.Dissection.CatNo INNER JOIN
     dbo.Division ON dbo.Dissection.DivNo = dbo.Division.ID INNER JOIN
     dbo.Departments ON dbo.Dissection.DeptNo = dbo.Departments.DeptID INNER JOIN
     dbo.SALES ON dbo.Dissection.DissNo = dbo.SALES.CODE
WHERE (dbo.SALES.BRN = 1) 
GROUP BY dbo.Category.CatNo, dbo.Category.Categaory
ORDER BY dbo.Category.CatNo

我收到以下错误消息,

  

Msg 8120,Level 16,State 1,Line 2       列'dbo.Category.CatNo'在选择列表中无效,因为它不包含在聚合函数或GROUP BY中   子句。

请帮忙吗?

3 个答案:

答案 0 :(得分:2)

子查询有一个select子句,带有一个Sum()函数,该函数尝试对子查询中未提及的表的列求值。 (子查询没有FROM子句)。这些子查询不应该是子查询,它们只需要是Sum()表达式。 您应该检查使用别名来简化SQL脚本。

SELECT c.CatNo Category, c.Categaory Name, 
     sum(case when s.Date between '2016-07-17' and '2016-07-23' 
           then s.SELLINC else 0 end) ActualSales,
     sum(case when s.Date between '2015-07-25' and '2015-07-19'
           then s.SELLINC else 0 end) LastYrVariance,
     sum(case when s.Date between '2016-01-01' and '2016-09-05' 
           then s.SELLINC else 0 end) YrToDateActual,
     sum(case when s.Date between '2015-01-01' AND '2015-09-05'
           then s.SELLINC else 0 end) LastYrToDateActual 
FROM dbo.Category c
      join dbo.Dissection d on d.CatNo = c.CatNo 
      join dbo.Division v on v.Id = d.DivNo 
      join dbo.Departments p on p.DeptID = d.DeptNo 
      join dbo.SALES s on s.code = d.DissNo 
WHERE s.BRN = 1 
GROUP BY c.CatNo, c.Categaory
ORDER BY c.CatNo

根据评论中的要求动态生成日期范围,使用datediff和dateAdd函数:我不确定你的意思上周(2016年31周)最后同一周(2015年31周),所以如果这不对,你可以修改为正确。

SELECT c.CatNo Category, c.Categaory Name, 
     sum(case when s.Date between 
           dateAdd(week, datediff(week, 0, getdate())-52, 0) 
       and dateAdd(week, datediff(week, 0, getdate()), 0) 
           then s.SELLINC else 0 end) ActualSales,
     sum(case when s.Date between '2015-07-25' and '2015-07-19'
           then s.SELLINC else 0 end) LastYrVariance,
     sum(case when s.Date between '2016-01-01' and '2016-09-05' 
           then s.SELLINC else 0 end) YrToDateActual,
     sum(case when s.Date between '2015-01-01' AND '2015-09-05'
           then s.SELLINC else 0 end) LastYrToDateActual 
FROM dbo.Category c
      join dbo.Dissection d on d.CatNo = c.CatNo 
      join dbo.Division v on v.Id = d.DivNo 
      join dbo.Departments p on p.DeptID = d.DeptNo 
      join dbo.SALES s on s.code = d.DissNo 
WHERE s.BRN = 1 
GROUP BY c.CatNo, c.Categaory
ORDER BY c.CatNo

答案 1 :(得分:1)

我无法找到在查询中生成该错误的原因。通过使用sub-queries语句

,您可以简单地使用CASE
SELECT dbo.category.catno     AS Category, 
       dbo.category.categaory AS NAME, 
       Sum(CASE WHEN dbo.sales.date BETWEEN '2016-07-17' AND '2016-07-23' THEN dbo.sales.sellinc END) AS ActualSales, 
       Sum(CASE WHEN dbo.sales.date BETWEEN '2015-07-19' AND '2015-07-25' THEN dbo.sales.sellinc END) AS LastYrVariance, 
       Sum(CASE WHEN dbo.sales.date BETWEEN '2016-01-01' AND '2016-09-05' THEN dbo.sales.sellinc END) AS YrToDateActual, 
       Sum(CASE WHEN dbo.sales.date BETWEEN '2015-01-01' AND '2015-09-05' THEN dbo.sales.sellinc END) AS LastYrToDateActual 
FROM   dbo.category 
       INNER JOIN dbo.dissection 
               ON dbo.category.catno = dbo.dissection.catno 
       INNER JOIN dbo.division 
               ON dbo.dissection.divno = dbo.division.id 
       INNER JOIN dbo.departments 
               ON dbo.dissection.deptno = dbo.departments.deptid 
       INNER JOIN dbo.sales 
               ON dbo.dissection.dissno = dbo.sales.code 
WHERE  ( dbo.sales.brn = 1 ) 
GROUP  BY dbo.category.catno, 
          dbo.category.categaory 
ORDER  BY dbo.category.catno 

答案 2 :(得分:0)

您可以尝试使用以下查询。

 SELECT   Category.CatNo as Category, Category.Categaory as Name, 
(select SUM(SALES.SELLINC) where (SALES.DATE BETWEEN '2016-07-17' AND '2016-07-23')) AS ActualSales, 
(select SUM(SALES.SELLINC) where (SALES.DATE BETWEEN '2015-07-19' AND '2015-07-25')) AS LastYrVariance,
(select SUM(SALES.SELLINC) where (SALES.DATE BETWEEN '2016-01-01' AND '2016-09-05')) AS YrToDateActual,
(select SUM(SALES.SELLINC) where (SALES.DATE BETWEEN '2015-01-01' AND '2015-09-05')) AS LastYrToDateActual  
FROM            dbo.Category INNER JOIN
                         dbo.Dissection ON Category.CatNo = Dissection.CatNo INNER JOIN
                         dbo.Division ON Dissection.DivNo = Division.ID INNER JOIN
                         dbo.Departments ON Dissection.DeptNo = Departments.DeptID INNER JOIN
                         dbo.SALES ON Dissection.DissNo = SALES.CODE
WHERE        (SALES.BRN = 1) 

GROUP BY Category.CatNo, Category.Categaory
ORDER BY Category.CatNo