在SQL中添加行

时间:2017-02-20 15:59:51

标签: sql sql-server ssms

哦,所以我有这个东西。我想知道每个实体每周的订单数量。 我从这个问题开始:

select 
DATEPART(YEAR, OrderDate) AS 'Year',
DATEPART(WEEK, OrderDate) AS 'Week',
COUNT(*) as 'Amount of Orders',
EntityID
FROM tblOrder
WHERE DATEPART(YEAR, tblOrder.OrderDate) = 2015
GROUP BY EntityID,OrderDate
ORDER BY Week asc

结果很好,但到处都是双排:

Year | Week | Amount of Orders | EntityID
2015 |  1   |        1         |     1
2015 |  1   |        1         |     1
2015 |  1   |        1         |     1
2015 |  1   |        1         |     1
2015 |  1   |        1         |     1
2015 |  1   |        1         |    15  
2015 |  1   |        1         |    15  

我想要达到的目标是:

Year | Week | Amount of Orders | EntityID
2015 |  1   |        5         |     1
2015 |  1   |        2         |     2
2015 |  1   |        4         |     3
2015 |  1   |        9         |     4

等等。我的查询出了什么问题?

2 个答案:

答案 0 :(得分:2)

GROUP BY年和周而不是orderdate:

select [Year], [week], EntityID, count(*)
from
(
select DATEPART(YEAR, OrderDate) AS 'Year',
       DATEPART(WEEK, OrderDate) AS 'Week',
       EntityID
FROM tblOrder
WHERE DATEPART(YEAR, tblOrder.OrderDate) = 2015
) dt
GROUP BY [Year], [Week], EntityID
ORDER BY Week asc

因为同一年/周可以有来自不同日期的订单。如果您是GROUP BY日期,那么您将获得该年/周的多行。

答案 1 :(得分:0)

只需在group by子句中使用相同的表达式。

pyspark --master spark://myurl:7077