?
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
?
200 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
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

850 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