Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择“按上个月添加的新项目计数”_Sql_Sql Server_Sql Server 2008 R2 - Fatal编程技术网

Sql 选择“按上个月添加的新项目计数”

Sql 选择“按上个月添加的新项目计数”,sql,sql-server,sql-server-2008-r2,Sql,Sql Server,Sql Server 2008 R2,我有以下SQL表 Country_ID | Country | ----------------------- 1 | France | 2 | England | 3 | Germany | 4 | Scotland | ----------------------- WorkList_ID | Places | DateOfCreation | -------------------------------

我有以下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)

我使用了左连接,以防有些国家还没有新的连接。这样,即使没有订单,也可以列出所有国家/地区

SELECT Country, NVL(pt.total_count,0), NVL(nt.new_count,0)
FROM country_table cl
LEFT JOIN (SELECT worklist _id, 
      count(worklist_id) as total_count 
      FROM places_table 
      GROUP BY worklist_id) pt ON pt.worklist_id = cl.country_id
LEFT JOIN (SELECT worklist _id, 
     count(worklist_id) as new_count 
     FROM places_table 
     WHERE dataofcreate > yourdate  
     GROUP BY worklist_id) nt ON nt.worklist_id = cl.country_id

我使用了左连接,以防有些国家还没有新的连接。这样,即使没有订单,也可以列出所有国家/地区

SELECT Country, NVL(pt.total_count,0), NVL(nt.new_count,0)
FROM country_table cl
LEFT JOIN (SELECT worklist _id, 
      count(worklist_id) as total_count 
      FROM places_table 
      GROUP BY worklist_id) pt ON pt.worklist_id = cl.country_id
LEFT JOIN (SELECT worklist _id, 
     count(worklist_id) as new_count 
     FROM places_table 
     WHERE dataofcreate > yourdate  
     GROUP BY worklist_id) nt ON nt.worklist_id = cl.country_id

这应该可以完成任务。只需简单的左连接,就可以使用上个月的大小写表达式:

USE Sandbox;
GO

CREATE TABLE #Country (Country_ID int, Country varchar(10));
INSERT INTO #Country
VALUES (1,'France'),
       (2,'England'),
       (3,'Germany'),
       (4,'Scotland');

CREATE TABLE #Worklist (WorkList_ID int, Places varchar(10), DateOfCreation date);
INSERT INTO #Worklist
SELECT Id, Place, CONVERT(date, Creation,103)
FROM (VALUES (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')) WL(ID, Place, Creation);
GO
SELECT C.Country,
       COUNT(WL.WorkList_ID) AS Total,
       COUNT(CASE WHEN DateOfCreation >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1,0) AND DateOfCreation < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),0) THEN WL.WorkList_ID END) AS LastMonth
FROM #Country C
     LEFT JOIN #Worklist WL ON C.Country = WL.Places
GROUP BY C.Country

GO
DROP TABLE #Country;
DROP TABLE #Worklist;

这应该可以完成任务。只需简单的左连接,就可以使用上个月的大小写表达式:

USE Sandbox;
GO

CREATE TABLE #Country (Country_ID int, Country varchar(10));
INSERT INTO #Country
VALUES (1,'France'),
       (2,'England'),
       (3,'Germany'),
       (4,'Scotland');

CREATE TABLE #Worklist (WorkList_ID int, Places varchar(10), DateOfCreation date);
INSERT INTO #Worklist
SELECT Id, Place, CONVERT(date, Creation,103)
FROM (VALUES (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')) WL(ID, Place, Creation);
GO
SELECT C.Country,
       COUNT(WL.WorkList_ID) AS Total,
       COUNT(CASE WHEN DateOfCreation >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1,0) AND DateOfCreation < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),0) THEN WL.WorkList_ID END) AS LastMonth
FROM #Country C
     LEFT JOIN #Worklist WL ON C.Country = WL.Places
GROUP BY C.Country

GO
DROP TABLE #Country;
DROP TABLE #Worklist;

您如何定义上个月?作为一个有用的提示,在提供日期时,请以确定的格式提供日期。我们中的一些人无法使用这些数据,因为一年中没有13个月。yyyyMMdd是最首选的格式:上个月是从今天开始的一个月前。为了澄清日期格式是DD/MM/YYYY,我很抱歉没有明确说明。您如何定义上个月?作为一个有用的提示,在提供日期时,请以确定的格式提供。我们中的一些人无法使用这些数据,因为一年中没有13个月。yyyyMMdd是最首选的格式:上个月是从今天开始的一个月前。为了澄清日期格式是DD/MM/YYYY,我很抱歉没有说清楚。