我不得不選擇從SQL Sever的表中多個日期範圍即數據選擇數據範圍爲指定的列的SQL Server
1990-1994, 1992-1996, 1994-1998, 1996-2000, 1998-2002, 2000-2004,
2002-2006, 2004-2008, 2006-2010, 2008-2012, 2010-2014
我已經使用這個查詢來獲取無DATE範圍數據即
SELECT
aid, research_area_category_id,
CAST(research_area as VARCHAR(100)) [research_area],
COUNT(*) [Counting]
FROM
sub_aminer_paper
GROUP BY
CAST(research_area as VARCHAR(100)), aid, research_area_category_id
HAVING
aid = 12403
ORDER BY
Counting DESC
這給出輸出作爲圖像即
現在對於使用WHERE
子句的每個DATE範圍,我必須在DATE範圍的相應列中顯示數據。雖然我已經使用這個查詢即
SELECT
aid, research_area_category_id,
[research_area] = CAST(research_area as VARCHAR(100)),
[Counting] = COUNT(*),
[1990 - 1994] = SUM(CASE WHEN p_year BETWEEN 1990 AND 1994 THEN 1 ELSE 0 END),
[1992 - 1996] = SUM(CASE WHEN p_year BETWEEN 1992 AND 1996 THEN 1 ELSE 0 END),
[1994 - 1998] = SUM(CASE WHEN p_year BETWEEN 1994 AND 1998 THEN 1 ELSE 0 END),
[1996 - 2000] = SUM(CASE WHEN p_year BETWEEN 1996 AND 2000 THEN 1 ELSE 0 END),
[1998 - 2002] = SUM(CASE WHEN p_year BETWEEN 1998 AND 2002 THEN 1 ELSE 0 END),
[2000 - 2004] = SUM(CASE WHEN p_year BETWEEN 2000 AND 2004 THEN 1 ELSE 0 END),
[2002 - 2006] = SUM(CASE WHEN p_year BETWEEN 2002 AND 2006 THEN 1 ELSE 0 END),
[2004 - 2008] = SUM(CASE WHEN p_year BETWEEN 2004 AND 2008 THEN 1 ELSE 0 END),
[2006 - 2010] = SUM(CASE WHEN p_year BETWEEN 2006 AND 2010 THEN 1 ELSE 0 END),
[2008 - 2012] = SUM(CASE WHEN p_year BETWEEN 2008 AND 2012 THEN 1 ELSE 0 END),
[2010 - 2014] = SUM(CASE WHEN p_year BETWEEN 2010 AND 2014 THEN 1 ELSE 0 END)
FROM
sub_aminer_paper
WHERE
aid = 2937
AND p_year BETWEEN 1990 AND 2014
GROUP BY
aid, CAST(research_area AS VARCHAR(100)), research_area_category_id
ORDER BY aid ASC, Counting DESC
而這個查詢輸出這樣的:
,但我需要下(1990- 1994年,1992- 1996年,1994- 1998年research_area_category_id
值...這些專欄。例如。在1990 - 1994
柱,它應該顯示各research_area_category_id
即1
,1
和32
代替Counting
即1
,1
和1
,類似地它應該顯示33
代替2
在1998 - 2002
柱,反之亦然。
請幫助和提前致謝。
你想添加您的日期範圍爲列? –
@FelixPamittan絕對是 – maliks
'HAVING'只能用於**集合**,例如'COUNT','MAX','AVG'等等 - 而不是像這樣基本的表達方式 - 使用'WHERE援助= 12403'代替 –