SQL 服务器 - Select 与 Group By 一起 Raw_Number
SQL Server - Select with Group By together Raw_Number
我正在使用 SQL Server 2000 (80)。所以,无法使用LAG功能。
我有一个包含四列的数据集的代码:
Purchase_Date
Facility_no
Seller_id
Sale_id
我需要确认失踪 Sale_ids。所以每个 sale_id 都是 100% 连续的,所以顺序不应该有任何间隙。
如果指定,此代码适用于特定日期和商店。但是我需要处理整个数据集,循环遍历每个 facility_id 和每个 seller_id 永远 purchase_date
declare @MAXCOUNT int
set @MAXCOUNT =
(
select MAX(Sale_Id)
from #table
where
Facility_no in (124) and
Purchase_date = '2/7/2020'
and Seller_id = 1
)
;WITH TRX_COUNT AS
(
SELECT 1 AS Number
union all
select Number + 1 from TRX_COUNT
where Number < @MAXCOUNT
)
select * from TRX_COUNT
where
Number NOT IN
(
select Sale_Id
from #table
where
Facility_no in (124)
and Purchase_Date = '2/7/2020'
and seller_id = 1
)
order by Number
OPTION (maxrecursion 0)
我的数据集
本栏目:
case when
Sale_Id=0 or 1=Sale_Id-LAG(Sale_Id) over (partition by Facility_no, Purchase_Date, Seller_id)
then 'OK' else 'Previous Missing' end
会告诉您哪些 Seller_Ids 缺少一些促销活动。如果你想更进一步并得到你想要的输出,那么过滤掉并区分 'Previous Missing' 个,然后加入一个计数 table on not exists
.
编辑:OP 在评论中提到他们不能使用 LAG()。那么我的建议是:
- 制作一个温度 table 具有最大 (sale_id) 组 facility/seller_id
- 然后你可以通过这个伪代码查询得到你丢失的结果:
Select ...
from temptable t
inner join tally N on t.maxsale <=N.num
where not exists( select ... from sourcetable s where s.facility=t.facility and s.seller=t.seller and s.sale=N.num)
> because the only way to "construct" nonexisting combinations is to construct them all and just remove the existing ones.
这个成功了
; WITH cte_Rn AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY Facility_no, Purchase_Date, Seller_id ORDER BY Purchase_Date) AS [Rn_Num]
FROM (
SELECT
Facility_no,
Purchase_Date,
Seller_id,
Sale_id
FROM MyTable WITH (NOLOCK)
) a
)
, cte_Rn_0 as (
SELECT
Facility_no,
Purchase_Date,
Seller_id,
Sale_id,
-- [Rn_Num] AS 'Skipped Sale'
-- , case when Sale_id = 0 Then [Rn_Num] - 1 Else [Rn_Num] End AS 'Skipped Sale for 0'
, [Rn_Num] - 1 AS 'Skipped Sale for 0'
FROM cte_Rn a
)
SELECT
Facility_no,
Purchase_Date,
Seller_id,
Sale_id,
-- [Skipped Sale],
[Skipped Sale for 0]
FROM cte_Rn_0 a
WHERE NOT EXISTS
(
select * from cte_Rn_0 b
where b.Sale_id = a.[Skipped Sale for 0]
and a.Facility_no = b.Facility_no
and a.Purchase_Date = b.Purchase_Date
and a.Seller_id = b.Seller_id
)
--ORDER BY Purchase_Date ASC
我正在使用 SQL Server 2000 (80)。所以,无法使用LAG功能。
我有一个包含四列的数据集的代码:
Purchase_Date
Facility_no
Seller_id
Sale_id
我需要确认失踪 Sale_ids。所以每个 sale_id 都是 100% 连续的,所以顺序不应该有任何间隙。
如果指定,此代码适用于特定日期和商店。但是我需要处理整个数据集,循环遍历每个 facility_id 和每个 seller_id 永远 purchase_date
declare @MAXCOUNT int
set @MAXCOUNT =
(
select MAX(Sale_Id)
from #table
where
Facility_no in (124) and
Purchase_date = '2/7/2020'
and Seller_id = 1
)
;WITH TRX_COUNT AS
(
SELECT 1 AS Number
union all
select Number + 1 from TRX_COUNT
where Number < @MAXCOUNT
)
select * from TRX_COUNT
where
Number NOT IN
(
select Sale_Id
from #table
where
Facility_no in (124)
and Purchase_Date = '2/7/2020'
and seller_id = 1
)
order by Number
OPTION (maxrecursion 0)
我的数据集
本栏目:
case when
Sale_Id=0 or 1=Sale_Id-LAG(Sale_Id) over (partition by Facility_no, Purchase_Date, Seller_id)
then 'OK' else 'Previous Missing' end
会告诉您哪些 Seller_Ids 缺少一些促销活动。如果你想更进一步并得到你想要的输出,那么过滤掉并区分 'Previous Missing' 个,然后加入一个计数 table on not exists
.
编辑:OP 在评论中提到他们不能使用 LAG()。那么我的建议是:
- 制作一个温度 table 具有最大 (sale_id) 组 facility/seller_id
- 然后你可以通过这个伪代码查询得到你丢失的结果:
Select ...
from temptable t
inner join tally N on t.maxsale <=N.num
where not exists( select ... from sourcetable s where s.facility=t.facility and s.seller=t.seller and s.sale=N.num)
> because the only way to "construct" nonexisting combinations is to construct them all and just remove the existing ones.
这个成功了
; WITH cte_Rn AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY Facility_no, Purchase_Date, Seller_id ORDER BY Purchase_Date) AS [Rn_Num]
FROM (
SELECT
Facility_no,
Purchase_Date,
Seller_id,
Sale_id
FROM MyTable WITH (NOLOCK)
) a
)
, cte_Rn_0 as (
SELECT
Facility_no,
Purchase_Date,
Seller_id,
Sale_id,
-- [Rn_Num] AS 'Skipped Sale'
-- , case when Sale_id = 0 Then [Rn_Num] - 1 Else [Rn_Num] End AS 'Skipped Sale for 0'
, [Rn_Num] - 1 AS 'Skipped Sale for 0'
FROM cte_Rn a
)
SELECT
Facility_no,
Purchase_Date,
Seller_id,
Sale_id,
-- [Skipped Sale],
[Skipped Sale for 0]
FROM cte_Rn_0 a
WHERE NOT EXISTS
(
select * from cte_Rn_0 b
where b.Sale_id = a.[Skipped Sale for 0]
and a.Facility_no = b.Facility_no
and a.Purchase_Date = b.Purchase_Date
and a.Seller_id = b.Seller_id
)
--ORDER BY Purchase_Date ASC