Solved

SQL Server 2008 - Display record depending on status of data in another table

Posted on 2013-05-28
1
386 Views
Last Modified: 2013-05-28
Trying to figure out how to write the SQL for the below scenario.

-  Lets say we have two tables Invoice and Timesheet.  
-  The Invoice table is used to pay all Timesheets.  
-  A user can work on the timesheet until they submit it.  Once it is submitted then it needs to be approved, rejected, or closed.

So, If the timesheet is new or open then I do not want the related invoice record to display.  It can only display if all associated timesheets are either approved, rejected, or closed.

Sample Table structure

-  Invoice.InvoiceID, Invoice.Status, Invoice.Amt, Invoice.PaidBy, Invoice.PaidOn

-  Timesheet.TimesheetID, Timesheet.InvoiceID, Timesheet.Status, TimeSheet.Rate, Timesheet.HoursWorked, Timesheet.Travel, Timesheet.Food, Timesheet.Gas, Timesheet.Hotel
0
Comment
Question by:CipherIS
1 Comment
 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
ID: 39201745
SELECT *
FROM Invoice I
WHERE NOT EXISTS (SELECT * FROM Timesheet WHERE I.InvoiceID=InvoiceID AND Status NOT IN ('Approved','Rejected','Closed'))

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 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

19 Experts available now in Live!

Get 1:1 Help Now