【发布时间】:2018-03-09 16:49:35
【问题描述】:
我有以下 SQL 表...
Country_ID | Country |
-----------------------
1 | France |
2 | England |
3 | Germany |
4 | Scotland |
-----------------------
WorkList_ID | Places | DateOfCreation |
----------------------------------------
1 | France | 01/02/2018 |
2 | England | 11/01/2018 |
3 | Germany | 21/02/2018 |
1 | France | 13/03/2017 |
2 | England | 21/01/2018 |
4 | Scotland | 04/03/2018 |
2 | England | 08/02/2018 |
3 | Germany | 13/03/2017 |
1 | France | 09/02/2018 |
2 | England | 11/03/2017 |
---------------------------------------
使用国家/地区键来链接表格。如何生成下表:
Country | Total Count | New From Last month |
--------------------------|----------------------
France | 3 | 1 |
England | 4 | 2 |
Germany | 2 | 1 |
Scotland | 1 | 0 |
--------------------------------------------------
我想显示工作列表的总数以及上个月添加的所有新列表?
这是我目前所拥有的:
(SELECT Places, COUNT(Places) as 'Outstanding List'
FROM WorkList
WHERE Places IN
(SELECT Country FROM tblCountry)
GROUP BY Places)
【问题讨论】:
-
你如何定义“上个月”?
-
作为一个有用的提示,在提供日期时,请以确定的格式提供。我们中的一些人不能使用这些数据,因为一年中没有 13 个月。
yyyyMMdd是最首选的格式。 :) -
上个月是从今天开始的 1 个月前。
-
只是为了澄清日期格式是 DD/MM/YYYY 但我很抱歉没有说清楚。
标签: sql sql-server sql-server-2008-r2