2011-01-25 86 views
0

我有一個SQL語句,如..有什麼辦法可以實現下面的代碼?

Select 
Res1, 
Res2, 
Res3, 
Res4 
from tbl1 
where Res1=1 
Group by Res2 
Having Res4>0 

我想甲肝最後一列,但該列是基於Res1Res3。個人陳述將

Select 
Res5 
from tbl1 
where Res1=1 and max(Res3) 
Group by Res2 
Having Res4>0 

我想合併Res5到第一個聲明。

Res3是可以通過執行第一條語句得到的總值。

我想要做的一樣..

Select 
Res1, 
Res2, 
Res3, 
Res4 
(Select 
Res5 
from tbl1 
where Res1=1 and max(Res3) 
Group by Res2 
Having Res4>0) 
from tbl1 
where Res1=1 
Group by Res2 
Having Res4>0 

但apperantly這是不正確的。

如何實現這一目標?

確切代碼將是

declare @noshow int, @target int; 
set @noshow=20; 
set @target=1200; 

select 
CONVERT(VARCHAR(10), visit.regdate, 103) as 'Date', 
datename(weekday, visit.regdate) as 'Day', 
count(queueno) as 'Total_Served', 
sum(case when datediff(second, starttime, nexttime)<= @target then 1 else 0 end) as 'Less_Target', 
isnull((sum(case when datediff(second, starttime, nexttime)<= @target then 1 else 0 end)*100)/count(queueno),0) as 'Less_Target_Per', 
sum(case when datediff(second, starttime, nexttime)> @target then 1 else 0 end) as 'More_Target', 
isnull((sum(case when datediff(second, starttime, nexttime)> @target then 1 else 0 end)*100)/count(queueno),0) as '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) as '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) as 'Max_Waiting_Time', 
CONVERT(VARCHAR(26), getdate(), 108) as 'Cus_Arrival' 
from visit 
where visit.branchno in ( '1007' ) and 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' ) and visit.catname in ('BY PASS' ,'REG STORE' ,'REGISTRATION' ,'ROOM 1 to 4' ,'ROOM A1 & A2' ,'ROOM A3 & A4' ,'ROOM A5-A7&A9-A11' ,'ROOM A8-BMD' ,'ROOM B20' ,'ROOM B21 B23 B24' ,'Ward Cases' ) and visit.btnname in ('BY PASS' ,'REG STORE' ,'REGISTRATION' ,'ROOM 1 to 4' ,'ROOM A1 & A2' ,'ROOM A3 & A4' ,'ROOM A5-A7&A9-A11' ,'ROOM A8-BMD' ,'ROOM B20' ,'ROOM B21' ,'B23' ,'B24' ,'Ward Cases' ) and (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')) and datediff(second,nexttime,endtime)>@noshow 
group by regdate 
having sum(case when datediff(second, starttime, nexttime)> @target then 1 else 0 end) >0 

忽略where子句。它太長了。

我想用第二個陳述替換Cus_Arrival

它實際上是到達時間Max_Waiting_Time

+1

你是不是想實現一個最大的n-per-group查詢?看看http://stackoverflow.com/questions/3442931/sql-server-select-distinct-rows-using-most-recent-value-only – batwad 2011-01-25 09:39:55

回答

0

我假設你使用的是MS SQL Server。

Select 
    Res1, 
    Res2, 
    Res3, 
    Res4 
from tbl1 
where Res1=1 
Group by Res2 
Having Res4>0 

無效。

您無法選擇不屬於組的組成部分的列。

閱讀有關組由這裏 http://msdn.microsoft.com/en-us/library/ms177673.aspx

在 列表中任何 非聚合表達式中的每個表或視圖的列必須列入 GROUP BY列表:

+0

這是一個示例代碼..它是有效的。看看我的實際陳述。 – william 2011-01-26 01:14:33

相關問題