Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

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 December 7th, 2003, 11:32 AM
Petu Petu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 13 Petu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to Petu
How do I concatenate two select statements?

How do I concatenate two select statements? e.g. Select SELECT LastName,FirstName FROM Persons WITH SELECT SUM(Age) FROM Persons

Thanx

Reply With Quote
  #2  
Old December 7th, 2003, 01:24 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,696 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 16 h 40 m 48 sec
Reputation Power: 986
from your example, it looks like you want the lastname/firstname of every row in the table, as well as the sum of all their ages, which is just one row

there's only one way to do this, and you may not like it --
Code:
select ' ' as flag
     , lastname, firstname, age
  from persons 
union all
select 'total:'
     , ' ', ' ', sum(age) 
  from persons 
order
    by 1, 2, 3
note the flag is the main sort key, so all the lastname/firstname rows will sort first, and then the total row will come last

rudy

Reply With Quote
  #3  
Old December 7th, 2003, 02:02 PM
Petu Petu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 13 Petu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to Petu
Actually what I want to do is combine this two statements:

SqlJunk = " SELECT SUM(MSD+Methods+Analysis+Architectures+Models+Studio+Elective1+Elective2+Elective3) As PersonTotals FROM tblpersons "
SqlJunk = SqlJunk & " WHERE User_Id=" & Request.Form("DaInBox")

SqlJunk = "SELECT FirstName, LastName, WeekNo, ProgramName, StudioTeamName FROM tblpersons"
SqlJunk = SqlJunk & " WHERE User_Id=" & Request.Form("DaInBox")

Sorry for not being specific.
Thanx

Reply With Quote
  #4  
Old December 7th, 2003, 04:39 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,696 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 16 h 40 m 48 sec
Reputation Power: 986
judging from the WHERE clauses, and assuming that User_Id is unique, you do not need to use SUM(), which usually operates on multiple rows

that is, it will work on one row too, but there's no need to use it at all

therefore you can simple combine the SELECTs of both queries --
Code:
select msd + methods + analysis + architectures 
     + models + studio + elective1 + elective2 
     + elective3  as persontotals 
     , firstname, lastname, weekno, programname, studioteamname 
  from tblpersons
 where user_id = value

rudy

Reply With Quote
  #5  
Old December 7th, 2003, 09:11 PM
Petu Petu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 13 Petu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to Petu
WHEN I USE THIS STATEMENT(the one you send me)

If Request.Form("TypeSearch") = "User_Id" Then
SqlJunk = "SELECT MSD+Methods+Analysis+Architectures+Models+Studio+Elective1+Elective2+Elective3 As PersonTotals, FirstName, LastName, WeekNo, ProgramName, StudioTeamName FROM tblpersons"
SqlJunk = SqlJunk & " WHERE User_Id=" & Request.Form("DaInBox")
End If

I GET THE FOLLOWING RESULTS

Here are the results of your search:
FirstName LastName WeekNo ProgramName StudioTeamName
Maggy Lae 1 MSE PMC
PersonTotals =

PersonTotals doesn't have a value. I want to get the SUM of all those fields(which are numbers) and the Firstname.....are text.

Thanx

Reply With Quote
  #6  
Old December 7th, 2003, 09:51 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,696 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 16 h 40 m 48 sec
Reputation Power: 986
again, SUM is an aggregate function, operating on multiple rows

you just want simple addition

now, one thing that could be causing your problem is that one of the values is null

okay, therefore, if you want to add them up, you have to guard against nulls --
Code:
select coalesce(MSD,0)
     + coalesce(Methods,0)
     + coalesce(Analysis,0)
     + coalesce(Architectures,0)
     + coalesce(Models,0)
     + coalesce(Studio,0)
     + coalesce(Elective1,0)
     + coalesce(Elective2,0)
     + coalesce(Elective3,0)  As PersonTotals
     , firstname
    ...
rudy

Reply With Quote
  #7  
Old December 8th, 2003, 01:28 AM
Petu Petu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 13 Petu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to Petu
Thankx a lot....looks like I was only missing to write PersonTotals from the Recordset....I included another column (PersonTotals) so that it can write the totals there......the following statement worked out perfectly.

Here is what I get:

Here are the results of your search:
FirstName LastName WeekNo ProgramName TeamName Total
Maggy Lae 1 MSE PMC 47




Thankx again

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > How do I concatenate two select statements?


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