MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
The Best Selling PC Migration Utility.
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:
Dell PowerEdge Servers
  #1  
Old December 13th, 2003, 05:19 PM
syris syris is offline
A fiend for the SQL Scene
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Ft myers
Posts: 5 syris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to syris
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)

Reply With Quote
  #2  
Old December 14th, 2003, 01:24 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,733 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 20 h 11 m 38 sec
Reputation Power: 869
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?

Reply With Quote
  #3  
Old December 14th, 2003, 11:38 AM
syris syris is offline
A fiend for the SQL Scene
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Ft myers
Posts: 5 syris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to syris
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.

Reply With Quote
  #4  
Old December 14th, 2003, 11:49 AM
syris syris is offline
A fiend for the SQL Scene
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Ft myers
Posts: 5 syris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to syris
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.

Reply With Quote
  #5  
Old December 14th, 2003, 12:18 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,733 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 20 h 11 m 38 sec
Reputation Power: 869
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

Reply With Quote
  #6  
Old December 14th, 2003, 03:14 PM
syris syris is offline
A fiend for the SQL Scene
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Ft myers
Posts: 5 syris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to syris
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??

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Join Multiple Select Statements with functions


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway