找到"项目"算特别"用户"对于不同的间隔天

时间:2015-06-29 14:13:01

标签: sql sql-server sql-server-2008

user_table
........................................
| user_id   |  user_name  |  password  |
""""""""""""""""""""""""""""""""""""""""

project_table
..............................................
|  Project_id  |  user_id  |  Date_assigned  |
''''''''''''''''''''''''''''''''''''''''''''''

我有两个表,一个是User_table,另一个是Project_table! 我需要从这两个表中生成报告!

必需的输出是:

..................................................................
|  user_name |   0 - 10 Days  |   10 - 20 days |  20 - 30 days   |
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

输出表包含在过去0到10天内分配给特定用户的项目数量! 另一列包含从当天起10至20天内分配的项目数。 最后一列包含从当天起20至30天内分配的项目数。

这是我尝试过的SQL查询:

select 
User_table.User_Name, COUNT(1)Project_0_to_10 
from user_table
INNER join Project_table as tb_project on tb_project.DateAssigned 
between  DATEADD(DD,-10,GETDATE()) and DATEADD(DD,0,GETDATE()) and
tb_project.user_id=User_table.User_id
group by user_table.user_name

这将以此格式输出

  ...................................
  |  user_name  |  Project_0_to_10  |
  '''''''''''''''''''''''''''''''''''

我无法找到其他两列的计数! 谢谢!

2 个答案:

答案 0 :(得分:2)

你走在正确的轨道上。既然你知道如何创建

-------------------------------
| user_name | Project_0_to_10 | 
-------------------------------

我确信你也可以创建

--------------------------------
| user_name | Project_10_to_20 | 
--------------------------------

--------------------------------
| user_name | Project_20_to_30 | 
--------------------------------

正确?让我们调用那些SQL A,B和C.现在你需要做的就是组合它们:

WITH 
  A AS (/* your A SQL here */),
  B AS (/* your B SQL here */),
  C AS (/* your C SQL here */)
SELECT COALESCE(a.user_name, b.user_name, c.user_name)
     , COALESCE(A.Project_0_to_10, 0)
     , COALESCE(B.Project_10_to_20, 0)
     , COALESCE(C.Project_20_to_30, 0)
  FROM A
       FULL OUTER JOIN B ON A.user_name = B.user_name
       FULL OUTER JOIN C ON B.user_name = C.user_name

FULL OUTER JOIN确保列出所有用户,即使他们只出现在A,B或C中的一个用户.COALESCE确保在未列入A之一的用户的相应列中显示0 ,B和C.

答案 1 :(得分:2)

例如,您可以使用条件计数。这可以这样实现:

-- Create demo data
CREATE TABLE #user(user_id int identity(1,1), user_name nvarchar(50))

INSERT INTO #user(user_name) VALUES(N'A'),(N'B')

CREATE TABLE #projects(project_id int, user_id int, date_assigned datetime)

INSERT INTO #projects(project_id, user_id, date_assigned)
VALUES  (1,1,GETDATE()),(1,2,DATEADD(Day,-11,GETDATE())), (2,2,DATEADD(day,-11,GETDATE())), (3,2,DATEADD(day,-21,GETDATE())),
        (2,1,DATEADD(day,-21,GETDATE())),(3,1,DATEADD(day,-21,GETDATE()))

-- your part
SELECT u.user_name, 
    SUM(CASE WHEN p.date_assigned BETWEEN DATEADD(day,-10,GETDATE()) AND GETDATE() THEN 1 ELSE 0 END) last_10_days,
    SUM(CASE WHEN p.date_assigned BETWEEN DATEADD(day,-20,GETDATE()) AND DATEADD(day,-11,GETDATE()) THEN 1 ELSE 0 END) last_20_days,
    SUM(CASE WHEN p.date_assigned BETWEEN DATEADD(day,-30,GETDATE()) AND DATEADD(day,-21,GETDATE()) THEN 1 ELSE 0 END) last_30_days
FROM #user as u
INNER JOIN #projects as p
        ON u.user_id = p.user_id
GROUP BY u.user_name

-- cleanup
DROP TABLE #projects
DROP TABLE #user