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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Need to join two queries for output

Author  Topic 

limester
Starting Member

11 Posts

Posted - 2011-12-01 : 15:48:20
Hi there,

I need to join 2 different queries so that I can get a) get a count of something b) and the volume using sum. But I am not sure how to construct this query properly:

SELECT sum(convert(float,field1)/1024/1024/1024) from
tbl1
inner join tbl2
on tbl1.field2 = tbl2.field2
inner join tbl3
on tbl1.field3 = tbl3.field3
inner join tbl4
on tbl3.field4 = tbl4.field4
where tbl4.field4 in
(select field4 from field5 where (field6 between '20111101' and '20111130'))

select count(field5), field7 from tbl4 where field6 between '20111101' and '20111130'
group by field7

Any help would be greatly appreciated!

Cheers!

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2011-12-01 : 16:30:20
You question is not clear. What output you are expecting and problems with it.
Go to Top of Page

limester
Starting Member

11 Posts

Posted - 2011-12-01 : 17:24:26
Hi,

Sorry. If I break it out into the two queries above as q1 and q2

The output I am looking for from the query would be:

q2.field7 q2.count(field5) q1

so I am looking to get the total count of field5, grouped by field7 with the volume that was outputted in query 1.

Hope that helps?

Cheers!
Go to Top of Page

stans
Starting Member

1 Post

Posted - 2011-12-01 : 18:10:11
you mean inline views, you must give them aliases (q1,q2) then join on the common field (field7).
Go to Top of Page

limester
Starting Member

11 Posts

Posted - 2011-12-03 : 10:48:34
Hi,

Thanks so much! Would you be able to provide an example of an inline view?

Cheers!
Go to Top of Page
   

- Advertisement -