2011-08-23 85 views
0

我有兩個表說表-A和表-B問題加入

表-A具有

Userid | Date 
| 
    101  | 15 Aug ,2011 
    102  | 15 Aug ,2011 
    103  | 16 Aug ,2011 
    104  | 16 Aug ,2011 
    105  | 17 Aug ,2011 

表-B

Userid(f.k) | sts 
    101  | x 
    102  | y 
    101  | z 
    103  | x 
    101  | y 

需要的輸出是

Table-C 我想表c的輸出爲 DAte,Total,Sts爲X,STS爲y的

Date | Total | Sts (if=x) |Sts (if=y) |Avg (Total/2) 

15 Aug| 20 | 15  |5   |10  
16aug | 30 | 22  |8   |15 

魅力我試着使用:

select Date, select case when [Tasble-B].Sts='x' Then 0 else 1 END as StsX,select case when [Tasble-B].Sts='Y' Then 0 else 1 END as StsY,Total/2 
from 
    Table-A 
    inner join Table-B on Table-A.UserID = Table-B.UserID   
    b group by RegDate 

我得到的錯誤,我與這個query.Please幫助滯留在提前 感謝

+0

總數和平均是什麼場/表的總和與平均值? Sts(if = x)是什麼意思?你已經嘗試過什麼方法? –

+0

@paolo:請現在閱讀我的問題。和答案 – rakeshwalia

回答

2
select Date, 
     select case when [Tasble-B].Sts='x' Then 0 
        else 1 
       END as StsX, 
     select case when [Tasble-B].Sts='Y' Then 0 
        else 1 
       END as StsY, 
     Total/2 
from Table-A 
    inner join Table-B 
    on Table-A.UserID = Table-B.UserID 
b group by RegDate 
  1. 你這樣做group by RegDate所以我假設你應該在字段列表中使用RegDate而不是Date或者也許是RegDate as Date
  2. 您錯過了整個專欄。你可以做到這一點與count(*) as Total
  3. select case ...語句不應該選擇的,但sum(case ...)
  4. Total/2,來自哪裏Total?我的猜測是,您需要StsXStsY的平均值,但這也可能意味着您要使用計算的Total列並將其除以2.如果是這種情況,您需要重複彙總語句count(*)/2或者您可以使用此整個查詢作爲子查詢並且使用字段Total進行計算,就像您已經做的那樣,Total/2
  5. 您的表名需要封裝在括號內[Table-B]
  6. 您在group by條款之前有額外的b。你應該刪除它。

`

1

你可以試試:

select [Date], 
     (SUM(case when B.Sts='x' Then 1 
        else 0 
       END) + 
     SUM(case when B.Sts='Y' Then 1 
        else 0 
       END)) AS Total, 
     SUM(case when B.Sts='x' Then 1 
        else 0 
       END) AS StsX, 
     SUM(case when B.Sts='Y' Then 1 
        else 0 
       END) as StsY, 
     (SUM(case when B.Sts='x' Then 1 
        else 0 
       END) + 
     SUM(case when B.Sts='Y' Then 1 
        else 0 
       END)) 
     /2.0 AS Average 
from A 
    inner join B 
    on A.UserID = B.UserID 
group by [Date] 

我測試了它:

WITH A (UserId, [Date]) AS ( 
SELECT 101, '2011-08-15' 
UNION 
SELECT 102, '2011-08-15' 
UNION 
SELECT 103, '2011-08-16' 
UNION 
SELECT 104, '2011-08-16' 
UNION 
SELECT 105, '2011-08-17' 
), 
B (UserId, sts) AS 
(
    SELECT 101, 'x' 
    UNION 
    SELECT 102, 'y' 
    UNION 
    SELECT 101, 'z' 
    UNION 
    SELECT 103, 'x' 
    UNION 
    SELECT 101, 'y' 
)  
select [Date], 
     (SUM(case when B.Sts='x' Then 1 
        else 0 
       END) + 
     SUM(case when B.Sts='Y' Then 1 
        else 0 
       END)) AS Total, 
     SUM(case when B.Sts='x' Then 1 
        else 0 
       END) AS StsX, 
     SUM(case when B.Sts='Y' Then 1 
        else 0 
       END) as StsY, 
     (SUM(case when B.Sts='x' Then 1 
        else 0 
       END) + 
     SUM(case when B.Sts='Y' Then 1 
        else 0 
       END)) 
     /2.0 AS Average 
from A 
    inner join B 
    on A.UserID = B.UserID 
group by [Date] 
+0

+1 - 你太善良:) –

+1

@Mikael:不,只是懶惰......我認爲你花更長的時間來寫你的答案(我希望rakeshwalia花時間閱讀它) –