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
187 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 500 total points
Comment Utility
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 40

Expert Comment

by:Sharath
Comment Utility
can you provide some sample set and your desired output?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now