如何针对合并的两个 select 语句创建顺序编号?
How to create sequential numbering against two select statements that are unioned?
我有一个 SQL 查询,它只是两个 SELECT
语句之间的联合。假设整个查询的结果产生 10 条记录。作为该结果集的一部分,我想要一个数字和顺序列(如主键 - 0、1、2、3 等)。我的问题是我的编码方式,正如您在下面看到的,不会产生唯一值(0 值重复两次,1 值重复两次,正如预期的那样,因为 union 语句)。如何让下面的 "Transaction Sequence Number" 显示 1,2,3,4,5,6,7,8,9,10?而不是 1,2,3,4,5,1,2,3,4,5?
这是我的代码(如下)。我将很快发布我的结果集的屏幕截图 - SQL 服务器现在对我来说非常慢 - 我希望没有结果集的屏幕截图这是有意义的:
select top 5
'ACCT PROMORESP' as 'Transaction Identifier',
row_number() over (order by s.HL_ACCT_ID) as 'Transaction Sequence Number',
s.HL_ACCT_ID as 'HL_Acct_ID',
null as [AcctNumber],
null as [AcctType],
null as [AcctSource],
null as [DUNS_NBR],
null as [DBPLCR_CONTACT_ID],
s.CAMPAIGNCODE as [CellCode],
'-1' as [OfferCode],
case
when c.EventDate is not null then 'Click'
when c.EventDate is null then
case
when sub.status = 'unsubscribed' then 'Unsubscribe'
when sub.status = 'bounced' then 'Bounce'
when sub.status = 'held' then 'Bounce'
end
end as [ResponseType],
convert(varchar, c.EventDate, 112) as [ResponseDate],
null as [ResponseQuantity],
null as [ResponseValue],
'Email' as [ResponseChannel],
null as [Cookie ID],
null as [IP address],
null as [Device ID],
null as [CUSTOM_TEXT_01],
null as [CUSTOM_TEXT_02],
null as [CUSTOM_TEXT_03],
null as [CUSTOM_TEXT_04],
null as [CUSTOM_TEXT_05]
from cXXXXXXX.sendlog s with (nolock)
inner join cXXXXXXX._subscribers sub with (nolock) on sub.SubscriberKey = s.Email
left join cXXXXXXX._click c with (nolock) on c.JobID = s.JobID and c.SubscriberKey = s.Email
where c.EventDate is not null or (c.EventDate is null and (sub.status = 'unsubscribed' or sub.status = 'bounced' or sub.status = 'held')) and c.isunique = 1
union all
select top 5
'ACCT PROMORESP' as 'Transaction Identifier',
(row_number() over (order by s.HL_ACCT_ID)) + 1 as 'Transaction Sequence Number',
s.HL_ACCT_ID as 'HL_Acct_ID',
null as [AcctNumber],
null as [AcctType],
null as [AcctSource],
null as [DUNS_NBR],
null as [DBPLCR_CONTACT_ID],
s.CAMPAIGNCODE as [CellCode],
'-1' as [OfferCode],
case
when o.EventDate is not null then 'Message Open'
when o.EventDate is null then
case
when sub.status = 'unsubscribed' then 'Unsubscribe'
when sub.status = 'bounced' then 'Bounce'
when sub.status = 'held' then 'Bounce'
end
end as [ResponseType],
convert(varchar, o.EventDate, 112) as [ResponseDate],
null as [ResponseQuantity],
null as [ResponseValue],
'Email' as [ResponseChannel],
null as [Cookie ID],
null as [IP address],
null as [Device ID],
null as [CUSTOM_TEXT_01],
null as [CUSTOM_TEXT_02],
null as [CUSTOM_TEXT_03],
null as [CUSTOM_TEXT_04],
null as [CUSTOM_TEXT_05]
from cXXXXXXX.sendlog s with (nolock)
inner join cXXXXXXX._subscribers sub with (nolock) on sub.SubscriberKey = s.Email
left join cXXXXXXX._open o with (nolock) on o.JobID = s.JobID and o.SubscriberKey = s.Email
where o.EventDate is not null or (o.EventDate is null and (sub.status = 'unsubscribed' or sub.status = 'bounced' or sub.status = 'held')) and o.isunique = 1
您可以将 sql 包含在一个普通的 table 表达式中,然后给它们编号:
;WITH CTE AS(your sql...)
SELECT *,
(row_number() over (order by HL_ACCT_ID)) + 1 as 'Transaction Sequence Number'
FROM CTE
您必须确保您在 cte 中具有唯一的列名。此外,如果您将此 sql 语句与其他语句一起使用,请确保添加“;”在它之前或以';'结束前一个语句。
试试这个。
SELECT ROW_NUMBER() OVER(ORDER BY DATA.HL_ACCT_ID),DATA.* FROM
(your query here...)DATA
我有一个 SQL 查询,它只是两个 SELECT
语句之间的联合。假设整个查询的结果产生 10 条记录。作为该结果集的一部分,我想要一个数字和顺序列(如主键 - 0、1、2、3 等)。我的问题是我的编码方式,正如您在下面看到的,不会产生唯一值(0 值重复两次,1 值重复两次,正如预期的那样,因为 union 语句)。如何让下面的 "Transaction Sequence Number" 显示 1,2,3,4,5,6,7,8,9,10?而不是 1,2,3,4,5,1,2,3,4,5?
这是我的代码(如下)。我将很快发布我的结果集的屏幕截图 - SQL 服务器现在对我来说非常慢 - 我希望没有结果集的屏幕截图这是有意义的:
select top 5
'ACCT PROMORESP' as 'Transaction Identifier',
row_number() over (order by s.HL_ACCT_ID) as 'Transaction Sequence Number',
s.HL_ACCT_ID as 'HL_Acct_ID',
null as [AcctNumber],
null as [AcctType],
null as [AcctSource],
null as [DUNS_NBR],
null as [DBPLCR_CONTACT_ID],
s.CAMPAIGNCODE as [CellCode],
'-1' as [OfferCode],
case
when c.EventDate is not null then 'Click'
when c.EventDate is null then
case
when sub.status = 'unsubscribed' then 'Unsubscribe'
when sub.status = 'bounced' then 'Bounce'
when sub.status = 'held' then 'Bounce'
end
end as [ResponseType],
convert(varchar, c.EventDate, 112) as [ResponseDate],
null as [ResponseQuantity],
null as [ResponseValue],
'Email' as [ResponseChannel],
null as [Cookie ID],
null as [IP address],
null as [Device ID],
null as [CUSTOM_TEXT_01],
null as [CUSTOM_TEXT_02],
null as [CUSTOM_TEXT_03],
null as [CUSTOM_TEXT_04],
null as [CUSTOM_TEXT_05]
from cXXXXXXX.sendlog s with (nolock)
inner join cXXXXXXX._subscribers sub with (nolock) on sub.SubscriberKey = s.Email
left join cXXXXXXX._click c with (nolock) on c.JobID = s.JobID and c.SubscriberKey = s.Email
where c.EventDate is not null or (c.EventDate is null and (sub.status = 'unsubscribed' or sub.status = 'bounced' or sub.status = 'held')) and c.isunique = 1
union all
select top 5
'ACCT PROMORESP' as 'Transaction Identifier',
(row_number() over (order by s.HL_ACCT_ID)) + 1 as 'Transaction Sequence Number',
s.HL_ACCT_ID as 'HL_Acct_ID',
null as [AcctNumber],
null as [AcctType],
null as [AcctSource],
null as [DUNS_NBR],
null as [DBPLCR_CONTACT_ID],
s.CAMPAIGNCODE as [CellCode],
'-1' as [OfferCode],
case
when o.EventDate is not null then 'Message Open'
when o.EventDate is null then
case
when sub.status = 'unsubscribed' then 'Unsubscribe'
when sub.status = 'bounced' then 'Bounce'
when sub.status = 'held' then 'Bounce'
end
end as [ResponseType],
convert(varchar, o.EventDate, 112) as [ResponseDate],
null as [ResponseQuantity],
null as [ResponseValue],
'Email' as [ResponseChannel],
null as [Cookie ID],
null as [IP address],
null as [Device ID],
null as [CUSTOM_TEXT_01],
null as [CUSTOM_TEXT_02],
null as [CUSTOM_TEXT_03],
null as [CUSTOM_TEXT_04],
null as [CUSTOM_TEXT_05]
from cXXXXXXX.sendlog s with (nolock)
inner join cXXXXXXX._subscribers sub with (nolock) on sub.SubscriberKey = s.Email
left join cXXXXXXX._open o with (nolock) on o.JobID = s.JobID and o.SubscriberKey = s.Email
where o.EventDate is not null or (o.EventDate is null and (sub.status = 'unsubscribed' or sub.status = 'bounced' or sub.status = 'held')) and o.isunique = 1
您可以将 sql 包含在一个普通的 table 表达式中,然后给它们编号:
;WITH CTE AS(your sql...)
SELECT *,
(row_number() over (order by HL_ACCT_ID)) + 1 as 'Transaction Sequence Number'
FROM CTE
您必须确保您在 cte 中具有唯一的列名。此外,如果您将此 sql 语句与其他语句一起使用,请确保添加“;”在它之前或以';'结束前一个语句。
试试这个。
SELECT ROW_NUMBER() OVER(ORDER BY DATA.HL_ACCT_ID),DATA.* FROM
(your query here...)DATA