Solved

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

Posted on 2013-05-28
1
392 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

733 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