?
Solved

How do you write a query when 2 fields in two different tables need to be joined to sort by Example..?

Posted on 2008-10-15
3
Medium Priority
?
196 Views
Last Modified: 2010-03-20
I want to  write this query :

select  A.lead, A.date_sold,A.price,A.money,B.pay_amount,B.note_date
from TableA  A, TableB  B
where customerid =  1  (on both tables)

I need the info above but I need to sort the data by combining the A.date_sold and the B.note_date together so the rows are sorted by date

Another way of explaining it:

Select * from table A   where customerid = 1 (the date field in this table is date_sold)
Select * from table B where customerid = 1 (the date field in this table is note_date)

I need them sorted together so the queries are in order by date.


0
Comment
Question by:LeadCo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22725888
this will do:
select  A.lead, A.date_sold,A.price,A.money,B.pay_amount,B.note_date 
from TableA  A
join TableB  B
  on b.Customerid = a.CustomerID
where a.customerid =  1 

Open in new window

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 2000 total points
ID: 22725967
Do you need to sort by the newer of the two values (note_date and date_sold)?

If so?

"I need the info above but I need to sort the data by combining the A.date_sold and the B.note_date together so the rows are sorted by date "
You didn't say which takes precedence as the primary sort.  Or is by the newer of the two?

(select  A.lead, A.date_sold,A.price,A.money,B.pay_amount,B.note_date, case when a.date_sold > b.note_date then a.date_sold else b.note_date end as SortDate
from TableA  A
join TableB  B
  on b.Customerid = a.CustomerID
where a.customerid =  1 )
order by case when a.date_sold > b.note_date then a.date_sold else b.note_date end

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 22915479
can you provide some sample set and your desired output?
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question