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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Joining two select statements

Author  Topic 

Petronas
Posting Yak Master

134 Posts

Posted - 2008-11-21 : 15:27:51
Hi ,

I have two select statements. One to get orders_received and one for Orders_cancel.

select order_id as Orders_Received
from orders as o
where o.cancel_date is null
and o.marketing in (34109,34111)




select order_id as Orders_cancelled
from orders as o
where o.cancel_date is not null
and o.marketing in (34109,34111)

I want the result to be shown as

Orders_Received Orders_cancelled
3456676 567668
6787847 893804

How do I go about joining these two statement to get my output

Thanks,
Petronas

robvolk
Most Valuable Yak

15732 Posts

Posted - 2008-11-21 : 15:44:29
What happens if there are more orders received than cancelled, i.e. 4 rows vs. 5 rows?
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-11-21 : 15:46:39
Why would you want to join order id ?? What basis ??
Go to Top of Page

Petronas
Posting Yak Master

134 Posts

Posted - 2008-11-21 : 16:10:40
The requirement is to provide the order_received_date as the parameter for the orders_received.
and the cancel_date as the parameter for the orders_cancel and union the both. I know the row numbers are not same.
Any ideas of how this can be done?

Thanks for all your help,
Petronas
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-11-21 : 16:28:26
quote:
Originally posted by Petronas

The requirement is to provide the order_received_date as the parameter for the orders_received.
and the cancel_date as the parameter for the orders_cancel and union the both. I know the row numbers are not same.
Any ideas of how this can be done?

Thanks for all your help,
Petronas




quote:
Originally posted by Petronas

The requirement is to provide the order_received_date as the parameter for the orders_received.
and the cancel_date as the parameter for the orders_cancel and union the both. I know the row numbers are not same.
Any ideas of how this can be done?

Thanks for all your help,
Petronas




I'm sorry. I am not understanding it correct probably. If you want to union them both, then why join it ??
Or is it just for display/reporting purpose?
If its for reporting, rather have front end handle it.

Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-11-21 : 18:09:34
I know this may not be correct but Here is my shot:

select (case when cancel_date is null then Order_id else 0 end)as Order_received,
(case when cancel_date is not null then Order_id else 0 end)as Ordered_Cancelled
from Orders
Where marketing in (34109,34111)
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2008-11-22 : 15:32:27
This is probably easy but you need to explain better. What is that example input data for the example results you gave, and what are the results for the two individual queries you gave?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-23 : 01:48:37
quote:
Originally posted by Petronas

The requirement is to provide the order_received_date as the parameter for the orders_received.
and the cancel_date as the parameter for the orders_cancel and union the both. I know the row numbers are not same.
Any ideas of how this can be done?

Thanks for all your help,
Petronas


show some sample data from tables and explain what you want as output. that should make matters easy
One possible solution can be along the lines of what Sodeep posted, but without seeing data we cant confirm. Try to provide info in below format

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -