Author |
Topic |
new_bees
Starting Member
27 Posts |
Posted - 2008-05-08 : 16:57:14
|
Hi Guys,I'm back again. First of all, thank those who responded to my previous posts. This is new to me and I am still learning.Here is my new problem. I've been trying to do a join query unfortunately the results are different. Someone can pinpoint to me what is wrong with my query when I joined 3 tables. If I only joined 2 tables I get the correct results, but when I joined 3 tables the results change see below:1st QueryJoined 2 TablesSELECT b.ITEMDESC AS Description, SUM(a.QTYSHPPD) AS ReceivedFROM POP10500 a LEFT OUTER JOIN IV00500 b ON a.ITEMNMBR = b.ITEMNMBRWHERE (DATEPART([year], a.DATERECD) = '2008') AND (DATEPART([month], a.DATERECD) = '4')GROUP BY b.ITEMDESCHAVING (SUM(a.QTYSHPPD) > 0)ORDER BY b.ITEMDESC1st Query Result:ITEMNMBR | Description | Received1,3DIM110-001KGCTN | 1,3 Dimethylamlamine | .2500035DIITY110-001KGCTN | 3,5-Diiodo-L-Tyrosine | 1.00000HYDTRY110-025KGDRM | 5 HTP/Griffonia Seed Extract 98% | 250.0000067DIHY110-001KGCTN | 6,7-dihydroxybergamottin | 1.0000078BENZ110-001KGBAG | 7,8-Benzoflavone | 1.00000 ACESUL110-025KGDRM | Acesulfame K USP | 5000.00000================================================================================================2nd QueryJoined 3 TablesSELECT b. ITEMNMBR, b.ITEMDESC AS Description, SUM(a.QTYSHPPD) AS ReceivedFROM POP10500 a LEFT OUTER JOIN IV00500 b ON a.ITEMNMBR = b.ITEMNMBR INNER JOIN IV00102 c ON b.ITEMNMBR = c.ITEMNMBRWHERE (DATEPART([year], a.DATERECD) = '2008') AND (DATEPART([month], a.DATERECD) = '4')GROUP BY b.ITEMDESC, b. ITEMNMBRHAVING (SUM(a.QTYSHPPD) > 0)ORDER BY b.ITEMDESC2nd Query result:ITEMNMBR | Description | Received1,3DIM110-001KGCTN | 1,3 Dimethylamlamine | .7500035DIITY110-001KGCTN | 3,5-Diiodo-L-Tyrosine | 3.00000HYDTRY110-025KGDRM | 5 HTP/Griffonia Seed Extract 98% | 750.0000067DIHY110-001KGCTN | 6,7-dihydroxybergamottin | 2.0000078BENZ110-001KGBAG | 7,8-Benzoflavone | 3.00000 ACESUL110-025KGDRM | Acesulfame K USP | 20000.00000---------------------------------------------------------As you noticed, the Received column value changed when I joined 3 tables. Should it be the same because it link to the forieng key "ITEMNMBR"I think the problem is where I did the 3rd table join see in RED.IV00102 c ON b.ITEMNMBR = c.ITEMNMBRI want the "Received" column value the same as the 1st query when I joined the 3 tables. How do I do this?Please help...RegardsNew bee |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-08 : 17:06:43
|
Your first query doesn't even include ITEMNMBR in the output list. Your second query has an additional column in the GROUP BY. So you are doing different things rather than just adding another join. Please explain.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
new_bees
Starting Member
27 Posts |
Posted - 2008-05-08 : 17:12:44
|
Thanks tkizer,Okay, I added the ITEMNMBR to the 1st query. If I removed one/additional column in the GROUP BY in the 2nd query, I got an error message. The error says is not contain in the aggregate function. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-08 : 17:20:43
|
You don't just add columns to a GROUP BY to make an error go away. You add columns to the GROUP BY because that's what you want to group data on. See Jeff's blog for more info: http://weblogs.sqlteam.com/jeffs/archive/2007/07/20/60261.aspxSo what do you want to group the data on?And do these two queries return different result sets?:SELECT b.ITEMDESC AS Description, SUM(a.QTYSHPPD) AS ReceivedFROM POP10500 a LEFT OUTER JOINIV00500 b ON a.ITEMNMBR = b.ITEMNMBRWHERE (DATEPART([year], a.DATERECD) = '2008') AND (DATEPART([month], a.DATERECD) = '4')GROUP BY b.ITEMDESCHAVING (SUM(a.QTYSHPPD) > 0)ORDER BY b.ITEMDESCSELECT b.ITEMDESC AS Description, SUM(a.QTYSHPPD) AS ReceivedFROM POP10500 a LEFT OUTER JOINIV00500 b ON a.ITEMNMBR = b.ITEMNMBRINNER JOIN IV00102 c ON b.ITEMNMBR = c.ITEMNMBRWHERE (DATEPART([year], a.DATERECD) = '2008') AND (DATEPART([month], a.DATERECD) = '4')GROUP BY b.ITEMDESCHAVING (SUM(a.QTYSHPPD) > 0)ORDER BY b.ITEMDESCTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
new_bees
Starting Member
27 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-09 : 10:04:41
|
quote: Originally posted by new_bees I think the problem is adding/sum duplicate value. I tried using Sum(distinct a.QTYSHPPD) and still no luck. But I think I'm getting closer after reading this article. http://www.sqlteam.com/article/how-to-use-group-by-with-distinct-aggregates-and-derived-tablesAre there expert here who can pinpoint where and what needed to be corrected? I'm trying to join 3 tables.
Its because you have more than 1 records for same ITEMNMBR in IV00102 table. B/w can i ask the purpose behind joining to this table as i cant see you retrieving any fields from it in select list and also dont use any of its fields in condition checking? |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-09 : 11:45:34
|
There are plenty of people that can help you here, but you need to answer our questions. Check out my last post. The answer to the second question will determine if the problem was due to your grouping or with the third table.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
new_bees
Starting Member
27 Posts |
Posted - 2008-05-09 : 13:57:20
|
Thanks guys,I am confused about this whole project my boss wanted me to do. There are 3 tables that I need to query, all has primary key "ITEMNMBR" where I can joined them through the ITEMNMBR. Now, here is the difficult part I could not figure it out. My boss wants me to use "ITEMDESC" to joined all 3 tables, however only 2 of the 3 tables has the "ITEMDESC" column. I had it working but the problem is is not adding/sum the correct way because there is no ITEMDESC in the other tables. This is weird.Here is my SQL query. This one is working.Also, if someone can spot unnecessary code let me know.SELECT o.ITEMNMBR, o.ITEMDESC AS Description, d.Received AS Received, e.Sold AS SoldFROM IV00101 o INNER JOIN (SELECT ITEMNMBR, SUM(QTYSHPPD) AS Received FROM POP10500 WHERE (DATEPART([year], DATERECD) = '2008') AND (DATEPART([month], DATERECD) = '4') AND QTYSHPPD <> 0 GROUP BY ITEMNMBR) d ON o.ITEMNMBR = d.ITEMNMBR INNER JOIN (SELECT ITEMNMBR, SUM(QTYORDER) AS Sold FROM SOP30300 WHERE (DATEPART([year], ACTLSHIP) = '2008') AND (DATEPART([month], ACTLSHIP) = '4') GROUP BY ITEMNMBR) e ON o.ITEMNMBR = e.ITEMNMBRORDER BY o.ITEMDESCHere is the result.ITEMNMBR | ITEMDESC | Received | Sold-------------------------------------------------------------------------------------------------------ANIRAC110-025KGCTN | Aniracetam | 3.00000 | 3.00000ARTLEA110-025KGDRM | Artichoke (Synara Scolymus) Leaf P.E. 4:1 | 125.00000 | 125.00000ASCACI210-025KGCTN | Ascorbic Acid DC97 Cellulose | 10000.00000 | 2075.00000ASCPAL110-025KGDRM | Ascorbyl Palmitate FCC/USP | 1000.00000 | 185.00000ASHWAG150-025KGDRM | Ashwagandha Root P.E. 2.5% | 25.00000 | 25.00000BEEPOL110-020KGCTN | Bee Pollen Powder | 60.00000 | 60.00000BEEPOL110-025KGDRM | Bee Pollen Powder | 200.00000 | 100.00000BEEPOL110-025KGDRM | Bee Pollen Powder | 200.00000 | 100.00000The red color should SUMMED up because they all have same Item description, but the problem is the have different ITEMNMBR.Any help?Newbees |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-09 : 13:59:45
|
Why are you ignoring my posts?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
new_bees
Starting Member
27 Posts |
Posted - 2008-05-09 : 14:14:40
|
I'm sorry tkizer. Okay, I want to group by ITEMDESC. As you know in the above data marked red. The item desc "Bee Pollen Powder" is the same product but different ITEMNMBR due to the packaging type "KGCTN" KG cartons and "KGDRM" KG drums we received. Sinced they have different ITEMNMBR, I can't sum them up, but my boss wants me to use ITEMDESC to joined them. As I stated in my prrevoius post prior to this one. Only 2 tables "IV00101" and "SOP30300" that has the ITEMDESC column. Hope this clarify what I wanted to do. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-09 : 14:19:02
|
And what's the answer to my second question 3 posts ago? I need you to run those 2 queries without making any changes and check if the data is different. Let us know what you find.It doesn't matter that the joined columns are different. There is no requirement that they be the same. They can even be different between each joins.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-10 : 00:54:32
|
quote: Originally posted by new_bees Thanks guys,I am confused about this whole project my boss wanted me to do. There are 3 tables that I need to query, all has primary key "ITEMNMBR" where I can joined them through the ITEMNMBR. Now, here is the difficult part I could not figure it out. My boss wants me to use "ITEMDESC" to joined all 3 tables, however only 2 of the 3 tables has the "ITEMDESC" column. I had it working but the problem is is not adding/sum the correct way because there is no ITEMDESC in the other tables. This is weird.Here is my SQL query. This one is working.Also, if someone can spot unnecessary code let me know.SELECT o.ITEMNMBR, o.ITEMDESC AS Description, d.Received AS Received, e.Sold AS SoldFROM IV00101 o INNER JOIN (SELECT ITEMNMBR, SUM(QTYSHPPD) AS Received FROM POP10500 WHERE (DATEPART([year], DATERECD) = '2008') AND (DATEPART([month], DATERECD) = '4') AND QTYSHPPD <> 0 GROUP BY ITEMNMBR) d ON o.ITEMNMBR = d.ITEMNMBR INNER JOIN (SELECT ITEMNMBR, SUM(QTYORDER) AS Sold FROM SOP30300 WHERE (DATEPART([year], ACTLSHIP) = '2008') AND (DATEPART([month], ACTLSHIP) = '4') GROUP BY ITEMNMBR) e ON o.ITEMNMBR = e.ITEMNMBRORDER BY o.ITEMDESCHere is the result.ITEMNMBR | ITEMDESC | Received | Sold-------------------------------------------------------------------------------------------------------ANIRAC110-025KGCTN | Aniracetam | 3.00000 | 3.00000ARTLEA110-025KGDRM | Artichoke (Synara Scolymus) Leaf P.E. 4:1 | 125.00000 | 125.00000ASCACI210-025KGCTN | Ascorbic Acid DC97 Cellulose | 10000.00000 | 2075.00000ASCPAL110-025KGDRM | Ascorbyl Palmitate FCC/USP | 1000.00000 | 185.00000ASHWAG150-025KGDRM | Ashwagandha Root P.E. 2.5% | 25.00000 | 25.00000BEEPOL110-020KGCTN | Bee Pollen Powder | 60.00000 | 60.00000BEEPOL110-025KGDRM | Bee Pollen Powder | 200.00000 | 100.00000BEEPOL110-025KGDRM | Bee Pollen Powder | 200.00000 | 100.00000The red color should SUMMED up because they all have same Item description, but the problem is the have different ITEMNMBR.Any help?Newbees
SELECT MAX(o.ITEMNMBR) AS ITEMNMBR, o.ITEMDESC AS Description, SUM(d.Received) AS Received, MAX(e.Sold) AS SoldFROM IV00101 o INNER JOIN (SELECT ITEMNMBR, SUM(QTYSHPPD) AS Received FROM POP10500 WHERE (DATEPART([year], DATERECD) = '2008') AND (DATEPART([month], DATERECD) = '4') AND QTYSHPPD <> 0 GROUP BY ITEMNMBR) d ON o.ITEMNMBR = d.ITEMNMBR INNER JOIN (SELECT ITEMNMBR, SUM(QTYORDER) AS Sold FROM SOP30300 WHERE (DATEPART([year], ACTLSHIP) = '2008') AND (DATEPART([month], ACTLSHIP) = '4') GROUP BY ITEMNMBR) e ON o.ITEMNMBR = e.ITEMNMBRGROUP BY o.ITEMDESCORDER BY o.ITEMDESC This will give you what you are looking for i.e one row per ITEM DESC with sum of recieved and Sold values. But one thing you should note is that since you're grouping on ITEM DESC now you wont have a single ITEMNUMBER against each record. So my question what according to your business rules is ITEM NUMBER value to be shown against this summary record? As an example, i have taken the Maximun itemnumber value to be returned along with summary. If you can specify yopur business rule, then iwill be able to provide with full soln. |
 |
|
new_bees
Starting Member
27 Posts |
Posted - 2008-05-12 : 11:38:39
|
Thank you Visakh, your revised code did the job perfectly. The data from the query match from my manager spreadsheet. I'm glad to be a member of this site, so many good expert people willing to help beginner like myself. Thank you! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-12 : 11:43:26
|
quote: Originally posted by new_bees Thank you Visakh, your revised code did the job perfectly. The data from the query match from my manager spreadsheet. I'm glad to be a member of this site, so many good expert people willing to help beginner like myself. Thank you!
You're welcome. We are always glad to help you out |
 |
|
new_bees
Starting Member
27 Posts |
Posted - 2008-05-13 : 13:48:58
|
Hi guys,I am facing another problem. My manager wants to display all the products that were SOLD but has not Received, and Received but has not SOLD in month i.e April, both ways. The code Visak posted is working perfectly, however, I could not figure it out how to include them. Some of the products don't have DATERECD in the POP10500 table. When I query it, it returned blank record. POP10500 Table - Records Product ReceivedItemnmbr | QTYSHPPD | DATERECD----------------------------------------METSUL-989 | 0 | 2/28/2008CABON-745 | 0 | 2/27/2008METSUL-989 | | CABON-745 | | AMINO1-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 | 0 | GINS-145 | 0 | The Final Result should look like below:Itemnmbr | Received | Sold----------------------------------------METSUL-989 | 0 | 30CABON-745 | 0 | 25 AMINO1-989 | 50 | 0GINS-145 | 20 | 0The fields marked RED are need to show in the report.Since there are no DATEs in the "DATERECD & ACTLSHIP" fields, how do I accomplished this.Here is my SQL code:SELECT MAX(a.ITEMNMBR) AS ITEMNMBR, a.ITEMDESC AS Description, SUM(b.Received) AS Received, MAX(c.Sold) AS SoldFROM IV00101 a INNER JOIN (SELECT ITEMNMBR, SUM(QTYSHPPD) AS Received FROM POP10500 WHERE (DATEPART([year], DATERECD) = '2008') AND (DATEPART([month], DATERECD) = '4') GROUP BY ITEMNMBR) b ON a.ITEMNMBR = b.ITEMNMBR INNER JOIN (SELECT ITEMNMBR, SUM(QTYORDER) AS Sold FROM SOP30300 WHERE (DATEPART([year], ACTLSHIP) = '2008') AND (DATEPART([month], ACTLSHIP) = '4') AND (soptype = '3') GROUP BY ITEMNMBR) c ON a.ITEMNMBR = c.ITEMNMBRWHERE (b.Received = 0) AND (c.Sold <> 0)GROUP BY a.ITEMDESCORDER BY a.ITEMDESCDon't worry about the ItemDesc field. I just didn't include it here.Help again guys. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-13 : 14:05:05
|
Try like this:-SELECT MAX(a.ITEMNMBR) AS ITEMNMBR, a.ITEMDESC AS Description, SUM(b.Received) AS Received, MAX(c.Sold) AS SoldFROM IV00101 a INNER JOIN(SELECT ITEMNMBR, SUM(QTYSHPPD) AS ReceivedFROM POP10500WHERE (((DATEPART([year], DATERECD) = '2008') AND (DATEPART([month], DATERECD) = '4')) OR DATERECD IS NULL)GROUP BY ITEMNMBR) b ON a.ITEMNMBR = b.ITEMNMBR INNER JOIN(SELECT ITEMNMBR, SUM(QTYORDER) AS SoldFROM SOP30300WHERE (((DATEPART([year], ACTLSHIP) = '2008') AND (DATEPART([month], ACTLSHIP) = '4')) OR ACTLSHIP IS NULL) AND (soptype = '3')GROUP BY ITEMNMBR) c ON a.ITEMNMBR = c.ITEMNMBRWHERE (b.Received = 0) AND (c.Sold <> 0)GROUP BY a.ITEMDESCORDER BY a.ITEMDESC |
 |
|
new_bees
Starting Member
27 Posts |
Posted - 2008-05-14 : 11:47:24
|
Visakh I tried your new code and didn't work. It does not count right.Thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-14 : 14:24:41
|
quote: Originally posted by new_bees Visakh I tried your new code and didn't work. It does not count right.Thanks
Are dates having NULL values or ' ' values for RED records? |
 |
|
|