【问题标题】:SQL Server - Select with Group By together Raw_NumberSQL Server - 与 Group By 一起选择 Raw_Number
【发布时间】:2021-10-27 14:53:54
【问题描述】:

我正在使用 SQL Server 2000 (80)。因此,不能使用 LAG 功能。

我有一个四列数据集的代码:

Purchase_Date

Facility_no

Seller_id

Sale_id

我需要识别缺失的 Sale_ids。所以每个 sale_id 都是 100% 连续的,所以顺序上不应该有任何间隙。

此代码适用于特定日期和存储(如果指定)。但我需要处理整个数据集循环遍历每个设施 ID 和每个卖家 ID 的购买日期

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)

我的数据集

【问题讨论】:

    标签: tsql select grouping window-functions row-number


    【解决方案1】:

    本栏目:

    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”,并加入一个计数表on not exists

    编辑:OP 在 cmets 中提到他们不能使用 LAG()。那么,我的建议是:

    • 创建一个临时表,其中包含按设施/卖方 ID 的 max(sale_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.
    

    【讨论】:

    • 嗨@George,我无法使用LAG 功能,我的数据库版本与SQL Server 2000 (80) 兼容。我已经更新了我的问题中的代码。它让我接近我需要的东西。你能看看吗?
    • 在你加入你的N.num是什么意思?
    • N 是一个计数表。一个计数表只有从 1 到假设 1000 的数字。您需要它来创建每个可能的销售 id,然后通过不存在删除现有的,然后得到缺少的。
    • 我有点迷路了。我已经根据 Facility_id、Date、Seller_id 来自 source_table 组的 max(sale_id) 生成了包含每个可能的 sale_id 的临时表(称为 tally)。然后我根据 t.maxsale 上的计数 N 将它内部连接到源
    • 所以我添加了一个新代码来生成数据集,实际 max(sale_id) 按 Facility_id、Date、Seller_id 分组,第二个数据集生成每个可能的销售 id。但它无法生成第二个数据集,并出现错误 GROUP BY、HAVING 或在递归公用表表达式“All_PossibleSale_ids”的递归部分中不允许聚合函数。
    【解决方案2】:

    这个成功了

    ; 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
    

    【讨论】:

      猜你喜欢
      • 2011-12-10
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 1970-01-01
      • 2016-01-14
      相关资源
      最近更新 更多