SQL结果按计数放入3个存储桶
我希望结果是SQL结果按计数放入3个存储桶,sql,sql-server,Sql,Sql Server,我希望结果是 ============================= Itemnumber| Check_ind| year ============================= 123 |Y | 2011 456 |Y | 2011 123 |Y | 2012 456 |Y | 2011 456 |Y | 2011 我想计算每个itemnumb
=============================
Itemnumber| Check_ind| year
=============================
123 |Y | 2011
456 |Y | 2011
123 |Y | 2012
456 |Y | 2011
456 |Y | 2011
我想计算每个itemnumber出现在表中的总时间,其中year=2011,然后将其放入bucket中。我最初的想法是:
=====================
1| 2-3| 4
=====================
123| 456 |
我的猜测是也许有一个更好的解决方案使用枢轴 当有人发现这一点时,以下是我的解决方案: 如果出现问题,可以使用大小写处理null以显示空格。 我在样本中包含了更多的数据,请告诉我是否可以。 必须使用完全联接,因为我不知道哪个组的项目最多。
我将789和999项添加到数据样本中这个问题没有很好的定义,所以我在这里做了一些猜测。注意,我还以可消费的格式发布ddl和示例数据。这让那些试图帮助别人的人变得容易多了
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
我想这就是你需要的-
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
如果123和456只出现一次,您会期望结果是什么样的?您期望如何返回出现0次的ItemNumber?另外,您希望计数为3的列显示在哪一列?两者都有?@ChorWaiChun我认为结果是正确的,因为123在表中只显示一次,其中年份=2011,所以它属于0-1巴克,但输出是什么???如果你在同一个桶中有两个项目编号,你希望结果集中有什么?我强烈建议你在下一篇文章之前阅读这篇文章。如果您清楚地提供了信息,这对每个人来说都会简单得多,包括您自己。OP结果上的项目名称,而不是计数。我们不知道,因为他们从未澄清过。我们都在做假设,因为问题太模糊了。我知道问题不太好。但我希望表格输出结果能够非常清晰地显示项目编号。是的,当要求清楚时,只返回了模糊信息。谢谢!我在等着看是否有更简单的方法来解决这个问题=我喜欢你的解决方案:在这个小提琴中工作,但有很多空值。@JuanCarlosOropeza-我刚刚编辑了我的答案,用空白代替空值。问题不在于空值。OP请求同一行中的多个数据。但看起来我并不在乎。
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
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