The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MS SQL Development
|
Join Multiple Select Statements with functions
Discuss Join Multiple Select Statements with functions in the MS SQL Development forum on Dev Shed. Join Multiple Select Statements with functions MS SQL Development forum discussing administration, MS SQL queries, and other MS SQL-related topics. SQL Server is Microsoft's enterprise database engine.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 13th, 2003, 05:19 PM
|
|
A fiend for the SQL Scene
|
|
Join Date: Nov 2003
Location: Ft myers
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Join Multiple Select Statements with functions
Hello,
MS SQL 2000 & ASP Question:
How would I join these multiple select statements so i can create one select query to use with my sub call without using outer join?
call query2table("SELECT SUM([Customers]) as [# of Cust] FROM (SELECT COUNT(*) AS [Customers] FROM Customers WHERE (active = 1) and (RepID = 4) union SELECT COUNT(*) AS [Customers] FROM Customers WHERE (active = 1) and (RepID = 4)) as aPlus")
call query2table("SELECT COUNT(*) AS [Customers] FROM Customers WHERE active = '1' and RepID = '4' and RecruiterID = '4'")
call query2table("SELECT SUM([Reps]) as [# of Reps] FROM (SELECT COUNT(*) AS [Reps] FROM RepDetail WHERE (active = 1) and (RecruiterID = 4) union SELECT COUNT(*) AS [Reps] FROM RepDetail WHERE (active = 1) and (RecruiterID = 4)) as aPlus")
call query2table("Select RecruiterID as 'Recruiter #',RepID as 'Rep #',FirstName,LastName,CompanyName,State,HomePhone,WorkPhone from RepDetail where Active = '1' and RecruiterID = '4'")
Thanks,
Russell
(rusty@streetmadness.com)
|

December 14th, 2003, 01:24 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
something is very, very wrong, but i'm not sure what it is
let's take a look at the first of your queries:
Code:
SELECT SUM([Customers]) as [# of Cust]
FROM (
SELECT COUNT(*) AS [Customers]
FROM Customers
WHERE (active = 1)
and (RepID = 4)
union
SELECT COUNT(*) AS [Customers]
FROM Customers
WHERE (active = 1)
and (RepID = 4)
) as aPlus
look at the SELECT, FROM, and WHERE clauses in two parts of the UNION -- they're identical!
furthermore, since there's no GROUP BY in either, each of the two parts of the UNION will produce exactly one row
now, since it's UNION, and not UNION ALL, and since the two rows will be identical, therefore the UNION will eliminate one of them
thus the SUM([Customers]) in the outer query is summing just one value, so that's not necessary, and the entire thing can be reduced as follows:
Code:
select count(*) as [customers]
from customers
where active = 1
and repid = 4
could you explain a bit more what you're trying to do?
|

December 14th, 2003, 11:38 AM
|
|
A fiend for the SQL Scene
|
|
Join Date: Nov 2003
Location: Ft myers
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Using 2 tables: Customers, RepDetail
What im trying to do is create a report for users logged into their backoffice and this will display information like how many reps they have signed up or how many customers they have. I just need 4 Selects statements into one so I can run a sub call and it will create the table with the variables.
Last edited by syris : December 14th, 2003 at 11:50 AM.
|

December 14th, 2003, 11:49 AM
|
|
A fiend for the SQL Scene
|
|
Join Date: Nov 2003
Location: Ft myers
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Ok I noticed the problems also and I will admit Im very new to sql but here is what I have so far:
call query2table("SELECT COUNT(*) AS [# of Cust:] FROM Customers WHERE active = '1' and RepID = '4'")
call query2table("SELECT COUNT(*) AS [Total Cust:] FROM Customers WHERE active = '1' and RepID = '4' and RecruiterID = '4'")
call query2table("SELECT COUNT(*) AS [# of Reps:] FROM RepDetail WHERE active = '1' and RecruiterID = '4'")
call query2table("Select RecruiterID as 'Recruiter #:',RepID as 'Rep #:',FirstName as 'First Name:',LastName as 'Last Name:',CompanyName as 'Company Name:',State as 'State:',HomePhone As 'Phone#:',WorkPhone as 'Work Phone#:' from RepDetail where Active = '1' and RecruiterID = '4'")
How can I join these 4 select statements pulling from 2 tables?
Last edited by syris : December 14th, 2003 at 11:56 AM.
|

December 14th, 2003, 12:18 PM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
|
you cannot combine these 4 queries into one, not in mysql
you can combine the first two
but you cannot combine either of the first two with the third or fourth, and you cannot combine the third and the fourth
look at the FROM and WHERE clauses of the third and fourth -- identical
the third query is simply a count of the number of detail records produced by the fourth
|

December 14th, 2003, 03:14 PM
|
|
A fiend for the SQL Scene
|
|
Join Date: Nov 2003
Location: Ft myers
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Ok I figured it out here is what I have now:
call query2table1("SELECT RepDetail.RepID as 'RepID:', RepDetail.FirstName as 'First Name:', RepDetail.LastName as 'Last Name:', RepDetail.State as 'State:', RepDetail.HomePhone as 'Home#:', RepDetail.WorkPhone as 'Work#:', RepDetail.Email as 'Email:', COUNT(RepDetail.RepID) AS [# of Reps:], COUNT(*) AS [# of Cust:], COUNT(Customers.RecruiterID) AS [Total Cust:] FROM Customers LEFT JOIN RepDetail ON Customers.RepID=RepDetail.RepID WHERE RepDetail.Active = '1' and RepDetail.RepID = '" & repsids & "' or Customers.RepID = '4' GROUP BY RepDetail.RepID, RepDetail.FirstName, RepDetail.LastName, RepDetail.State, RepDetail.HomePhone, RepDetail.WorkPhone, RepDetail.Email;")
Response.write "<br>"
call query2table2("Select RepDetail.RecruiterID as 'Recr#:',RepDetail.RepID as 'RepID:',RepDetail.FirstName as 'First Name:',RepDetail.LastName as 'Last Name:',RepDetail.CompanyName as 'Company:',RepDetail.State as 'State:',RepDetail.HomePhone As 'Phone#:',RepDetail.WorkPhone as 'Work Phone#:' FROM RepDetail WHERE Active = '1' and RecruiterID = '" & repsids & "' GROUP BY RepDetail.RecruiterID, RepDetail.RepID, RepDetail.FirstName, RepDetail.LastName, RepDetail.CompanyName, RepDetail.State, RepDetail.HomePhone, RepDetail.WorkPhone")
Now I need to create 3 more report tables pulling info from the previous query starting with query2table2. Is this possible and how would I do something like that??
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|