0

I have two tables, PROJECT and BILLINGS, as follows:

Project_ID  Project_Client
----------  ------------------
P1          ABC Company
P2          DEF Company
P3          GHI Company

Proj_ID     Bill_Date   Bill_Amount
----------  ----------- -----------
P1          01-01-2013  123.45
P1          11-01-2013  234.56
P2          08-15-2013  345.67
P2          09-15-2013  456.78
P3          07-31-2013  567.89

I want to return the list of Projects and Project Clients only for projects that have billings AFTER 09-01-2013. How do I do the proper JOIN to do this?

6
  • The proper JOIN would be on the Project ID, but if you are looking for a full query, could you tell us what kind of SQL you are dealing with? MSSQL? MySQL?
    – SQLHound
    May 28, 2015 at 21:10
  • It's MSSQL...sorry!
    – Steve
    May 28, 2015 at 21:13
  • Also, the primary table is PROJECT (as there are already multiple JOINs from it).
    – Steve
    May 28, 2015 at 21:14
  • Also are you looking for a cumulative total bill amount for the project?
    – SQLHound
    May 28, 2015 at 21:27
  • No, actually just a list of Project_ID and Project_Client that have a billing on 9/1/2013 or later.
    – Steve
    May 28, 2015 at 21:28

2 Answers 2

1

If you want to get just project id and clients (which are both on project table) there is no need to join Billings :

SELECT p.Project_ID, p.Project_Client 
FROM  PROJECT p 
    WHERE EXISTS 
      (
         SELECT NULL FROM BILLINGS b WHERE b.Project_ID = a.Project_ID 
         AND b.Bill_Date > 'your_date'
      ) 
0
0

Try:

SELECT
    a.[Project ID]
    , a.[Project Client]
FROM PROJECT a
INNER JOIN BILLINGS b
    ON a.[Project ID] = b.[Proj_ID]
WHERE b.[Bill Date] > '09-01-2013'
2
  • Excellent! And that won't return multiple records if there are multiple billings >= 9/1/13?
    – Steve
    May 28, 2015 at 21:37
  • It could, but that's not the data as represented in the given BILLINGS table. The query could be made into a SELECT DISTINCT to combat that.
    – SQLHound
    May 28, 2015 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.