2015-08-25 19 views
-3
============================= 
Itemnumber| Check_ind| year 
============================= 
123  |Y   | 2011 
456  |Y   | 2011 
123  |Y   | 2012 
456  |Y   | 2011 
456  |Y   | 2011 

我想結果爲SQL結果到3桶由數

===================== 
1| 2-3| 4 
===================== 
123| 456 | 

我想指望每個itemnumber出現在表以及年= 2011,然後把它放進水桶總時間。我的itnitial認爲是這樣的:

SELECT case when count(Itemnumber)>=0 and <=1 then '1' 
    case when count(Itemnumber)>=2 and <=3 then '2-3' 
    else '4' end 
    from table where year = '2011' 
+0

你會如何希望得到的結果一樣,如果,兩個123和456時僅一次? –

+0

您如何期待返回出現0次的ItemNumbers?另外,你想要在哪一列出現3?都? – APH

+0

@ChorWaiChun我認爲結果是正確的,因爲123只在表格中顯示一次,並且年份= 2011,所以它屬於0-1桶 – love233126

回答

0

我覺得這是你所需要的 -

DECLARE @T TABLE (ItemNumber VARCHAR(5) 
        ,Check_Ind CHAR(1) 
        ,YEAR varchar(4) 
       ) 

INSERT INTO @T VALUES ('123','Y','2011') 
        ,('456','Y','2011') 
        ,('123','Y','2012') 
        ,('456','Y','2011') 
        ,('456','Y','2011') 

SELECT * FROM @t 


SELECT CASE WHEN Count(ItemNumber) < 2 THEN ItemNumber ELSE '' END [0-1] 
    ,CASE WHEN Count(ItemNumber) BETWEEN 2 AND 3 THEN ItemNumber ELSE '' END [2-3] 
    ,CASE WHEN Count(ItemNumber) > 3 THEN ItemNumber ELSE '' END [4] 
    FROM @T 
    WHERE YEAR = '2011' 
    GROUP BY ItemNumber 
+0

我喜歡你的解決方案:在這個小提琴http://sqlfiddle.com/#!6/708df/2工作,但有很多的空值。 –

+0

@JuanCarlosOropeza - 我剛剛編輯了我的答案,用空白替換空值。 –

+0

問題不在於空值。 OP請求同一行中的多個數據。但看起來不太在乎。 –

1

我的猜測是也許有更好的解決方案,使用數據透視。

雖然有人發現,這裏是我的解決方案:

  • 你可以處理空與case顯示space如果這是一個問題。

Sql Fiddle Demo

  • 我包括樣品中多了一些數據,讓我知道這是確定的。
  • 必須使用FULL JOIN,因爲我不知道哪個組會擁有最多的項目。

with item_count AS (
     SELECT itemnumber, count(*) as total 
     FROM item 
     WHERE year = '2011' 
     GROUP BY itemnumber  
), t_01 AS (
     SELECT itemnumber, ROW_NUMBER() OVER(ORDER BY itemnumber) AS row_id 
     FROM item_count 
     WHERE total between 0 and 1 
), t_02 AS (
     SELECT itemnumber, ROW_NUMBER() OVER(ORDER BY itemnumber) AS row_id 
     FROM item_count 
     WHERE total between 2 and 3 
), t_03 AS (
     SELECT itemnumber, ROW_NUMBER() OVER(ORDER BY itemnumber) AS row_id 
     FROM item_count 
     WHERE total = 4 
) 
SELECT t_01.itemnumber as '0-1', t_02.itemnumber as '2-3', t_03.itemnumber as '4' 
from 
    t_01 
    full join t_02 
     on t_01.row_id = t_02.row_id 
    full join t_03 
     on t_01.row_id = t_03.row_id 

enter image description here
我加入項目789999的數據樣本

+0

謝謝!我期待着看看解決方案是否有更簡單的方法=) – love233126

0

這個問題是沒有明確定義,所以我有點作出猜測這裏。注意我也在以消耗品格式發佈ddl和樣本數據。這使得想要幫助的人變得更容易。

create table #Something 
(
    Itemnumber int, 
    Check_ind char(1), 
    MyYear int 
) 

insert #Something 
select 123, 'Y', 2011 union all 
select 456, 'Y', 2011 union all 
select 123, 'Y', 2012 union all 
select 456, 'Y', 2011 union all 
select 456, 'Y', 2011 

--Now add another group for the "2-3" bucket 
insert #Something 
select 12, 'Y', 2011 union all 
select 12, 'Y', 2011; 

with GroupSubtotals as 
(
    select case when COUNT(ItemNumber) < 2 then 1 end as [0-1] 
     , case when COUNT(ItemNumber) > 1 and COUNT(ItemNumber) < 4 then 1 end as [2-3] 
     , case when COUNT(ItemNumber) > 3 then 1 end as [4] 
    from #Something s 
    where s.MyYear = 2011 
    group by ItemNumber 
) 

select SUM([0-1]) as [0-1] 
    , SUM([2-3]) as [2-3] 
    , SUM([4]) as [4] 
from GroupSubtotals 
+0

OP結果中的項目「name」是什麼,而不是「count」。 –

+0

我們不知道,因爲他們從未澄清過。我們都在做假設,因爲這個問題太模糊。 –

+0

我知道這個問題不是太好。但是表格輸出'我想要結果'顯示項目編號非常清楚。 –