13. oktober 2006 - 10:34Der er
8 kommentarer og 2 løsninger
Hjælp til Query
Hejsa,
Jeg har en Query jeg ikke lige kan få til at vise korrekt,
Det jeg har behov for er at få vist de Summerede felter per nummer, lige nu summere den bare totalt op og viser det samme total i alle (sum felterne)...
Nogen som har et bud. ******
SELECT "Denmark$Production BOM Line"."Production BOM No_" AS 'Produktionsstyklistenr', "Denmark$Production BOM Line"."Version Code" AS 'Versionskode', "Denmark$Production BOM Line".No_ AS 'Nummer', "Denmark$Production BOM Line".Description AS 'Beskrivelse', "Denmark$Item"."Base Unit of Measure" AS 'Basisenhed', "Denmark$Production BOM Line"."Unit of Measure Code" AS 'Enhedskode', "Denmark$Production BOM Line"."Quantity per" AS 'Antal pr', "Denmark$Production BOM Line".Quantity AS 'Antal ialt', Sum("Denmark$Prod_ Order Line".Quantity) AS 'Frigivet ordretilgang', Sum("Denmark$Item Ledger Entry".Quantity) AS 'Lagerbeholdning' Sum("Denmark$Purchase Line"."Outstanding Quantity") AS 'Antal på købsordre' FROM "Denmark$Production BOM Line" "Denmark$Production BOM Line", "Denmark$Prod_ Order Line" "Denmark$Prod_ Order Line", "Denmark$Item" "Denmark$Item", "Denmark$Item Ledger Entry" "Denmark$Item Ledger Entry", "Denmark$Purchase Line" "Denmark$Purchase Line"
WHERE "Denmark$Prod_ Order Line"."Item No_" = "Denmark$Item".No_ AND "Denmark$Item Ledger Entry"."Item No_" = "Denmark$Item".No_ AND "Denmark$Purchase Line".No_ = "Denmark$Item".No_ and (("Denmark$Production BOM Line"."Production BOM No_"='CS74001') AND ("Denmark$Production BOM Line"."Version Code"='A') or ("Denmark$Production BOM Line"."Production BOM No_"='ES00011') AND ("Denmark$Production BOM Line"."Version Code"='B') OR ("Denmark$Production BOM Line"."Production BOM No_"='ES00017') AND ("Denmark$Production BOM Line"."Version Code"='D') OR ("Denmark$Production BOM Line"."Production BOM No_"='HS74006') AND ("Denmark$Production BOM Line"."Version Code"='D') OR ("Denmark$Production BOM Line"."Production BOM No_"='US74001') AND ("Denmark$Production BOM Line"."Version Code"='B') OR ("Denmark$Production BOM Line"."Production BOM No_"='WS218') AND ("Denmark$Production BOM Line"."Version Code"='B'))
GROUP BY "Denmark$Production BOM Line"."Production BOM No_", "Denmark$Production BOM Line"."Version Code", "Denmark$Production BOM Line".No_, "Denmark$Production BOM Line".Description, "Denmark$Item"."Base Unit of Measure", "Denmark$Production BOM Line"."Unit of Measure Code", "Denmark$Production BOM Line"."Quantity per", "Denmark$Production BOM Line".Quantity
Her er den første linie skrevet ud, og som det er nu, vises der forkert i de 3 sidste felter, f.eks det felt som er lagerbeholdning, burde der kun stå 6 og ikke 3717
Produktionsstyklistenr;Versionskode;Nummer;Beskrivelse;Basisenhed;Enhedskode;Antal p;Antal ialt;Frigivet ordretilgang;Lagerbeholdning;Antal på købsordre CS74001;A;CT24028;Spændering;5;STK;STK;1;1;50234;3717;93602;
Queryen skal måske skrives lidt om, er der nogen eksperter derude som har et godt bud på at sum ikke lægger alle linier sammen per "Denmark$Production BOM Line".No_, , kan se når jeg laver en count at der er 281, så jeg mangler formentlig nok at lave min query mere unique... men kan ikke lige se hvor
Jeg har forsøgt at gøre din SQL noget mere overskuelig, og har sat ind ekstra paranterse i forbindelse med dine OR: Men for at få noget fornuftig ud af din forespørgsel, savner jeg en link mellem BOM tabellen og f.eks. item? Du har linket Item tabellen til Order, Ledger og Line tabellen, men BOM tabellen er ikke linket.
SELECT BOM.Production BOM No_" AS 'Produktionsstyklistenr', BOM.Version Code" AS 'Versionskode', BOM.No_ AS 'Nummer', BOM.Description AS 'Beskrivelse', Item."Base Unit of Measure" AS 'Basisenhed', BOM.Unit of Measure Code" AS 'Enhedskode', BOM.Quantity per" AS 'Antal pr', BOM.Quantity AS 'Antal ialt', Sum(Order.Quantity) AS 'Frigivet ordretilgang', Sum(Ledger.Quantity) AS 'Lagerbeholdning' Sum(Line."Outstanding Quantity") AS 'Antal på købsordre'
FROM [Denmark$Production BOM Line] BOM, [Denmark$Prod_ Order Line] Order, [Denmark$Item] Item, [Denmark$Item Ledger Entry] Ledger, [Denmark$Purchase Line] Line
WHERE Order.[Item No_] = Item.No_ AND Ledger.[Item No_] = Item.No_ AND Line.No_ = Item.No_ And ( ((BOM.Production BOM No_"='CS74001') AND (BOM.Version Code"='A')) or ((BOM.Production BOM No_"='ES00011') AND (BOM.Version Code"='B')) OR ((BOM.Production BOM No_"='ES00017') AND (BOM.Version Code"='D')) OR ((BOM.Production BOM No_"='HS74006') AND (BOM.Version Code"='D')) OR ((BOM.Production BOM No_"='US74001') AND (BOM.Version Code"='B')) OR ((BOM.Production BOM No_"='WS218') AND (BOM.Version Code"='B')) )
GROUP BY BOM.[Production BOM No_], BOM.[Version Code], BOM.[No_], BOM.[Description], Item.[Base Unit of Measure], BOM.[Unit of Measure Code], BOM.[Quantity per], BOM.[Quantity]
Jeg havde ikke fået konverteret alle " til [] i feltnavn. Jeg har ændret til [] da det er den normal måde på SQL Server til at vise feltnavn:
SELECT BOM.[Production BOM No_] AS 'Produktionsstyklistenr', BOM.[Version Code] AS 'Versionskode', BOM.[No_ AS] 'Nummer', BOM.[Description] AS 'Beskrivelse', Item.[Base Unit of Measure] AS 'Basisenhed', BOM.[Unit of Measure Code] AS 'Enhedskode', BOM.[Quantity per] AS 'Antal pr', BOM.[Quantity] AS 'Antal ialt', Sum(Order.Quantity) AS 'Frigivet ordretilgang', Sum(Ledger.Quantity) AS 'Lagerbeholdning' Sum(Line.[Outstanding Quantity]) AS 'Antal på købsordre'
FROM [Denmark$Production BOM Line] BOM, [Denmark$Prod_ Order Line] Order, [Denmark$Item] Item, [Denmark$Item Ledger Entry] Ledger, [Denmark$Purchase Line] Line
WHERE Order.[Item No_] = Item.No_ AND Ledger.[Item No_] = Item.No_ AND Line.No_ = Item.No_ And ( ((BOM.[Production BOM No_]='CS74001') AND (BOM.[Version Code]='A')) or ((BOM.[Production BOM No_]='ES00011') AND (BOM.[Version Code]='B')) OR ((BOM.[Production BOM No_]='ES00017') AND (BOM.[Version Code]='D')) OR ((BOM.[Production BOM No_]='HS74006') AND (BOM.[Version Code]='D')) OR ((BOM.[Production BOM No_]='US74001') AND (BOM.[Version Code]='B')) OR ((BOM.[Production BOM No_]='WS218') AND (BOM.[Version Code]='B')) )
GROUP BY BOM.[Production BOM No_], BOM.[Version Code], BOM.[No_], BOM.[Description], Item.[Base Unit of Measure], BOM.[Unit of Measure Code], BOM.[Quantity per], BOM.[Quantity]
Vær obs på at du, på den måde du linker dine tabeller sammen på, filtrerer, således at du kun får de Item.No_ hvor der findes tilsvarende No_ i både Order, Ledger og Line tabel. Dette kunne være en af grunderne til at du ikke får de summer som du ønsker?
Jeg løste selv problemet, og ja ved godt at det måske var lidt svært se hvor jeg ville hen. men sådan kom det til at se ud, og send mig begge et svar så kan i dele points, i gjorde et forsøgt og det skal i have noget for. (jeg godt klar over at bruge " istedet for [] og det skal jeg nok få rettet)
SELECT "Denmark$Production BOM Line"."Production BOM No_" AS 'Produktionsstyklistenr', "Denmark$Production BOM Line"."Version Code" AS 'Versionskode', "Denmark$Production BOM Line".No_ AS 'Nummer', "Denmark$Production BOM Line".Description AS 'Beskrivelse', "Denmark$Item"."Base Unit of Measure" AS 'Basisenhed', "Denmark$Production BOM Line"."Unit of Measure Code" AS 'Enhedskode', "Denmark$Production BOM Line"."Quantity per" AS 'Antal pr', "Denmark$Production BOM Line".Quantity AS 'Antal ialt',
(SELECT CASE WHEN Sum("Denmark$Prod_ Order Line".[Remaining Quantity]) is not null THEN Sum("Denmark$Prod_ Order Line".[Remaining Quantity]) ELSE 0 END FROM dbo.[Denmark$Prod_ Order Line] WHERE "Denmark$Prod_ Order Line"."Item No_" = "Denmark$Item".No_ ) AS 'Frigivet ordretilgang',
(SELECT CASE WHEN Sum("Denmark$Item Ledger Entry".Quantity) is not null THEN Sum("Denmark$Item Ledger Entry".Quantity) ELSE 0 END FROM dbo.[Denmark$Item Ledger Entry] WHERE "Denmark$Item Ledger Entry"."Item No_" = "Denmark$Item".No_ ) AS 'Lagerbeholdning',
(SELECT CASE WHEN Sum("Denmark$Purchase Line"."Outstanding Quantity") is not null THEN Sum("Denmark$Purchase Line"."Outstanding Quantity") ELSE 0 END FROM dbo.[Denmark$Purchase Line] WHERE "Denmark$Purchase Line".No_ = "Denmark$Item".No_ ) AS 'Antal på købsordre'
FROM dbo.[Denmark$Item] [Denmark$Item] INNER JOIN dbo.[Denmark$Production BOM Line] AS [Denmark$Production BOM Line] On "Denmark$Production BOM Line".No_ = "Denmark$Item".No_
WHERE
([Denmark$Production BOM Line].[Production BOM No_] = 'CS74001') AND ([Denmark$Production BOM Line].[Version Code] = 'A') OR ([Denmark$Production BOM Line].[Production BOM No_] = 'ES00011') AND ([Denmark$Production BOM Line].[Version Code] = 'B') OR ([Denmark$Production BOM Line].[Production BOM No_] = 'ES00017') AND ([Denmark$Production BOM Line].[Version Code] = 'D') OR ([Denmark$Production BOM Line].[Production BOM No_] = 'HS74006') AND ([Denmark$Production BOM Line].[Version Code] = 'D') OR ([Denmark$Production BOM Line].[Production BOM No_] = 'US74001') AND ([Denmark$Production BOM Line].[Version Code] = 'B') OR ([Denmark$Production BOM Line].[Production BOM No_] = 'WS218') AND ([Denmark$Production BOM Line].[Version Code] = 'B')
ORDER BY "Denmark$Production BOM Line"."Production BOM No_", "Denmark$Production BOM Line".No_
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.