这可能不是最好的方法,但这就是我想出来的。这显示了M-F之间每天产品类型的每个工作周的运行总计。我真的很想订购它,以便本周计数最高的那些是最重要的。有没有办法做到这一点?我正在使用SQL Server 2008 R2。
SELECT descr,
(SELECT COUNT(1) FROM case1 c WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) AND case1.product_type_id = c.product_type_id) Monday,
(SELECT COUNT(1) FROM case1 c WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 1) AND case1.product_type_id = c.product_type_id) Tuesday,
(SELECT COUNT(1) FROM case1 c WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 2) AND case1.product_type_id = c.product_type_id) Wednesday,
(SELECT COUNT(1) FROM case1 c WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 3) AND case1.product_type_id = c.product_type_id) Thursday,
(SELECT COUNT(1) FROM case1 c WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 4) AND case1.product_type_id = c.product_type_id) Friday
FROM case1 WITH(NOLOCK)
INNER JOIN product_type WITH(NOLOCK)
ON case1.product_type_id = product_type.product_type_id
WHERE case1.product_type_id IN(SELECT product_type_id FROM case1 WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) BETWEEN DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) AND DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 4))
GROUP BY case1.product_type_id, descr
ORDER BY descr
答案 0 :(得分:1)
将查询放在子查询中,并在外部查询中执行顺序。
select T.descr,
T.Monday,
T.Tuesday,
T.Wednesday,
T.Thursday,
T.Friday
from (
-- Your query goes here
) as T
order by T.Monday + T.Tuesday + T.Wednesday + T.Thursday + T.Friday desc
答案 1 :(得分:0)
试试这个:
SELECT descr,
(SELECT COUNT(1) FROM case1 c WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) AND case1.product_type_id = c.product_type_id) Monday,
(SELECT COUNT(1) FROM case1 c WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 1) AND case1.product_type_id = c.product_type_id) Tuesday,
(SELECT COUNT(1) FROM case1 c WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 2) AND case1.product_type_id = c.product_type_id) Wednesday,
(SELECT COUNT(1) FROM case1 c WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 3) AND case1.product_type_id = c.product_type_id) Thursday,
(SELECT COUNT(1) FROM case1 c WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 4) AND case1.product_type_id = c.product_type_id) Friday
FROM case1 WITH(NOLOCK)
INNER JOIN product_type WITH(NOLOCK)
ON case1.product_type_id = product_type.product_type_id
WHERE case1.product_type_id IN(SELECT product_type_id FROM case1 WITH(NOLOCK) WHERE CAST(Date_Created AS DATE) BETWEEN DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) AND DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 4))
GROUP BY case1.product_type_id, descr
ORDER BY (select count(1) from case1 c2 where case1.product_type_id = c.product_type_id and c2.product_type_id = c.product_type_id) desc, descr