Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
new_bees
Starting Member
27 Posts |
Posted - 2008-05-14 : 12:50:13
|
I really need help. I'm trying to create a product movement in a month's view. Show all product that were sold but not (0) Received in month of April and show all product thas were Received but has not (0) Sold in April.Here are the tables.POP10500 Table - Records Product ReceivedItemnmbr | QTYSHPPD | DATERECD----------------------------------------CABON-745 | 0 | 2/27/2008AMINO1-989 | 50 | 4/2/2008GINS-145 | 20 | 4/2/2008SOP30300 Table - Records Product SoldItemnmbr | QTYORDER | ACTLSHIP----------------------------------------METSUL-989 | 20 | 4/10/2008METSUL-989 | 10 | 4/9/2008CABON-745 | 10 | 4/6/2008CABON-745 | 15 | 4/5/2008AMINO1-989 | | GINS-145 | | The Final Result should look like below:Itemnmbr | Received | Sold----------------------------------------METSUL-989 | 0 | 30CABON-745 | 0 | 25 AMINO1-989 | 50 | 0GINS-145 | 20 | 0As you can see in the tables above. The product "METSUL-989" in the SOP30300 table has not received shipment in the month of April therefore no record for that month in table POP10500, but has sold 30KG in the SOP30300 table.When I use the Where clause "WHERE (((DATEPART([year], DATERECD) = '2008') AND (DATEPART([month], DATERECD) = '4')) OR DATERECD IS NULL)"to query shipment received in April it's not counting the sold from the table SOP30300. The Final result should look like the above final result data.How do I link these tables and still able to add the sold from SOP30300 even if that product has not order or no record in the POP10500 for the month of April?MY SQL query:SELECT MAX(a.ITEMNMBR) AS ITEMNMBR, a.ITEMDESC AS Description, SUM(b.Received) AS Received, MAX(c.Sold) AS Sold FROM IV00101 a INNER JOIN ( SELECT ITEMNMBR, SUM(QTYSHPPD) AS Received FROM POP10500 WHERE (DATEPART([year], DATERECD) = @Year) AND (DATEPART([month], DATERECD) = @Month) GROUP BY ITEMNMBR ) b ON a.ITEMNMBR = b.ITEMNMBR INNER JOIN ( SELECT ITEMNMBR, SUM(QTYORDER) AS Sold FROM SOP30300 WHERE (DATEPART([year], ACTLSHIP) = @Year) AND (DATEPART([month], ACTLSHIP) = @Month) GROUP BY ITEMNMBR ) c ON a.ITEMNMBR = c.ITEMNMBR GROUP BY a.ITEMDESC ORDER BY a.ITEMDESCCan you help me out again?Thanks,Newbee |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-14 : 13:05:22
|
[code]SELECT MAX(a.ITEMNMBR) AS ITEMNMBR, a.ITEMDESC AS Description, ISNULL(SUM(b.Received),0) AS Received, ISNULL(SUM(c.Sold),0) AS SoldFROM IV00101 a LEFT JOIN(SELECT ITEMNMBR, SUM(QTYSHPPD) AS ReceivedFROM POP10500WHERE (DATEPART([year], DATERECD) = @Year) AND (DATEPART([month], DATERECD) = @Month) GROUP BY ITEMNMBR) b ON a.ITEMNMBR = b.ITEMNMBR LEFT JOIN(SELECT ITEMNMBR, SUM(QTYORDER) AS SoldFROM SOP30300WHERE (DATEPART([year], ACTLSHIP) = @Year) AND (DATEPART([month], ACTLSHIP) = @Month)GROUP BY ITEMNMBR) c ON a.ITEMNMBR = c.ITEMNMBRGROUP BY a.ITEMDESCORDER BY a.ITEMDESC[/code] |
 |
|
new_bees
Starting Member
27 Posts |
Posted - 2008-05-14 : 13:43:59
|
Visakh, thanks a lot, I think I'm getting closer. The code just need a little tweak.I want to filter out the field marked in RED with no received and no sold in month. My boss don't want to see them.Also be able to SUM up the received and sold by DescriptionITEMNMBR | Description | Received | Sold----------------------------------------------------------------------------------1,3DIM110-001KGCTN | 1,3 Dimethylamlamine | .25 | .25146AND110-001KGBAG | 1,4,6-andros | 0 | 0ASCB-21 | Ascorbic C | 0 | 0Amino25 | Amino Acid | 0 | 0METISO110-005KGCTN | 5-Methyl-7-Methoxy | 0 | 0METISO110-025KGDRM | 5-Methyl-7-Methoxy | 0 | 5BETALA110-025KGCTN | Bete Alanine | 0 | 4BETALA110-025KGDRM | Bete Alanine | 10 | 4The Result should look like below table:ITEMNMBR | Description | Received | Sold----------------------------------------------------------------------------------1,3DIM110-001KGCTN | 1,3 Dimethylamlamine | .25 | .25METISO110-025KGDRM | 5-Methyl-7-Methoxy | 0 | 5BETALA110-025KGCTN | Bete Alanine | 10 | 8I tried to add a HAVING and where clause but no lock. It gives me an error.SELECT MAX(a.ITEMNMBR) AS ITEMNMBR, a.ITEMDESC AS Description, ISNULL(SUM(b.Received),0) AS Received, ISNULL(SUM(c.Sold),0) AS SoldFROM IV00101 a LEFT JOIN(SELECT ITEMNMBR, SUM(QTYSHPPD) AS ReceivedFROM POP10500WHERE (DATEPART([year], DATERECD) = @Year) AND (DATEPART([month], DATERECD) = @Month) GROUP BY ITEMNMBR) b ON a.ITEMNMBR = b.ITEMNMBR LEFT JOIN(SELECT ITEMNMBR, SUM(QTYORDER) AS SoldFROM SOP30300WHERE (DATEPART([year], ACTLSHIP) = @Year) AND (DATEPART([month], ACTLSHIP) = @Month)GROUP BY ITEMNMBR) c ON a.ITEMNMBR = c.ITEMNMBRWHERE (b.Received <> 0) AND (c.Sold = 0)GROUP BY a.ITEMDESCHAVING ISNULL(SUM(c.Sold))ORDER BY a.ITEMDESCThanks a lot...New Bee |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-14 : 13:55:14
|
quote: Originally posted by new_bees Visakh, thanks a lot, I think I'm getting closer. The code just need a little tweak.I want to filter out the field marked in RED with no received and no sold in month. My boss don't want to see them.Also be able to SUM up the received and sold by DescriptionITEMNMBR | Description | Received | Sold----------------------------------------------------------------------------------1,3DIM110-001KGCTN | 1,3 Dimethylamlamine | .25 | .25146AND110-001KGBAG | 1,4,6-andros | 0 | 0ASCB-21 | Ascorbic C | 0 | 0Amino25 | Amino Acid | 0 | 0METISO110-005KGCTN | 5-Methyl-7-Methoxy | 0 | 0METISO110-025KGDRM | 5-Methyl-7-Methoxy | 0 | 5BETALA110-025KGCTN | Bete Alanine | 0 | 4BETALA110-025KGDRM | Bete Alanine | 10 | 4The Result should look like below table:ITEMNMBR | Description | Received | Sold----------------------------------------------------------------------------------1,3DIM110-001KGCTN | 1,3 Dimethylamlamine | .25 | .25METISO110-025KGDRM | 5-Methyl-7-Methoxy | 0 | 5BETALA110-025KGCTN | Bete Alanine | 10 | 8I tried to add a HAVING and where clause but no lock. It gives me an error.SELECT MAX(a.ITEMNMBR) AS ITEMNMBR, a.ITEMDESC AS Description, ISNULL(SUM(b.Received),0) AS Received, ISNULL(SUM(c.Sold),0) AS SoldFROM IV00101 a LEFT JOIN(SELECT ITEMNMBR, SUM(QTYSHPPD) AS ReceivedFROM POP10500WHERE (DATEPART([year], DATERECD) = @Year) AND (DATEPART([month], DATERECD) = @Month) GROUP BY ITEMNMBR) b ON a.ITEMNMBR = b.ITEMNMBR LEFT JOIN(SELECT ITEMNMBR, SUM(QTYORDER) AS SoldFROM SOP30300WHERE (DATEPART([year], ACTLSHIP) = @Year) AND (DATEPART([month], ACTLSHIP) = @Month)GROUP BY ITEMNMBR) c ON a.ITEMNMBR = c.ITEMNMBRWHERE (b.Received <> 0) AND (c.Sold = 0)GROUP BY a.ITEMDESCHAVING ISNULL(SUM(c.Sold))ORDER BY a.ITEMDESCThanks a lot...New Bee
SELECT MAX(a.ITEMNMBR) AS ITEMNMBR, a.ITEMDESC AS Description, ISNULL(SUM(b.Received),0) AS Received, ISNULL(SUM(c.Sold),0) AS SoldFROM IV00101 a LEFT JOIN(SELECT ITEMNMBR, SUM(QTYSHPPD) AS ReceivedFROM POP10500WHERE (DATEPART([year], DATERECD) = @Year) AND (DATEPART([month], DATERECD) = @Month) GROUP BY ITEMNMBR) b ON a.ITEMNMBR = b.ITEMNMBR LEFT JOIN(SELECT ITEMNMBR, SUM(QTYORDER) AS SoldFROM SOP30300WHERE (DATEPART([year], ACTLSHIP) = @Year) AND (DATEPART([month], ACTLSHIP) = @Month)GROUP BY ITEMNMBR) c ON a.ITEMNMBR = c.ITEMNMBRGROUP BY a.ITEMDESCHAVING (SUM(b.Received) + SUM(c.Sold)) >0ORDER BY a.ITEMDESC |
 |
|
|
|
|
|
|