2012-03-22 78 views
2

我有一個表如下。SQL跨標籤樞軸查詢

CaseID StatusID StageID  CaseRegisterTime City 
1  1   5   datetime   XYZ 
2  1   5   datetime   ABC 

現在我想它Citywise計數,僅只有特定的日期,以及在條件statusid = 1級ID = 5

Cities  CurrentDate-1 CurrentDate-2 January2012-CurrentDate-3 
XYZ   5     51     5008 
JUS   0     0     125 
ABC   1     0     48 

我希望我的頭組病例爲CaseRegisterTime爲如上所示。

請幫助。

+0

嘗試使用Google搜索數據透視表查詢SQL您會發現很多您正在嘗試執行的示例。 – Brian 2012-03-22 13:47:27

+0

我試過擺動,但主要問題是日期列,我只有限制,如上面的示例輸出限制日期不想查找所有其他日期。 – 2012-03-22 13:51:18

+2

你從哪裏得到日期?一般來說,當我們問「你有什麼嘗試?」我們希望看到代碼 – 2012-03-22 13:54:36

回答

0

使用case when您感興趣的日期轉換爲'CurrentDate-1''CurrentDate-2',然後轉動的結果,使用這個字符串作爲新列。

或者,你可以做這樣的事情:

select City, sum(Date1) as Date1, sum(Date2) as Date2 
from(
select City, 
    case when CaseRegisterTime='2012-01-01' then 1 else 0 end as Date1, 
    case when CaseRegisterTime='2012-15-01' then 1 else 0 end as Date2 
from sample 
) as T 
group by City 

你也不得不過濾掉不具有所需的日期寄存器。

0

這裏有很多方法可以做到這一點在SQL Server 2008(使用日期數據類型)之一:

select distinct a.City as Cities 
    , (select count(*) 
     from MyTable 
     where CaseRegisterTime >= cast(getdate() - 1 as date) 
      and CaseRegisterTime < cast(getdate() - 0 as date) 
      and StatusID = a.StatusID 
      and StageID = a.StageID 
      and City = a.City 
    ) as [CurrentDate-1] 
    , (select count(*) 
     from MyTable 
     where CaseRegisterTime >= cast(getdate() - 2 as date) 
      and CaseRegisterTime < cast(getdate() - 1 as date) 
      and StatusID = a.StatusID 
      and StageID = a.StageID 
      and City = a.City 
    ) as [CurrentDate-2] 
    , (select count(*) 
     from MyTable 
     where CaseRegisterTime >= cast('20120101' as date) 
      and CaseRegisterTime < cast(getdate() - 2 as date) 
      and StatusID = a.StatusID 
      and StageID = a.StageID 
      and City = a.City 
    ) as [January2012-CurrentDate-3] 
from MyTable a 
where a.StatusID = 1 
    and a.StageID = 5 

更新

的情況下,與和方法@JotaBe採用的是約快兩倍在我的箱子(與許多欠掃描和讀取),所以這裏的可能是什麼樣子:

select a.City as Cities 
    , sum(a.[CurrentDate-1]) as [CurrentDate-1] 
    , sum(a.[CurrentDate-2]) as [CurrentDate-2] 
    , sum(a.[January2012-CurrentDate-3]) as [January2012-CurrentDate-3] 
from (
    select City 
     , case when CaseRegisterTime >= cast(getdate() - 1 as date) 
       and CaseRegisterTime < cast(getdate() - 0 as date) 
       then 1 else 0 end [CurrentDate-1] 
     , case when CaseRegisterTime >= cast(getdate() - 2 as date) 
       and CaseRegisterTime < cast(getdate() - 1 as date) 
       then 1 else 0 end [CurrentDate-2] 
     , case when CaseRegisterTime >= cast('20120101' as date) 
       and CaseRegisterTime < cast(getdate() - 2 as date) 
       then 1 else 0 end [January2012-CurrentDate-3] 
    from MyTable 
    where StatusID = 1 
     and StageID = 5 
) as a 
group by a.City 
0

像這樣的東西會做什麼:

begin tran; 
go 
create table #t1(
    ID int identity, 
    City varchar, 
    RegisterDate datetime 
); 
declare @firstDate datetime, @secondDate datetime; 
set @firstDate = '2012-1-1'; 
set @secondDate = '2012-1-2'; 
insert into #t1 values 
    ('A', @firstDate), 
    ('A', @firstDate), 
    ('B', @firstDate), 
    ('B', @firstDate), 
    ('B', @firstDate), 
    ('A', @secondDate), 
    ('A', @secondDate), 
    ('A', @secondDate), 
    ('B', @secondDate), 
    ('B', @secondDate); 

select * from #t1; 

select pvt.* 
from(
    select ID, City, RegisterDate 
    from #t1 
) a 
pivot(
    count(a.ID) 
    for a.RegisterDate in ([2012-1-1], [2012-1-2]) 
) as pvt; 

drop table #t1; 
go 
rollback tran;