Solved

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

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with SQL Server Stoplist 2 20
Managing Columnstore Indexes 2 18
SQL Field Length for Email Address 3 17
SQL Pivot add row totals 2 13
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

895 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

21 Experts available now in Live!

Get 1:1 Help Now