|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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) |
|
#2
|
||||
|
||||
|
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
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
||||
|
||||
|
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 |
|
#6
|
|||
|
|||
|
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?? |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Join Multiple Select Statements with functions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|