我有一個表,我想類型的統計價格型和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? 對不起我可怕的英語。
thks但它沒有工作,它仍然只有相同類型(a到a,b到b),它不能求和(類型a和類型c),雖然他們有相同的日期 –
您是否嘗試對您的數據庫執行此操作? – Jade
哦,我的壞,我不認識有「GROUP BY TYPE,DAY」在我的代碼....它應該只有「GROUP BY DAY」.. thks你這麼多:D –