Solved

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

Posted on 2013-05-28
1
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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