Author |
Topic |
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-08-28 : 11:32:55
|
Good morning good people. Perhaps you can assist me with this weird issue. I have 2 tables [TBB] & [TBB2][TBB] only has a total of 17,000 records[TBB2] only has a total of 20,000 recordsTrying to get the [Shipment No.] & [Posting Date] information from the [TBB2] table, Where the dbo.TBB.[Sales Order No.] = dbo.TBB2.[Sales Order No.]When I use the following query, I get a result of over 225,000 records which just can't be correct. Can you tell me what is wrong with this query and provide syntax corrections etc.(Query below)SELECT dbo.TBB.[Sales Invoice No.], dbo.TBB.[Invoice Posting Date], dbo.TBB.[Item No.], dbo.TBB.Description, dbo.TBB.Quantity, dbo.TBB.[Unit Price], dbo.TBB.[Unit Cost], dbo.TBB.[Sales Order No.], dbo.TBB2.[Shipment No.], dbo.TBB2.[Shipment Posting Date]FROM dbo.TBB RIGHT OUTER JOIN dbo.TBB2 ON dbo.TBB.[Sales Order No.] = dbo.vw_TBB2.[Sales Order No.]Thanks so much in advanceE. |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-08-28 : 11:37:24
|
Hi TaraThanks for your responce, However I tried the INNER JOIN per your suggestion and I still get a query result of over 225,000 records.ThanksE |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-08-28 : 11:41:49
|
Here is the changed query syntax, but I still get a query result of over 220,000 records and there are obly approx 35,000 records total between the 2 tables(changes query)SELECT dbo.TBB.[Sales Invoice No.], dbo.TBB.[Invoice Posting Date], dbo.TBB.[Item No.], dbo.TBB.Description, dbo.TBB.Quantity, dbo.TBB.[Unit Price], dbo.TBB.[Unit Cost], dbo.TBB.[Sales Order No.], dbo.TBB2.[Shipment No.], dbo.TBB2.[Shipment Posting Date]FROM dbo.TBB INNER JOIN dbo.TBB2 ON dbo.TBB.[Sales Order No.] = dbo.TBB2.[Sales Order No.]ThanksE |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-08-28 : 11:45:54
|
Hi TaraHere are the resultsSELECT COUNT(*) FROM TBB17,224SELECT COUNT(*) FROM TBB220,884ThanksE |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-28 : 11:47:47
|
SELECT COUNT(*), COUNT(DISTINCT [Sales Order No.]) FROM TBBSELECT COUNT(*), COUNT(DISTINCT [Sales Order No.]) FROM TBB2 E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-28 : 11:49:29
|
[code]SELECT [Sales Order No.], COUNT(*)FROM TBBGROUP BY [Sales Order No.]HAVING COUNT(*) > 1SELECT [Sales Order No.], COUNT(*)FROM TBB2GROUP BY [Sales Order No.]HAVING COUNT(*) > 1[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-08-28 : 11:52:35
|
Hi TaraHere are the resultsSELECT COUNT(*), COUNT(DISTINCT [Sales Order No.]) FROM TBB17224 6456SELECT COUNT(*), COUNT(DISTINCT [Sales Order No.]) FROM TBB220884 6991ThanksE |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-28 : 11:54:04
|
Oh wait... I think you have multiple records (one record for every item in an order) in table TBB!Try thisSELECT DISTINCT dbo.TBB2.[Shipment No.], dbo.TBB2.[Shipment Posting Date]FROM dbo.TBBINNER JOIN dbo.TBB2 ON dbo.TBB2.[Sales Order No.] = dbo.TBB.[Sales Order No.] E 12°55'05.25"N 56°04'39.16" |
 |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-08-28 : 12:02:38
|
Hi TaraThe following syntax:SELECT DISTINCT dbo.TBB.[Sales Invoice No.], dbo.TBB.[Invoice Posting Date], dbo.TBB.[Item No.], dbo.TBB.Description, dbo.TBB.Quantity, dbo.TBB.[Unit Price], dbo.TBB.[Unit Cost], dbo.TBB.[Sales Order No.], dbo.TBB2.[Shipment No.], dbo.TBB2.[Shipment Posting Date]FROM dbo.TBBINNER JOIN dbo.TBB2 ON dbo.TBB2.[Sales Order No.] = dbo.TBB.[Sales Order No.]Generates a result of 44362 records...This syntaxSELECT DISTINCT dbo.TBB2.[Shipment No.], dbo.TBB2.[Shipment Posting Date]FROM dbo.TBBINNER JOIN dbo.TBB2 ON dbo.TBB2.[Sales Order No.] = dbo.TBB.[Sales Order No.]Generates a result of only 9303 recordsE |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-28 : 12:04:40
|
You are the only one to know which is correct!SELECT dbo.TBB2.[Shipment No.], MIN(dbo.TBB2.[Shipment Posting Date]), MAX(dbo.TBB2.[Shipment Posting Date])FROM dbo.TBBINNER JOIN dbo.TBB2 ON dbo.TBB2.[Sales Order No.] = dbo.TBB.[Sales Order No.]GROUP BY dbo.TBB2.[Shipment No.]ORDER BY dbo.TBB2.[Shipment No.] E 12°55'05.25"N 56°04'39.16" |
 |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-08-28 : 12:07:05
|
Hi TaraYes I am checking the results for acuracy now, i'll let you know my findingsThank you again so much for your helpE |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-08-28 : 12:16:31
|
Hi Tara, again thank you so much for your helpThere is still some sort of problem. for example when running the followingSELECT DISTINCT dbo.TBB.[Sales Invoice No.],dbo.TBB.[Invoice Posting Date],dbo.TBB.[Item No.],dbo.TBB.Description,dbo.TBB.Quantity,dbo.TBB.[Unit Price],dbo.TBB.[Unit Cost],dbo.TBB.[Sales Order No.],dbo.TBB2.[Shipment No.],dbo.TBB2.[Shipment Posting Date]FROM dbo.TBBINNER JOIN dbo.TBB2 ON dbo.TBB2.[Sales Order No.] = dbo.TBB.[Sales Order No.]Which generates a result of 44362 records...The problem is that there is a Sales Invoice for instance (example [Sales Invoice No.] SI323221) which has 38 line items.with the query above, it only displays 3 of the line items which have the same [Item No.] |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-28 : 12:21:54
|
As said before, you are the only one to know what you want.SELECT dbo.TBB.[Sales Invoice No.], dbo.TBB.[Invoice Posting Date], dbo.TBB.[Item No.], dbo.TBB.Description, dbo.TBB.Quantity, dbo.TBB.[Unit Price], dbo.TBB.[Unit Cost], dbo.TBB.[Sales Order No.], dbo.TBB2.[Shipment No.], dbo.TBB2.[Shipment Posting Date]FROM dbo.TBBINNER JOIN dbo.TBB2 ON dbo.TBB2.[Sales Order No.] = dbo.TBB.[Sales Order No.] AND dbo.TBB2.[Item No.] = dbo.TBB.[Item No.] E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-28 : 12:26:33
|
quote: Originally posted by tkizer It's actually Peso/Peter helping out now.
It does day something about his concentration... E 12°55'05.25"N 56°04'39.16" |
 |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-08-28 : 12:39:16
|
perhaps I mis-understood and didn't make clear what my objective was.This is what I am trying to accomplish:I need all of the data, each line item in the following fields: dbo.TBB.[Sales Invoice No.], dbo.TBB.[Invoice Posting Date], dbo.TBB.[Item No.], dbo.TBB.Description, dbo.TBB.Quantity, dbo.TBB.[Unit Price], dbo.TBB.[Unit Cost], dbo.TBB.[Sales Order No.],and the corresponding dbo.TBB2.[Shipment No.] & dbo.TBB2.[Shipment Posting Date] databased on the [Sales Order No.] being the same between the 2 tables [dbo.TBB & dbo.TBB2]Given that there is only 17,224 records in the dbo.TBB table, and 20,884 records in the dbo.TBB2 tableI didn't think I should get a query result of over 224,000 records. Thus my reason for requesting assistanceHopefully this clears up any mis-understanding I may have cause and/or was unclear aboutThanks againE |
 |
|
pharoah35
Yak Posting Veteran
81 Posts |
Posted - 2008-08-28 : 13:11:17
|
Any idea on the correct syntax which will help me accomplish the following ?I need all of the data, each line item in the following fields:dbo.TBB.[Sales Invoice No.],dbo.TBB.[Invoice Posting Date],dbo.TBB.[Item No.],dbo.TBB.Description,dbo.TBB.Quantity,dbo.TBB.[Unit Price],dbo.TBB.[Unit Cost],dbo.TBB.[Sales Order No.],and the corresponding dbo.TBB2.[Shipment No.] & dbo.TBB2.[Shipment Posting Date] databased on the [Sales Order No.] being the same between the 2 tables [dbo.TBB & dbo.TBB2]Given that there is only 17,224 records in the dbo.TBB table, and 20,884 records in the dbo.TBB2 tableI didn't think I should get a query result of over 224,000 records. Thus my reason for requesting assistance |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
Next Page
|