2015-09-15 65 views
2

我不得不選擇從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 

這給出輸出作爲圖像即

enter image description here

現在對於使用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 

而這個查詢輸出這樣的:

enter image description here

,但我需要下(1990- 1994年,1992- 1996年,1994- 1998年research_area_category_id值...這些專欄。例如。在1990 - 1994柱,它應該顯示各research_area_category_id1132代替Counting111,類似地它應該顯示33代替21998 - 2002柱,反之亦然。

請幫助和提前致謝。

+0

你想添加您的日期範圍爲列? –

+0

@FelixPamittan絕對是 – maliks

+1

'HAVING'只能用於**集合**,例如'COUNT','MAX','AVG'等等 - 而不是像這樣基本的表達方式 - 使用'WHERE援助= 12403'代替 –

回答

1

標籤Alleman已經提到在評論中這裏最好的辦法,但我要厚臉皮,並將其添加作爲一個答案。

你是清楚的,你想在你的轉動日期列顯示來自research_area_category_id列值。因此,第一步這裏是讓research_area_category_id每個CASE語句的輸出,而不是一個整數1

CASE WHEN p_year BETWEEN 1990 AND 1994 THEN research_area_category_id ELSE 0 END 

如果你只是這種變化跑出你的代碼,你會發現SUM功能使輸出爲倍數值爲research_area_category_id。例如,1998 - 2002的第一行將具有值66(兩倍33)。

所以這告訴我們,你不希望使用SUM功能了。但是,您仍然希望在具有不同的p_year值的所有行上聚合(分組)數據,因此您必須改用某種類型的聚合函數。如果你沒有,SQL Server會拋出一個錯誤,因爲你沒有按p_year分組。

最簡單的集合函數在這種情況下使用的是MAX,其從組行取最高值被分組成一個。 official documentation有一些簡單的例子。

這隻適用於您的情況,前提是research_area_category_id的所有值都是正數(大於CASE語句默認設置的0),它們似乎是。

結合變向CASE語句從SUMMAX的變化給你的查詢以下版本:

SELECT 
aid, research_area_category_id, 
[research_area] = CAST(research_area as VARCHAR(100)), 
[Counting] = COUNT(*), 
[1990 - 1994] = MAX(CASE WHEN p_year BETWEEN 1990 AND 1994 THEN research_area_category_id ELSE 0 END), 
[1992 - 1996] = MAX(CASE WHEN p_year BETWEEN 1992 AND 1996 THEN research_area_category_id ELSE 0 END), 
[1994 - 1998] = MAX(CASE WHEN p_year BETWEEN 1994 AND 1998 THEN research_area_category_id ELSE 0 END), 
[1996 - 2000] = MAX(CASE WHEN p_year BETWEEN 1996 AND 2000 THEN research_area_category_id ELSE 0 END), 
[1998 - 2002] = MAX(CASE WHEN p_year BETWEEN 1998 AND 2002 THEN research_area_category_id ELSE 0 END), 
[2000 - 2004] = MAX(CASE WHEN p_year BETWEEN 2000 AND 2004 THEN research_area_category_id ELSE 0 END), 
[2002 - 2006] = MAX(CASE WHEN p_year BETWEEN 2002 AND 2006 THEN research_area_category_id ELSE 0 END), 
[2004 - 2008] = MAX(CASE WHEN p_year BETWEEN 2004 AND 2008 THEN research_area_category_id ELSE 0 END), 
[2006 - 2010] = MAX(CASE WHEN p_year BETWEEN 2006 AND 2010 THEN research_area_category_id ELSE 0 END), 
[2008 - 2012] = MAX(CASE WHEN p_year BETWEEN 2008 AND 2012 THEN research_area_category_id ELSE 0 END), 
[2010 - 2014] = MAX(CASE WHEN p_year BETWEEN 2010 AND 2014 THEN research_area_category_id 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 

如果你有興趣,我嘲笑了像你這樣的數據的幾行在this SQL fiddle之前在回答之前測試此查詢。 (我猜在p_year值,但他們證明的原則,除非我誤解你的要求。)

+0

順便說一句,你可能要探討[PIVOT(https://technet.microsoft.com/en-us/library/ms177410(V = SQL.105)的.aspx)在Transact-SQL的功能,因爲這是另一種以您所追求的風格顯示結果的方法。不管是好還是壞,都取決於你的用例。 –

+0

@ philosophicles - 非常感謝,它的所有功能都改變了SUM到MAX,並且在THEN語句中需要列替換 – maliks