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