| Author |
Topic |
|
nhess80
Yak Posting Veteran
83 Posts |
Posted - 2011-07-06 : 16:28:00
|
| Hi,I am trying to figure out how I can Query the results of another query. Is this possible?Thanks,NickExampleSelect *FROM TABLEWHERE Column = 1SELECT *FROM RESULTS of First Query |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2011-07-06 : 17:13:57
|
What is the difference between those two result sets? Aren't they the same?However, one way to answer your question is by using a derived table:select [type], count(*) [typeCount]from ( select id, name, [type] from sysobjects ) dgroup by [type]order by 2 desc Be One with the OptimizerTG |
 |
|
|
nhess80
Yak Posting Veteran
83 Posts |
Posted - 2011-07-06 : 18:16:52
|
quote: Originally posted by TG What is the difference between those two result sets? Aren't they the same?However, one way to answer your question is by using a derived table:select [type], count(*) [typeCount]from ( select id, name, [type] from sysobjects ) dgroup by [type]order by 2 desc Be One with the OptimizerTG
Well what Im trying to do is thisSELECT SUM(RoundDuration) AS Duration,COUNT(CASE WHEN Duration > '0' THEN '1' END) AS Completes,SUM(Revenue) / SUM(RoundDuration) AS RateFROM CDRSBut I want to group by SUM(Revenue) / SUM(RoundDuration)which I am not able to...so i was just trying to find a way around that and i thought that if i ran my first query then queried the results like:Select SUM(Duration),SUM(Completes),RateFROM First_QueryGroup by Rate |
 |
|
|
nhess80
Yak Posting Veteran
83 Posts |
Posted - 2011-07-06 : 18:17:37
|
quote: Originally posted by tkizer You can use it as a derived table, like this:SELECT * FROM(SELECT Column1, Column2 FROM Table1 WHERE ...) dtWHERE ...Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
Well what Im trying to do is thisSELECT SUM(RoundDuration) AS Duration,COUNT(CASE WHEN Duration > '0' THEN '1' END) AS Completes,SUM(Revenue) / SUM(RoundDuration) AS RateFROM CDRSBut I want to group by SUM(Revenue) / SUM(RoundDuration)which I am not able to...so i was just trying to find a way around that and i thought that if i ran my first query then queried the results like:Select SUM(Duration),SUM(Completes),RateFROM First_QueryGroup by Rate |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
ankie
Starting Member
2 Posts |
Posted - 2011-07-06 : 23:03:55
|
| tkizer already provided you with the solution. you just need to apply your query into the solution.SELECT * FROM(SELECT Column1, Column2 FROM Table1 WHERE ...) dtWHERE ...Select Rate, sum(duration), sum(Completes) from(SELECT SUM(RoundDuration) AS Duration,COUNT(CASE WHEN Duration > '0' THEN '1' END) AS Completes,SUM(Revenue) / SUM(RoundDuration) AS RateFROM CDRS) dtgroup by Rate |
 |
|
|
nhess80
Yak Posting Veteran
83 Posts |
Posted - 2011-07-07 : 13:42:16
|
| Thank you very much |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|