2013-12-19 148 views
0

我有一個表,我想類型的統計價格型和C型爲賣出,B類和d爲買進天的結果類似SUM所有字段具有相同類型值的SQL Server

Table : 
    | Name | type | price | day  | 
    | n1 | a  |  1 |2013-10-10 | 
    | n2 | b  |  2 |2013-10-10 | 
    | n3 | c  |  3 |2013-10-10 | 
    | n4 | d  |  4 |2013-10-10 | 
    | n1 | a  |  5 |2013-11-10 | 
    | n2 | b  |  6 |2013-11-10 | 
    | n3 | c  |  7 |2013-11-10 | 
    | n4 | d  |  8 |2013-11-10 | 

我想造成這樣

| DAY  | SELL (price a+price c) | BUY (price b +price d) | 
|2013-10-10 |  4      |   6    | 
|2013-11-10 |  12     |   14    | 

這裏我的代碼

ALTER proc [dbo].[sp_mysp] 
@starttime datetime, 
@endtime datetime 
as 
    DECLARE @TIME DATETIME 
    SET @[email protected]@STARTTIME 
    SELECT (table.day) AS DAY , case BUY 
    WHEN TABLE.TYPE ='a' or TABLE.TYPE = 'c' THEN SUM (price) end, 
    CASE SELL 
    WHEN TABLE.TYPE = 'b' or TABLE.TYPE = 'd' THEN SUM (price) end, 

     FROM TABLE WHERE TABLE.DAY>@starttime AND TABLE.TIME<@ENDTIME 
     order by TABLE.DAY 


GO 

我無法總結(A型和C型),和(b型和類型d),因爲我想,它只能通過類型與綜上所述,B與B ...... ,我也得到類似結果

| DAY  | SELL (price a+price c) | BUY (price b +price d) | 
| 2013-10-10 |   1     |  NULL    | 
| 2013-10-10 |   3     |  NULL    | 
| 2013-10-10 |   NULL    |  2     | 
| 2013-10-10 |   NULL    |  4     | 
| 2013-11-10 |   5     |  NULL    | 
| 2013-11-10 |   7     |  NULL    | 
| 2013-11-10 |  NULL     |  6     | 
| 2013-11-10 |  NULL     |  8     | 

我任何sugesstion? 對不起我可怕的英語。

回答

1

試試這個

我沒有看到時間在你的表,但我猜列日是日期格式

ALTER proc [dbo].[sp_mysp] 
@starttime datetime, 
@endtime datetime 
as  
SELECT [Day], 
     SUM(CASE WHEN [Type] = 'a' OR [Type] = 'c' THEN price ELSE 0.00 END) SELL, 
     SUM(CASE WHEN [Type] = 'b' OR [Type] = 'd' THEN price ELSE 0.00 END) BUY 
FROM [TABLE] 
WHERE [Day] BETWEEN @starttime AND @endtime 
GROUP BY [Day] 
+0

thks但它沒有工作,它仍然只有相同類型(a到a,b到b),它不能求和(類型a和類型c),雖然他們有相同的日期 –

+0

您是否嘗試對您的數據庫執行此操作? – Jade

+0

哦,我的壞,我不認識有「GROUP BY TYPE,DAY」在我的代碼....它應該只有「GROUP BY DAY」.. thks你這麼多:D –

-2

我覺得你

TABLE.TYPE ='a' or TABLE.TYPE = 'c' 

應該

TABLE.TYPE ='a' AND TABLE.TYPE = 'c' 

這樣的話,那就只能和。如果兩者都是你所需要的。

+0

不行,結果恢復= 0(因爲任何類型的名只能是「a」或「b」或「c」或「d」 –

+0

它不能工作:)它不可能= a和= c :) –

0
ALTER proc [dbo].[sp_mysp] 
@starttime datetime, 
@endtime datetime 
as 
    DECLARE @TIME DATETIME 
    SET @[email protected]@STARTTIME 
    SELECT (table.day) AS DAY , SUM (case BUY 
    WHEN TABLE.TYPE ='a' or TABLE.TYPE = 'c' THEN price else 0 end) Buy, 
    SUM (CASE SELL 
    WHEN TABLE.TYPE = 'b' or TABLE.TYPE = 'd' THEN price else 0 end) Sell, 

     FROM TABLE WHERE TABLE.DAY>@starttime AND TABLE.TIME<@ENDTIME 
     order by TABLE.DAY 


GO 

試試這個,在所有情況下,你想總和,它只是在達到條件要價格添加到結果否則你忽略它。對我來說,案件應當在總和本身功不可沒外

對不起編輯,但我不能評論:)

我做了一個試驗那就是:

declare @table table (dayss int, typess char(1), price money) 
insert into @table values (1,'a', 2.00) 
insert into @table values (1,'c', 5.00) 
insert into @table values (1,'b', 3.00) 
insert into @table values (1,'d', 3.00) 
insert into @table values (2,'a', 20.00) 
insert into @table values (2,'c', 50.00) 
insert into @table values (2,'b', 30.00) 
insert into @table values (2,'d', 30.00) 

SELECT [Dayss], 
     SUM(CASE WHEN [Typess] = 'a' OR [Typess] = 'c' THEN price ELSE 0.00 END) SELL, 
     SUM(CASE WHEN [Typess] = 'b' OR [Typess] = 'd' THEN price ELSE 0.00 END) BUY 
FROM @table 
WHERE [Dayss] BETWEEN 1 AND 2 
GROUP BY [Dayss] 

我的結果是正確的:

1 7.0000 6.0000 2 70.0000 60.0000

因此,也許我不明白你想要或者別的什麼

+0

thks但它不起作用,它仍然只有相同類型a,b到b),它不能總結(類型a和類型c),雖然他們有同一天 –

+0

看看以前的帖子,對我來說它的工作,我誤解了一些東西? –

+0

yu,現在工作,你這麼多:D –

相關問題