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)
 problems with subquery

Author  Topic 

fatih252
Starting Member

4 Posts

Posted - 2012-05-09 : 03:45:07
Hello

I have two queries where i have difficulties combining them. I need this two counts to make an average for my archive table.



select count(*) from tbl_app_monitoring as mon 
where mon.status = 'alive'
group by mon.application_id , mon.test_name



select count(*) from tbl_app_monitoring as mon , tbl_application as  app
where mon.application_id = app.id
group by mon.application_id , mon.test_name



this is my table:

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-09 : 07:12:42
what output do u want?
Go to Top of Page

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-05-09 : 08:17:20
Please post the sample data not as an image but as a String of insert statements so that its easy for the people here to use it.

What do you mean when you say, "I need this two counts to make an average for my archive table". Please elaborate.

Please post the sample data and the Expected Result Set.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page

fatih252
Starting Member

4 Posts

Posted - 2012-05-09 : 08:58:53
quote:
Originally posted by vinu.vijayan

Please post the sample data not as an image but as a String of insert statements so that its easy for the people here to use it.

What do you mean when you say, "I need this two counts to make an average for my archive table". Please elaborate.

Please post the sample data and the Expected Result Set.

N 28° 33' 11.93148"
E 77° 14' 33.66384"



thanks for your answers, i found the solution!:

select application_id, test_name,status,count(*) as number
into #temptb
from tbl_app_monitoring
group by application_id, test_name, status

select application_id , test_name,sum(number) as total
into #temptb2
from #temptb
group by application_id, test_name

insert into tbl_app_monitoring_archive (test_name,percentage_status,application_id)
select TT1.test_name, cast(((number*1.0) /(total*1.0))*100 as int),TT1.application_id from
#temptb TT1 inner join #temptb2 TT2
on TT1.application_id = TT2.application_id
where status = 'alive'
group by TT1.test_name, number , total,TT1.application_id [url][/url]
Go to Top of Page
   

- Advertisement -