MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 17th, 2004, 03:27 AM
sonasonia sonasonia is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 31 sonasonia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 40 sec
Reputation Power: 5
Combining multiple queries into one

Hi,

I have 3 different queries and i want to get the count of each query in a single query.


Below are my queries. I want the result as

Count of Query A Count of Query B Count of Query c
-----------------------------------------------------
10 20 13

Can this be achieved using stored procedures. Any idea will be very much appreciated.

Query A:

SELECT tbl_customer.ClientCode
FROM tbl_customer INNER JOIN tbl_loginaccount ON tbl_customer.EmployeeID = tbl_loginaccount.EmployeeID
WHERE (tbl_customer.ClientCode NOT IN
(SELECT clientcode
FROM tbl_customerfup)) AND (tbl_customer.FirstCall <> '' OR
tbl_customer.FirstCall IS NOT NULL) AND (tbl_customer.DiscussionSummary <> '' OR
tbl_customer.DiscussionSummary IS NOT NULL)


Query B:

SELECT tbl_customer.ClientCode
FROM tbl_customer INNER JOIN
tbl_loginaccount ON tbl_customer.EmployeeID = tbl_loginaccount.EmployeeID INNER JOIN
tbl_mastersector ON tbl_customer.SectorCode = tbl_mastersector.SectorCode INNER JOIN
tbl_customerfup ON tbl_customer.ClientCode = tbl_customerfup.ClientCode
WHERE (tbl_customerfup.Fup =
(SELECT MAX(fup)
FROM tbl_customerfup
WHERE tbl_customer.ClientCode = tbl_customerfup.ClientCode) AND (tbl_customer.FirstCall <> '' OR
tbl_customer.FirstCall IS NOT NULL) AND (tbl_customer.DiscussionSummary <> '' OR
tbl_customer.DiscussionSummary IS NOT NULL))

Query C:

SELECT tbl_customer.ClientCode
FROM tbl_customer INNER JOIN
tbl_loginaccount ON tbl_customer.EmployeeID = tbl_loginaccount.EmployeeID INNER JOIN
tbl_mastersector ON tbl_customer.SectorCode = tbl_mastersector.SectorCode INNER JOIN
tbl_customerfup ON tbl_customer.ClientCode = tbl_customerfup.ClientCode
WHERE (tbl_customer.FirstCall <> '' OR
tbl_customer.FirstCall IS NOT NULL) AND (tbl_customerfup.Fup IN
(SELECT fup
FROM tbl_customerfup
WHERE tbl_customer.ClientCode = tbl_customerfup.ClientCode AND Sent = 'Proposal')) AND
(tbl_customer.DiscussionSummary <> '' OR
tbl_customer.DiscussionSummary IS NOT NULL)

Thanks and Regards
SS

Reply With Quote
  #2  
Old March 18th, 2004, 10:51 AM
sypher sypher is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Montreal
Posts: 92 sypher User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 10 sec
Reputation Power: 5
after each select just use @@rowCount and store it into a var because you will loose it .
like this
select A.....
set CountOfA = @@rowcount

select b...
set CountOfB = @@rowcount
select C.....
set CountOfC = @@rowcount

at the end

select CountOfA, CountOfB, CountOfC

Reply With Quote
  #3  
Old March 18th, 2004, 10:59 PM
sonasonia sonasonia is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 31 sonasonia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 40 sec
Reputation Power: 5
Hi sypher,

Your reply was of great use. I modified a bit and worked great. Thanks so much.

My stored procedure is as follows.

CREATE PROCEDURE dbo.SummarySQL @v varchar(10) AS


begin

Declare @rc1 int
Declare @rc2 int
Declare @rc3 int



SELECT @rc1=count(tbl_customer.ClientCode)
FROM tbl_customer INNER JOIN
tbl_loginaccount ON tbl_customer.EmployeeID = tbl_loginaccount.EmployeeID INNER JOIN
tbl_mastersector ON tbl_customer.SectorCode = tbl_mastersector.SectorCode INNER JOIN
tbl_customerfup ON tbl_customer.ClientCode = tbl_customerfup.ClientCode
WHERE (tbl_customerfup.Fup =
(SELECT MAX(fup)
FROM tbl_customerfup
WHERE tbl_customer.ClientCode = tbl_customerfup.ClientCode) AND (tbl_customer.FirstCall <> '' OR
tbl_customer.FirstCall IS NOT NULL) AND (tbl_customer.DiscussionSummary <> '' OR
tbl_customer.DiscussionSummary IS NOT NULL))





SELECT @rc2=count( tbl_customer.ClientCode)
FROM tbl_customer INNER JOIN
tbl_loginaccount ON tbl_customer.EmployeeID = tbl_loginaccount.EmployeeID INNER JOIN
tbl_mastersector ON tbl_customer.SectorCode = tbl_mastersector.SectorCode INNER JOIN
tbl_customerfup ON tbl_customer.ClientCode = tbl_customerfup.ClientCode
WHERE (tbl_customer.FirstCall <> '' OR
tbl_customer.FirstCall IS NOT NULL) AND (tbl_customerfup.Fup IN
(SELECT fup
FROM tbl_customerfup
WHERE tbl_customer.ClientCode = tbl_customerfup.ClientCode AND Sent = 'Proposal')) AND
(tbl_customer.DiscussionSummary <> '' OR
tbl_customer.DiscussionSummary IS NOT NULL)




SELECT @rc3=count(tbl_customer.ClientCode)
FROM tbl_customer INNER JOIN tbl_loginaccount ON tbl_customer.EmployeeID = tbl_loginaccount.EmployeeID
WHERE (tbl_customer.ClientCode NOT IN
(SELECT clientcode
FROM tbl_customerfup)) AND (tbl_customer.FirstCall <> '' OR
tbl_customer.FirstCall IS NOT NULL) AND (tbl_customer.DiscussionSummary <> '' OR
tbl_customer.DiscussionSummary IS NOT NULL)



select @rc1 as a,@rc2 as b , @rc3 as c

end
GO


Thanks and regards
SS

Reply With Quote
  #4  
Old March 19th, 2004, 12:12 PM
sypher sypher is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Montreal
Posts: 92 sypher User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 10 sec
Reputation Power: 5
glad to serve!!

Reply With Quote
  #5  
Old March 25th, 2004, 01:15 PM
alberte alberte is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 alberte User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
So i have 3 queries

Query 1 Student - shows student name and attendance based on empid
From courseReg table.

SQL = SELECT tblAuthor.name, courseReg.attended, courseReg.CourseID
FROM tblAuthor INNER JOIN courseReg ON tblAuthor.employee_code = courseReg.empid
WHERE (((courseReg.empid)="1175"))
or = (((courseReg.empid)='" & empid & "'))


Query 2 Courses - shows all course Titles
From [Course Titles] table

SQL = SELECT [Course Titles].Title, Courses.ID AS albert
FROM [Course Titles] LEFT JOIN Courses ON [Course Titles].ID = Courses.TitleID

query 3 Report

combines the two queries above in one that shows student name
attendance all all Course Titles

SQL = SELECT student.name, course.Title, student.attended
FROM student RIGHT JOIN course ON student.CourseID = course.albert

The problem is back at the first query (student). The report
query WELL AS LONG AS as i give it a value like 1175 in the student
query.

I have a dynamic form that shows all student names and then passes
the selected empid value to another asp page were that it has query 3 (report).

I don't know how to make query 3 run with the empid coming from a form and not being set at the datebase.

I hope i made my self clear.
So thank for your help or if here is another silution.

Reply With Quote
  #6  
Old March 29th, 2004, 01:51 AM
sonasonia sonasonia is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 31 sonasonia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 40 sec
Reputation Power: 5
You can try something like the following. Replace the student id with the parameter. In the asp page instead of SQL query you execute the stored procedure

Stored Procedure:

CREATE PROCEDURE dbo.QrySQL @v varchar(60) AS

Begin

SELECT tblAuthor.name, courseReg.attended, courseReg.CourseID
FROM tblAuthor INNER JOIN courseReg ON tblAuthor.employee_code = courseReg.empid
WHERE (((courseReg.empid)=@v))
or = (((courseReg.empid)='" & empid & "'))


.......

end

Go

Executing the Stored Procedure:

sql = "EXECUTE QrySql '1175'"


Hope this helps you.
SS

Last edited by sonasonia : April 5th, 2004 at 02:50 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Combining multiple queries into one


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT