Print Page | Close Window

Merging two SQL queries

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Database Discussion
Forum Description: Discussion and chat on database related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=1564
Printed Date: 29 March 2026 at 6:50pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Merging two SQL queries
Posted By: immersion
Subject: Merging two SQL queries
Date Posted: 02 April 2003 at 8:17pm

How do I merge the following two queries into one? I'm having problems with comparing iJob & iClient to themselves, which is why I'm inserting them in the second statement using ASP

Query 1 :

SELECT DISTINCT iClient, iJob, (SELECT tblClients.Name FROM tblClients WHERE Logbook.iClient = tblClients.ID) as Name
FROM Logbook
WHERE EXISTS (SELECT * FROM tblStatements WHERE tblStatements.iJob = Logbook.iJob AND tblStatements.iClient = Logbook.iClient)

Query 2 :

SELECT
(SELECT Sum(DateDiff('n',pStart,pFinish)) FROM Logbook WHERE DateDiff('ww',pDate,'" & Now & "') = 1 AND iEmployee = 1 AND iJob = " & iJob & " AND iClient = " & iClient & ") as Dan,
(SELECT Sum(DateDiff('n',pStart,pFinish)) FROM Logbook WHERE DateDiff('ww',pDate,'" & Now & "') = 1 AND iEmployee = 2 AND iJob = " & iJob & " AND iClient = " & iClient & ") as Angela, 
(SELECT Sum(DateDiff('n',pStart,pFinish)) FROM Logbook WHERE DateDiff('ww',pDate,'" & Now & "') = 1 AND iEmployee = 3  AND iJob = " & iJob & " AND iClient = " & iClient & ") as Matt,
(SELECT Sum(DateDiff('n',pStart,pFinish)) FROM Logbook WHERE DateDiff('ww',pDate,'" & Now & "') = 1 AND iEmployee > 3 AND iJob = " & iJob & " AND iClient = " & iClient & ") as Other
 FROM Logbook

And if you have any suggestions to clean this up (like having one datediff instead of 4 *hint hint*), I'd love to hear them




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net