sql server - Is there any way to achieve the following code? -


i have sql statement like..

select res1, res2, res3, res4 tbl1 res1=1 group res2 having res4>0 

i wanna hav final column column based on res1 , res3. individual statement be

select res5 tbl1 res1=1 , max(res3) group res2 having res4>0 

i wanna merge res5 first statement.

res3 total value can b got executing first statement.

i wanna like..

select res1, res2, res3, res4 (select res5 tbl1 res1=1 , max(res3) group res2 having res4>0) tbl1 res1=1 group res2 having res4>0 

but apperantly not correct.

how achieve that?

the exact code be

declare @noshow int, @target int;  set @noshow=20; set @target=1200;  select convert(varchar(10), visit.regdate, 103) 'date', datename(weekday, visit.regdate) 'day', count(queueno) 'total_served', sum(case when datediff(second, starttime, nexttime)<= @target 1 else 0 end) 'less_target', isnull((sum(case when datediff(second, starttime, nexttime)<= @target 1 else 0 end)*100)/count(queueno),0) 'less_target_per', sum(case when datediff(second, starttime, nexttime)> @target 1 else 0 end) 'more_target', isnull((sum(case when datediff(second, starttime, nexttime)> @target 1 else 0 end)*100)/count(queueno),0) 'more_target_per', isnull(convert(varchar(6), avg(datediff(second,nexttime,endtime))/3600)+ ':' + right('0' + convert(varchar(2), (sum(datediff(second,nexttime,endtime)) % 3600) / 60), 2)+ ':' + right('0' + convert(varchar(2), sum(datediff(second,nexttime,endtime)) % 60), 2),0) 'avg_serving_time', isnull(convert(varchar(6), max(datediff(second,starttime,nexttime))/3600)+ ':' + right('0' + convert(varchar(2), (max(datediff(second,starttime,nexttime)) % 3600) / 60), 2)+ ':' + right('0' + convert(varchar(2), max(datediff(second,starttime,nexttime)) % 60), 2),0) 'max_waiting_time', convert(varchar(26), getdate(), 108) 'cus_arrival' visit  visit.branchno in (  '1007'    )  ,  visit.wstation in ('1'  ,'10'  ,'11'  ,'15'  ,'2'  ,'20'  ,'21'  ,'23'  ,'24'  ,'28'  ,'29'  ,'3'  ,'30'  ,'31'  ,'32'  ,'33'  ,'4'  ,'5'  ,'6'  ,'7'  ,'8'  ,'9'    )  , visit.catname in ('by pass'  ,'reg store'  ,'registration'  ,'room 1 4'  ,'room a1 & a2'  ,'room a3 & a4'  ,'room a5-a7&a9-a11'  ,'room a8-bmd'  ,'room b20'  ,'room b21 b23 b24'  ,'ward cases'    )  , visit.btnname in ('by pass'  ,'reg store'  ,'registration'  ,'room 1 4'  ,'room a1 & a2'  ,'room a3 & a4'  ,'room a5-a7&a9-a11'  ,'room a8-bmd'  ,'room b20'  ,'room b21'  ,'b23'  ,'b24'  ,'ward cases'  )   , (convert(varchar(10), visit.tmstamp, 111) in(  '2010/11/01'     ,'2010/11/02'     ,'2010/11/03'     ,'2010/11/04'     ,'2010/11/05'     ,'2010/11/06'     ,'2010/11/07'     ,'2010/11/08'     ,'2010/11/09'     ,'2010/11/10'     ,'2010/11/11'     ,'2010/11/12'     ,'2010/11/13'     ,'2010/11/14'     ,'2010/11/15'     ,'2010/11/16'     ,'2010/11/17'     ,'2010/11/18'     ,'2010/11/19'     ,'2010/11/20'     ,'2010/11/21'     ,'2010/11/22'     ,'2010/11/23'     ,'2010/11/24'     ,'2010/11/25'     ,'2010/11/26'     ,'2010/11/27'     ,'2010/11/28'     ,'2010/11/29'     ,'2010/11/30'     ,'2010/12/01'     ,'2010/12/02'     ,'2010/12/03'     ,'2010/12/04'     ,'2010/12/05'     ,'2010/12/06'     ,'2010/12/07'     ,'2010/12/08'     ,'2010/12/09'     ,'2010/12/10'     ,'2010/12/11'     ,'2010/12/12'     ,'2010/12/13'     ,'2010/12/14'     ,'2010/12/15'     ,'2010/12/16'     ,'2010/12/17'     ,'2010/12/18'     ,'2010/12/19'     ,'2010/12/20'     ,'2010/12/21'     ,'2010/12/22'     ,'2010/12/23'     ,'2010/12/24'     ,'2010/12/25'     ,'2010/12/26'     ,'2010/12/27'     ,'2010/12/28'     ,'2010/12/29'     ,'2010/12/30'     ,'2010/12/31' )) ,  datediff(second,nexttime,endtime)>@noshow  group regdate having sum(case when datediff(second, starttime, nexttime)> @target 1 else 0 end) >0 

ignore clause. long.

i wanna replace cus_arrival second statement.

it arrival time max_waiting_time.

i assume using ms sql server.

select     res1,     res2,     res3,     res4 tbl1 res1=1 group res2 having res4>0 

is not valid.

you can not select columns not part of group by.

read group here http://msdn.microsoft.com/en-us/library/ms177673.aspx

each table or view column in nonaggregate expression in list must included in group list:


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -