簡體   English   中英

被代碼串聯 + row_number SQL 代碼困住了

[英]Stuck with code concatenation + row_number SQL code

這是我的數據的樣子:

id  type    row_number
---------------------------
1a  a         1
1a  a         2
1a  b         3
1a  b         4
1a  c         5
1a  b         6
2a  b         1
2a  b         2
2a  c         3
2a  a         4
2a  c   

我想要如下所述的新變量 new_type,

id  new_type    
-------------
1a  a_b_c   
2a  b_c_a   

例如:ID = 1a,購買次數 = 1 然后 new_type = type 它將添加另一種類型,如 concat(購買次數對於下一種類型無關緊要)。

試過這個代碼“

select id,
stuff((case when t.seqnum = '1' then '_' + type else '' end) +
              (case when t.seqnum = '2' then '_' + type else '' end) +
              (case when t.seqnum = '3' then '_' + type else '' end),
              1, 1, '') as new_type
from (select t.id, t.type , min(t.rowno) as min_tp, row_number() over (partition by t.id order by min(t.rowno)) as seqnum
      from try2 as t
      group by t.id, t.type 
     ) t
group by id;

但這給了我一個錯誤:

SQL 錯誤 [8120] [S0001]:選擇列表中的列“t.seqnum”無效,因為它既不包含在聚合函數中,也不包含在 GROUP BY 子句中。

如果你只有幾種類型,你可以這樣做:

select id,
       stuff( max(case when seqnum = 1 then '_' + type else '' end) +
              max(case when seqnum = 2 then '_' + type else '' end) +
              max(case when seqnum = 3 then '_' + type else '' end),
              1, 1, ''
            ) as new_type
from (select t.id, t.type, min(t.times_purchased) as min_tp,
             row_number() over (partition by t.id order by min(t.times_purchased)) as seqnum
      from t
      group by t.id, t.type
     ) t
group by id;

每種類型都需要一個單獨的case

請注意,在這里使用string_agg()很誘人,但無法控制結果字符串的排序。

這適用於一些假設; 如果我的假設是錯誤的,那么由您來修復它們,因為我們仍在等待您的嘗試等。

您使用的是不受支持的 SQL Server 版本這一事實也並不容易,但無論如何,這似乎有效:

WITH YourTable AS(
    SELECT *
    FROM (VALUES('1a','a',1),
                ('1a','a',2),
                ('1a','b',3),
                ('1a','b',4),
                ('1a','c',5),
                ('1a','b',6),
                ('2a','b',1),
                ('2a','b',2),
                ('2a','c',3),
                ('2a','a',4),
                ('2a','c',NULL))V(id, [type], [row_number]))
SELECT YT.id,
       STUFF((SELECT '_' + sq.[type]
              FROM (SELECT T.id,
                           T.[type],
                           T.[row_number],
                           ROW_NUMBER() OVER (PARTITION BY T.id, t.[type] ORDER BY t.[row_number]) AS RN
                    FROM YourTable T
                    WHERE T.id = YT.id
                      AND t.[row_number] IS NOT NULL) sq
              WHERE sq.RN = 1
              ORDER BY sq.[row_number]
              FOR XML PATH(''),TYPE).value('.','varchar(MAX)'),1,1,'') AS new_type
FROM YourTable YT
GROUP BY YT.id;

數據庫<>小提琴

我更喜歡使用 string_agg 以免對要連接的字符串數量做出假設。

select id,
string_agg(type, "_") within group (order by seqnum asc) as new_type
from
(
select t.id, t.type, min(t.times_purchased) as min_tp,
 row_number() over (partition by t.id order by min(t. times_purchased)) as seqnum
from table1 t
group by t.id, t.type
) as t
group by id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM