Tabelle ProductMst
(Kombination ist einzigartig - CodeNum, MainCatid, SubCatid)sql top 1 basierend auf Gruppe aufzeichnet
id CodeNum MainCatid SubCatid Desc Qty
1 001 1 1 prod1 5
2 001 1 2 prod2 10
3 001 2 3 prod3 1
4 002 2 3 prod4 2
5 003 2 3 prod5 3
6 004 1 1 prod6 4
Sub Tabelle SubCatMst
id name subcode
1 scat1 00
2 scat2 00
3 scat3 02
4 scat4 03
Gewünschtes Ergebnis
id CodeNum MainCatid SubCatid Desc Qty subcode
2 001 1 2 prod2 15 00
3 001 2 3 prod3 1 02
4 002 2 3 prod4 2 02
5 003 2 3 prod5 3 02
6 004 1 1 prod6 4 00
Grundsätzlich , ich wollte subcode
gruppieren, wenn dasselbe und sum (Qty)
basierend auf subcode
. Und nehmen Sie die TOP 1
Datensätze Daten wie alle Spalten basierend auf MAX(Qty)
.
Zusammenfassung: Bitte überprüfen Sie zuerst 2 Datensätze, die kombiniert.
Abfrage Versuch:
select * from (
select A.*, B.subcode,
ROW_NUMBER() OVER(PARTITION BY A.CodeNum, A.MainCatid, B.subcode ORDER BY A.Qty desc) as row
from ProductMst A
inner join SubCatMst B on A.SubCatid=B.id
) as A where row<=1
Zeigen Sie uns Ihren aktuellen Abfrageversuch. – jarlh
@jarlh hat meinen Code bearbeitet. –