Welcome to Dream.In.Code
Become an Expert!

Join 150,404 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 931 people online right now. Registration is fast and FREE... Join Now!




SQL Query Problems ?

 
Reply to this topicStart new topic

SQL Query Problems ?

ryan_reinaldi
18 Mar, 2008 - 06:00 PM
Post #1

New D.I.C Head
*

Joined: 20 Dec, 2007
Posts: 9


My Contributions
Hi, my name is Ryan.

I have some difficulties in query issues. I have this two UNION queries, like below.

This is the first union query:

CODE
SELECT DISTINCT  dbo.BOS_AR_Customer.szCustId, dbo.BOS_AR_Customer.szName, dbo.BOS_SD_FDo.szDoId, dbo.BOS_SD_FDoItem.decQty,
dbo.BOS_SD_FDoItem.decPrice
FROM dbo.BOS_AR_Customer INNER JOIN
dbo.BOS_SD_FDo ON dbo.BOS_AR_Customer.szCustId = dbo.BOS_SD_FDo.szCustId INNER JOIN
dbo.BOS_SD_FDoItem ON dbo.BOS_SD_FDo.szDoId = dbo.BOS_SD_FDoItem.szDoId
where BOS_SD_FDo.szSalesid = '001-D004' and BOS_SD_fdo.dtmdelivery = '2008-03-11' and BOS_SD_Fdo.bApplied = 1 and BOS_SD_FDo.szFsoId=' '
UNION
SELECT DISTINCT dbo.BOS_AR_Customer.szCustId, dbo.BOS_AR_Customer.szName, dbo.BOS_SD_Fso.szfsoId as szOrdNum, dbo.BOS_SD_fsoItem.decQty,
dbo.BOS_SD_fsoItem.decPrice
FROM dbo.BOS_AR_Customer INNER JOIN
dbo.BOS_SD_fso ON dbo.BOS_AR_Customer.szCustId = dbo.BOS_SD_fso.szCustId INNER JOIN
dbo.BOS_SD_fsoItem ON dbo.BOS_SD_fso.szfsoId = dbo.BOS_SD_fsoItem.szfsoId
where BOS_SD_fso.szSalesid = '001-D004' and BOS_SD_fso.dtmOrder = '2008-03-11' and BOS_SD_fso.bApplied = 1


And This is the second one:

CODE
select BOS_SD_FSOitem.szProductId, BOS_INV_Product.szName from BOS_SD_FSOItem inner join
BOS_INV_Product
on BOS_SD_FSOItem.szProductid = BOS_INV_PRoduct.szProductId
inner join BOS_SD_Fso on BOS_SD_FSOItem.szFsoId = BOS_SD_Fso.szFsoid
where szSalesid = '001-D004' and BOS_SD_FSO.dtmorder = '2008-03-11'
UNION
select BOS_SD_fdoitem.szProductId, BOS_INV_Product.szName from BOS_SD_fdoItem inner join
BOS_INV_Product
on BOS_SD_fdoItem.szProductid = BOS_INV_PRoduct.szProductId
inner join BOS_SD_fdo on BOS_SD_fdoItem.szdoId = BOS_SD_fdo.szdoid
where szSalesid = '001-D004' and BOS_SD_fdo.dtmdelivery = '2008-03-11'


What I want to do is, how can I gathered those two union queries and make it be as one result. I have tried using join believe me blink.gif , but the result is not the way I expected. So, can you help me guys Please ???? I really do need some assistance here, I just don`t wanna quit that easy.

I appreciate any kind of help.



FYI: I use SQL Server 2000. Tables that I used are BOS_AR_Cutomer, BOS_INV_Product, BOS_SD_FDO, BOS_SD_FDOItem, BOS_SD_FSO, BOS_SD_FSOItem.



Regards.


User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: SQL Query Problems ?
18 Mar, 2008 - 06:31 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
QUOTE(ryan_reinaldi @ 18 Mar, 2008 - 07:00 PM) *

Hi, my name is Ryan.I have tried using join believe me blink.gif , but the result is not the way I expected.


What is your expected results? What did the join produce versus what you need it to produce?
User is offlineProfile CardPM
+Quote Post

ryan_reinaldi
RE: SQL Query Problems ?
18 Mar, 2008 - 07:03 PM
Post #3

New D.I.C Head
*

Joined: 20 Dec, 2007
Posts: 9


My Contributions
At first I have tried using joins, merge all the six tables. I've tried left, outer, inner join but the result is far from the goals. Sometimes the result give me all null from the customer name, though there are customer name that match with the other data. Even sometimes the data doesn't show at all. I've tried using 'IN' keyword also by putting the second query in the first like IN '(Second query)', but I got this error:

'Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.'

So maybe all of you out there could give me some suggestions. Thanks.



Regards.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: SQL Query Problems ?
19 Mar, 2008 - 04:07 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,291



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
First, the two queries you offered do not need union at all, they only differ in the where. Also, the first is redundant, as you give criteria for BOS_SD_FDo.szFsoId=' ' in the first union and then just drop it in the second.

The second query is similar, but just requires "or" logic. You can optimize both quieries like so:

sql

SELECT DISTINCT dbo.BOS_AR_Customer.szCustId, dbo.BOS_AR_Customer.szName,
dbo.BOS_SD_Fso.szfsoId as szOrdNum, dbo.BOS_SD_fsoItem.decQty, dbo.BOS_SD_fsoItem.decPrice
FROM dbo.BOS_AR_Customer
INNER JOIN dbo.BOS_SD_fso ON dbo.BOS_AR_Customer.szCustId = dbo.BOS_SD_fso.szCustId
INNER JOIN dbo.BOS_SD_fsoItem ON dbo.BOS_SD_fso.szfsoId = dbo.BOS_SD_fsoItem.szfsoId
where BOS_SD_fso.szSalesid = '001-D004'
and BOS_SD_fso.dtmOrder = '2008-03-11'
and BOS_SD_fso.bApplied = 1

select BOS_SD_FSOitem.szProductId, BOS_INV_Product.szName
from BOS_SD_FSOItem
inner join BOS_INV_Product on BOS_SD_FSOItem.szProductid = BOS_INV_PRoduct.szProductId
inner join BOS_SD_Fso on BOS_SD_FSOItem.szFsoId = BOS_SD_Fso.szFsoid
where szSalesid = '001-D004'
and ( BOS_SD_FSO.dtmorder = '2008-03-11' or BOS_SD_fdo.dtmdelivery = '2008-03-11')


Now for the merged query. First, union should only be used as a last resort! In fact, you never really have to use it at all, you can work around it if need be. Don't use union!

This is what I think you're looking for

sql

SELECT DISTINCT dbo.BOS_AR_Customer.szCustId, dbo.BOS_AR_Customer.szName,
dbo.BOS_SD_Fso.szfsoId as szOrdNum, dbo.BOS_SD_fsoItem.decQty, dbo.BOS_SD_fsoItem.decPrice,
BOS_SD_FSOitem.szProductId, BOS_INV_Product.szName
FROM dbo.BOS_AR_Customer
INNER JOIN dbo.BOS_SD_fso ON dbo.BOS_AR_Customer.szCustId = dbo.BOS_SD_fso.szCustId
INNER JOIN dbo.BOS_SD_fsoItem ON dbo.BOS_SD_fso.szfsoId = dbo.BOS_SD_fsoItem.szfsoId
inner join BOS_INV_Product on BOS_SD_FSOItem.szProductid = BOS_INV_PRoduct.szProductId
where BOS_SD_fso.szSalesid = '001-D004'
and BOS_SD_fso.bApplied = 1
and ( BOS_SD_FSO.dtmorder = '2008-03-11' or BOS_SD_fdo.dtmdelivery = '2008-03-11')


Also, this could be easier to follow with some aliases:

sql

SELECT DISTINCT cust.szCustId, cust.szName as CustName,
fso.szfsoId as szOrdNum, item.decQty, item.decPrice,
item.szProductId, prod.szName as ProductName
FROM dbo.BOS_SD_fso fso
INNER JOIN dbo.BOS_AR_Customer cust ON cust.szCustId = fso.szCustId
INNER JOIN dbo.BOS_SD_fsoItem item ON item.szfsoId = fso.szfsoId
inner join BOS_INV_Product prod on item.szProductid = prod.szProductId
where fso.bApplied = 1
and fso.szSalesid = '001-D004'
and ( fso.dtmorder = '2008-03-11' or fso.dtmdelivery = '2008-03-11')


I mixed the order up a little on the last one, I thought is scanned better, since everything is dependant on fso, not cust.

Hope this helps.

User is online!Profile CardPM
+Quote Post

JulieBunavicz
RE: SQL Query Problems ?
27 Apr, 2008 - 12:32 PM
Post #5

New D.I.C Head
*

Joined: 27 Apr, 2008
Posts: 6

Much easier to read with the alias - TY

Regards,

Julie Bunavicz
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month