【问题标题】:CREATE TEMP TABLE BASED ON SELECT DISTINCT ON 3 COLUMNS BUT WITH 1 EXTRA COLUMN基于 SELECT DISTINCT ON 3 列但有 1 个额外列创建 TEMP 表
【发布时间】:2018-10-31 10:59:05
【问题描述】:

我需要在其中制作一个临时文件:

Partcode、MutationDate、MovementType、数量

每个零件代码的每个运动类型都有多个突变日期(最多可能有 9 个运动类型) 我需要获取每个部分代码的每个运动类型的最后一个突变日期以及随之而来的数量。

零件代码为 003307 的示例

003307 2018-05-31 1 -100
003307 2018-06-11 2 -33
003307 2018-04-25 3 +25

所有 9 种运动类型以此类推。

到目前为止我得到了什么:

create table #LMUT(
    MutationDate T_Date
    ,PartCode T_Code_Part
    ,CumInvQty T_Quantum_Qty10_3
    ,MovementType T_Type_PMOverInvt
    )

    insert #LMUT(
    MutationDate,
    Partcode,
    CumInvQty,
    MovementType)
    SELECT
    cast (max(MOV.MutationDate) as date)
    ,MOV.PartCode
    ,INV.MutationQty
    ,INV.PMOverInvtType
    FROM dbo.T_PartMovementMain as MOV
    inner join dbo.T_PartMovementOverInvt as INV on

INV.PMMainCode=MOV.PMMainCode
        WHERE
        MOV.PartMovementType = 1
        group by MOV.PartCode,INV.PMOverInvtType,INV.MutationQty,MOV.MutationDate

SELECT * FROM #LMUT where partcode='003007'
drop table #LMUT

结果:

    2016-12-06 00:00:00.000 003007 -24.000 2
    2016-09-29 00:00:00.000 003007 -24.000 2
    2016-11-09 00:00:00.000 003007 -24.000 2
    2016-11-22 00:00:00.000 003007 -24.000 2
    2016-10-26 00:00:00.000 003007 -24.000 2
    2016-09-12 00:00:00.000 003007 -42.000 2
    2016-10-13 00:00:00.000 003007 -24.000 2
    2016-12-03 00:00:00.000 003007 100.000 5
    2017-01-12 00:00:00.000 003007 -48.000 2
    2016-10-04 00:00:00.000 003007 306.000 7

不是我需要的,还有8次类型2

我还尝试了什么:

SELECT distinct MOV.Partcode,INV.PMOverInvtType,mov.MutationDate
        FROM dbo.T_PartMovementMain as MOV
        inner join dbo.T_PartMovementOverInvt as INV on
        INV.PMMainCode=MOV.PMMainCode
        WHERE
        mov.MutationDate = (SELECT MAX (c.MutationDate) FROM
        dbo.T_PartMovementMain as c
        inner join dbo.T_PartMovementOverInvt as d on D.PMMainCode=c.PMMainCode
                             WHERE
                                     C.PartMovementType = 1 AND
                                     C.PartCode=mov.PartCode AND
                                     D.PMMainCode = C.PMMainCode AND
                                     D.PMOverInvtType=inv.PMOverInvtType                                     
                                    )        
         and MOV.PartMovementType = 1 and mov.partcode='003007'
        order by MOV.Partcode,INV.PMOverInvtType

结果:

3007    2   2017-01-12 00:00:00.000
3007    5   2016-12-03 00:00:00.000
3007    7   2016-10-04 00:00:00.000

这就是我想要的,但我也需要获得数量。

【问题讨论】:

    标签: sql sql-server distinct temp-tables


    【解决方案1】:

    使用row_number()窗口函数

       with cte as
    
    (         SELECT  MOV.*,INV.*,
              row_number() over(partition by INV.PMOverInvtType order by MOV.MutationDate desc)rn
                FROM dbo.T_PartMovementMain as MOV
                inner join dbo.T_PartMovementOverInvt as INV on
                INV.PMMainCode=MOV.PMMainCode
    ) select cte.* from cte where rn=1
    

    【讨论】:

    • 有效!谢谢。我从来没有听说过这个'with'函数和row_number。
    • with cte as ( SELECT MOV.Partcode,INV.PMOverInvtType,mov.MutationDate,INV.MutationQty, row_number() over(partition by MOV.Partcode,INV.PMOverInvtType order by MOV.Partcode,INV .PMOverInvtType,MOV.MutationDate desc) rn FROM dbo.T_PartMovementMain as MOV inner join dbo.T_PartMovementOverInvt as INV on INV.PMMainCode=MOV.PMMainCode ) select cte.* from cte where rn=1 这是我现在使用的代码.
    【解决方案2】:

    这样解决了:

      create table #LMUT(
       PartCode     T_Code_Part
       ,MovementType T_Type_PMOverInvt
       ,MutationDate T_Date 
       ,CumInvQty    T_Quantum_Qty10_3
       )
    
    insert #LMUT(Partcode,MovementType,MutationDate,CumInvQty)
        select Artikel,Type,Datum,Aant
        from (
            SELECT  MOV.Partcode as Artikel,INV.PMOverInvtType as Type,mov.MutationDate as Datum,INV.MutationQty as Aant,
              row_number() over(partition by MOV.Partcode,INV.PMOverInvtType order by MOV.Partcode,INV.PMOverInvtType,MOV.MutationDate desc) rn
                FROM dbo.T_PartMovementMain as MOV
                inner join dbo.T_PartMovementOverInvt as INV on INV.PMMainCode=MOV.PMMainCode) cse
        where rn=1
    select * from #LMUT  order by Partcode  
    drop table #LMUT
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 2021-03-28
      • 2023-03-22
      • 1970-01-01
      • 2023-03-03
      • 2019-07-16
      • 1970-01-01
      相关资源
      最近更新 更多