Query vraag. Zoek laatste mutatiedatum per soort.

Status
Niet open voor verdere reacties.

sabrinahorst

Gebruiker
Lid geworden
28 aug 2006
Berichten
50
Hallo

Ik probeer een tijdelijk bestand aan te maken met daarin

Partcode, MutationDate, MovementType, Qty

Per Partcode moet daarin de allerlaatste mutatiedatum per movementtype komen te staan. Er zijn maximaal 9 movementtypes.

Dus wat erin moet komen te staan is bv voor artikel 003307

003307 2018-05-31 1 -100
003307 2018-06-11 2 -33
003307 2018-04-25 3 +25
etc voor elk van de 9 types (indien aanwezig).

Wat ik heb:

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
--order by MOV.MutationDate desc
SELECT * FROM #LMUT where partcode='003007'
drop table #LMUT

Resultaat:
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

Niet wat ik wilde dus.
Wat je ziet nog 8 keer type 2.


En als ik met select distinct werk:
SELECT distinct MOV.Partcode,INV.PMOverInvtType
FROM dbo.T_PartMovementMain as MOV
inner join dbo.T_PartMovementOverInvt as INV on INV.PMMainCode=MOV.PMMainCode
WHERE
MOV.PartMovementType = 1
order by MOV.Partcode,INV.PMOverInvtType

Dan krijg ik geen selectie op mijn max(mutatiedatum)




Hoe krijg ik dit voor elkaar?

Alvast bedankt
Sabrina
 
Laatst bewerkt:
Heb nu dit geprobeerd:

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
order by MOV.Partcode,INV.PMOverInvtType

en dan krijg ik idd de lijst per artikel/type met de laatste mutatiedatum.
Maar hoe krijg ik dan het mutatieaantal van dat record erbij?
 
Opgelost

Via stackoverflow kwam men op dit idee:

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

en het werkt.
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan