我有这个查询
SELECT semester, COUNT(id) AS total, RIGHT(RTRIM(semester), 4) AS year
FROM TextbookReservation
WHERE (semester IS NOT NULL)
AND (semester <> '')
AND (semester <> 'SM2008')
AND semester <> 'SU2008')
GROUP BY semester ORDER BY year, semester DESC
产生类似这样的东西:
semester total year
SP2006 2277 2006
FA2006 4367 2006
SP2007 2893 2007
FA2007 5624 2007
SP2008 4083 2008
FA2008 6451 2008
我想返回这样的内容:
year totalSpring totalFall
2006 2277 4367
2007 2893 5624
2008 4083 6451
关于如何处理此查询的任何想法?谢谢!
答案 0 :(得分:3)
没有PIVOT表,因为您没有指定版本:
SELECT [year],
sum(case WHEN left(semester,2)='SP' then total ELSE 0 END) as totalSpring ,
sum(case WHEN left(semester,2)='FA' THEN total else 0 end) as totalFall
from (
SELECT semester
, count(id) AS total
, right(RTRIM(semester), 4) AS year
FROM
TextbookReservation
WHERE
semester IS NOT NULL
AND semester <> ''
AND semester <> 'SM2008'
AND semester <> 'SU2008'
GROUP BY
semester
) t
GROUP BY t.[year]
order by t.[year]
答案 1 :(得分:2)
您要做的是创建pivot。版本2005和更高版本的SQL具有内置命令来透视数据。
您能指定数据库使用的SQL Server版本吗?
PS。您也可以使用2005年之前的Case语句执行此操作。由于您的示例仅有两个学期的离散值,我将使用该方法提供答案。
Select year, SUM(TotalSpring), SUM(TotalFall) From
(select RIGHT(RTRIM(semester), 4) AS year, case LEFT(semester,2) when 'SP' then 1 else 0 end TotalSpring, case LEFT(semester,2) when 'FA' then 1 else 0 end TotalFall
from TextbookReservation
WHERE (semester IS NOT NULL)
AND (semester <> '')
AND (semester <> 'SM2008')
AND semester <> 'SU2008') as t1
group by year
order by year
答案 2 :(得分:0)
其他答案的简化版本:
SELECT
RIGHT(RTRIM(semester), 4) year,
COUNT(CASE WHEN left(semester,2)='SP' then id else null end) totalSpring,
COUNT(CASE WHEN left(semester,2)='FA' then id else null end) totalFall
FROM TextbookReservation
WHERE (semester IS NOT NULL)
AND (semester <> '')
AND (semester <> 'SM2008')
AND (semester <> 'SU2008')
GROUP BY RIGHT(RTRIM(semester), 4)
ORDER BY RIGHT(RTRIM(semester), 4) DESC
这里是测试脚本:
CREATE TABLE #TextbookReservation
(
semester VARCHAR(6),
id INT
)
INSERT INTO #TextbookReservation VALUES ('SP2006', 1)
INSERT INTO #TextbookReservation VALUES ('SP2006', 2)
INSERT INTO #TextbookReservation VALUES ('SP2006', 3)
INSERT INTO #TextbookReservation VALUES ('SP2006', 4)
INSERT INTO #TextbookReservation VALUES ('SP2006', 5)
INSERT INTO #TextbookReservation VALUES ('FA2006', 1)
INSERT INTO #TextbookReservation VALUES ('FA2006', 2)
INSERT INTO #TextbookReservation VALUES ('FA2006', 3)
INSERT INTO #TextbookReservation VALUES ('SP2007', 1)
INSERT INTO #TextbookReservation VALUES ('SP2007', 2)
INSERT INTO #TextbookReservation VALUES ('FA2007', 1)
INSERT INTO #TextbookReservation VALUES ('FA2007', 2)
INSERT INTO #TextbookReservation VALUES ('FA2007', 3)
INSERT INTO #TextbookReservation VALUES ('FA2007', 4)
INSERT INTO #TextbookReservation VALUES ('FA2007', 5)
SELECT
RIGHT(RTRIM(semester), 4) year,
COUNT(CASE WHEN left(semester,2)='SP' then id else null end) totalSpring,
COUNT(CASE WHEN left(semester,2)='FA' then id else null end) totalFall
FROM #TextbookReservation
WHERE (semester IS NOT NULL)
AND (semester <> '')
AND (semester <> 'SM2008')
AND (semester <> 'SU2008')
GROUP BY RIGHT(RTRIM(semester), 4)
ORDER BY RIGHT(RTRIM(semester), 4) DESC
DROP TABLE #TextbookReservation