SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Merging Multiple SQL Queries
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dcarroll
Starting Member

3 Posts

Posted - 08/30/2011 :  09:39:10  Show Profile  Reply with Quote
I am new to SQL and programming in general, and I've found that it is easier to write large queries peicemeal and then combine the code by calling the queries in a main query as I need them.
What I'm curious to know is, once I've written my the code this way is there any easy way to combine the smaller queries into one large query?

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 08/30/2011 :  09:43:03  Show Profile  Reply with Quote
depending on your scenario you can use both UNION or JOIN operators for merging your small queries

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

gwilson67
Starting Member

USA
42 Posts

Posted - 08/30/2011 :  09:46:58  Show Profile  Visit gwilson67's Homepage  Reply with Quote
Union or Union All

Difference is that Union will remove the duplicates and Union All includes all records including duplicates.

Greg
http://www.freewebstore.org/tsqlcoderepository
Powerful tool for SQL Server development
Go to Top of Page

dcarroll
Starting Member

3 Posts

Posted - 08/30/2011 :  14:22:21  Show Profile  Reply with Quote
the " Main Query"

SELECT First(qryTom3.CruiseID) AS FirstOfCruiseID, First(qryTom3.SpeciesID) AS FirstOfSpeciesID, qryTom3.CommonName, First(qryTom3.EventNum) AS FirstOfEventNum1, Avg(IIf(IsNull([TotCt]),0,[TotCt])) AS Mean, StDev(IIf(IsNull([TotCt]),0,[TotCt])) AS StDev
FROM qryTom3 LEFT JOIN qryTom1 ON (qryTom3.SpeciesID = qryTom1.SpeciesID) AND (qryTom3.EventNum = qryTom1.EventNum)
GROUP BY qryTom3.CommonName;

qryTom3
SELECT Station.CruiseID, Station.EventNum, Species.SpeciesID, Species.CommonName, Species.SurveySpecies
FROM Species, Station
WHERE (((Species.SurveySpecies)="TRUE"));


qryTom1

SELECT Station.CruiseID, Station.EventNum, CatchSummary.SpeciesID, CatchSummary.TotCt
FROM Station INNER JOIN CatchSummary ON Station.EventNum=CatchSummary.EventNum;


My problem is that I can't figure out how to compile these queries into a single query
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 08/31/2011 :  00:20:48  Show Profile  Reply with Quote
are you using sql server? First is not a function in sql server

also can you show sample data from queries and then how you want to get merged output

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000