Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Merging Multiple SQL Queries

Author  Topic 

dcarroll
Starting Member

3 Posts

Posted - 2011-08-30 : 09:39:10
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

52326 Posts

Posted - 2011-08-30 : 09:43:03
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

42 Posts

Posted - 2011-08-30 : 09:46:58
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 - 2011-08-30 : 14:22:21
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

52326 Posts

Posted - 2011-08-31 : 00:20:48
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
   

- Advertisement -