有人可以帮助我吗?我有大约5000行的表。
我的查询是:
SELECT (LTRIM(RTRIM(p.pers_name)) + ' ' + RTRIM(LTRIM(p.pers_lastname))) AS Users,
staz.staz AS StartDate, dt_atldata AS EndDate,
CASE WHEN t.dt_atl = 0 THEN 'Working'
WHEN t.dt_atl = 1 THEN 'Retired' END AS [Status]
FROM dba.tarn AS t
LEFT OUTER JOIN dba.darb AS d
ON t.darb_id = d.darb_id
LEFT OUTER JOIN dba.pers AS p
ON d.pers_id = p.pers_id
OUTER APPLY (SELECT MIN(staz_datan) AS [stazas]
FROM dba.staz AS s
WHERE p.pers_id = s.pers_id) AS staz
WHERE t.dt_tipas = 1
AND t.dt_atl <> 2
以下是我的查询返回的数据:
User1 2012-01-16 00:00 2012-07-04 00:00 Retired
User2 2007-11-08 00:00 2013-03-22 00:00 Retired
User3 2012-09-13 00:00 2012-01-05 00:00 Retired
User4 2012-02-07 00:00 2012-04-27 00:00 Retired
User5 2013-08-15 00:00 2013-12-23 00:00 Retired
User6 2011-09-01 00:00 NULL Working
User7 NULL 2012-01-05 00:00 Retired
User8 2012-06-19 00:00 2013-02-28 00:00 Retired
User9 2013-11-04 00:00 NULL Working
User10 2013-11-11 00:00 NULL Working
User11 2012-01-25 00:00 NULL Working
User12 2012-09-04 00:00 NULL Working
User13 2008-08-04 00:00 NULL Working
User14 2008-03-03 00:00 NULL Working
User15 2008-08-05 00:00 2013-02-25 00:00 Retired
User16 2011-08-20 00:00 2012-01-09 00:00 Retired
User17 NULL 2013-05-14 00:00 Retired
User18 2008-08-05 00:00 NULL Working
User19 2013-04-02 00:00 2013-08-30 00:00 Retired
User20 2012-12-05 00:00 2013-02-04 00:00 Retired
and etc
我想得到以下结果:
在指定日期之前总共工作了多少用户(包括减去退休用户)。
示例2011-08等
当然结果密钥将是数年和数月。
我应该使用哪个查询来获得此结果?
我需要过去几年和几个月的历史,有多少用户工作过......
答案 0 :(得分:0)
试试这个:
WITH YourQuery AS (SELECT (LTRIM(RTRIM(p.pers_name)) + ' ' + RTRIM(LTRIM(p.pers_lastname))) AS Users,
staz.staz AS StartDate, dt_atldata AS EndDate,
CASE WHEN t.dt_atl = 0 THEN 'Working'
WHEN t.dt_atl = 1 THEN 'Retired' END AS [Status]
FROM dba.tarn AS t
LEFT OUTER JOIN dba.darb AS d
ON t.darb_id = d.darb_id
LEFT OUTER JOIN dba.pers AS p
ON d.pers_id = p.pers_id
OUTER APPLY (SELECT MIN(staz_datan) AS [stazas]
FROM dba.staz AS s
WHERE p.pers_id = s.pers_id) AS staz
WHERE t.dt_tipas = 1
AND t.dt_atl <> 2)
SELECT 'Working', ISNULL(DATEPART(year, EndDate), DATEPART(year, GETDATE())) AS year,
ISNULL(DATEPART(month, EndDate), DATEPART(month, GETDATE())) AS month,
(SELECT COUNT(*)
FROM YourQuery r1
WHERE DATEPART(year, ISNULL(r1.EndDate, GETDATE())) * 100 + DATEPART(month, ISNULL(r1.EndDate, GETDATE()))
>= DATEPART(year, ISNULL(r.EndDate, GETDATE())) * 100 + DATEPART(month, ISNULL(r.EndDate, GETDATE()))
AND DATEPART(year, ISNULL(r1.StartDate, 1900)) * 100 + DATEPART(month, ISNULL(r1.StartDate, 1))
<= DATEPART(year, ISNULL(r.EndDate, GETDATE())) * 100 + DATEPART(month, ISNULL(r.EndDate, GETDATE()))
) AS counter
FROM YourQuery r
GROUP BY DATEPART(year, ISNULL(EndDate, GETDATE())),
DATEPART(month, ISNULL(EndDate, GETDATE()))
UNION
SELECT 'Retired', ISNULL(DATEPART(year, EndDate), DATEPART(year, GETDATE())) AS year,
ISNULL(DATEPART(month, EndDate), DATEPART(month, GETDATE())) AS month,
(SELECT COUNT(*)
FROM YourQuery r1
WHERE DATEPART(year, ISNULL(r1.EndDate, GETDATE())) * 100 + DATEPART(month, ISNULL(r1.EndDate, GETDATE()))
<= DATEPART(year, ISNULL(r.EndDate, GETDATE())) * 100 + DATEPART(month, ISNULL(r.EndDate, GETDATE()))
) * -1 AS counter
FROM YourQuery r
GROUP BY DATEPART(year, ISNULL(EndDate, GETDATE())),
DATEPART(month, ISNULL(EndDate, GETDATE()))
ORDER BY year, month