这是可能的,我有如下结果,想要填充或组合每个id
Num id RMA
item1 1 0
item1 1 0
item1 1 0
item1 8 1
item1 8 1
item1 8 1
item1 1 0
item1 1 0
item1 1 0
item2 8 1
item2 8 1
item2 8 1
item2 8 1
item2 8 1
item2 8 1
item2 8 1
item2 1 0
我想得到如下结果
item1 id1=3,id8=3,id1=3
item2 id8=7,id1=1
答案 0 :(得分:1)
如果是SQL服务器:
您可以使用GROUP BY
子句:
SELECT Num
,id
,COUNT(RMA) AS Count_RMA
FROM Table
GROUP BY Num
,id
它会输出一个表格,该表格会计算每个唯一RMA
和Num
组合的每个id
。
像这样:
Num id Count_RMA
item1 1 6
item1 8 3
item2 8 7
item2 1 1
答案 1 :(得分:0)
它看起来像这样:
select t.*,
stuff( (select ',' + cast(id as varchar(max)) + '=' + cast(cnt as varchar(max))
from t tt
where t.num = tt.num
group by id
for xml path ('')
), 1, 1, ''
) as
from (select num from t) t
答案 2 :(得分:0)
但是,您需要创建一些额外的步骤并不困难,请参阅下面的代码:
DECLARE @items TABLE (Num varchar(10) , id INT, RMA int , _group int default(0) )
declare @gIndcator int = 0 , @old_id int = 0
insert @items (num,id,rma) select 'item1' , 1 , 0
insert @items (num,id,rma) select 'item1' , 1 , 0
insert @items (num,id,rma) select 'item1' , 1 , 0
insert @items (num,id,rma) select 'item1' , 8 , 1
insert @items (num,id,rma) select 'item1' , 8 , 1
insert @items (num,id,rma) select 'item1' , 8 , 1
insert @items (num,id,rma) select 'item1' , 1 , 0
insert @items (num,id,rma) select 'item1' , 1 , 0
insert @items (num,id,rma) select 'item1' , 1 , 0
insert @items (num,id,rma) select 'item2' , 8 , 1
insert @items (num,id,rma) select 'item2' , 8 , 1
insert @items (num,id,rma) select 'item2' , 8 , 1
insert @items (num,id,rma) select 'item2' , 8 , 1
insert @items (num,id,rma) select 'item2' , 8 , 1
insert @items (num,id,rma) select 'item2' , 8 , 1
insert @items (num,id,rma) select 'item2' , 8 , 1
insert @items (num,id,rma) select 'item2' , 1 , 0
UPDATE @items
SET @gIndcator = _group = case when @old_id <> id then @gIndcator + 1 else @gIndcator end
, @old_id = id
select * from @items
select distinct
num
, ids = stuff((
select ',id'+convert(varchar(12),b.id) + '=' +convert(varchar(12),b._count)
from (
SELECT num, id , _group, COUNT(1) _count
FROM @items c
where a.Num = c.Num
group by Num, id,_group) b
where a.Num = b.Num
order by b.Num,_group
for xml path (''), type).value('.','nvarchar(max)')
,1,1,'')
from @items a
item1 id1 = 3,id8 = 3,id1 = 3
item2 id8 = 7,id1 = 1
答案 3 :(得分:0)
;WITH CTE(Num , id, RMA)
AS
(
SELECT 'item1', 1 , 0 UNION ALL
SELECT 'item1', 1 , 0 UNION ALL
SELECT 'item1', 1 , 0 UNION ALL
SELECT 'item1', 8 , 1 UNION ALL
SELECT 'item1', 8 , 1 UNION ALL
SELECT 'item1', 8 , 1 UNION ALL
SELECT 'item1', 1 , 0 UNION ALL
SELECT 'item1', 1 , 0 UNION ALL
SELECT 'item1', 1 , 0 UNION ALL
SELECT 'item2', 8 , 1 UNION ALL
SELECT 'item2', 8 , 1 UNION ALL
SELECT 'item2', 8 , 1 UNION ALL
SELECT 'item2', 8 , 1 UNION ALL
SELECT 'item2', 8 , 1 UNION ALL
SELECT 'item2', 8 , 1 UNION ALL
SELECT 'item2', 8 , 1 UNION ALL
SELECT 'item2', 1 , 0
)
,Final
AS
(
SELECT Num,idval,cnt AS COUNT_RMA From
(
SELECT NUM,' id'+CASt(id AS Varchar(2))AS idval,cnt, ROW_NUMBER()OVEr(Partition by NUm,Cnt order by Num)AS Seq
From
(
SELECT NUm,id,COUNT(Id)Over(Partition by Num ,id order by Num)Cnt from CTE
)Dt
)Fnl
where Fnl.Seq=1
)
SELECT DISTINCT num, STUFF((SELECT DISTINCT ', '+CAST(''+CAST(idval AS VARCHAR)+'='+CAST(COUNT_RMA AS VARCHAR) AS VARCHAR(100))
From Final I WHERE O.Num=I.Num foR XML PATH('')),1,1,'') As Requiredvalue
from Final O