作者laler (Jacky)
看板Database
标题[SQL ] PIVOT 语法问题
时间Wed Nov 14 13:01:59 2018
资料库名称:Ms-SQL
资料库版本:2008
内容/问题描述:
资料Table A
Orderno Time Price
-------------------------------
2018110101 20181101 100
2018110102 20181101 200
2018110201 20181102 300
2018110202 20181102 400
资料Table B
Orderno 付款方式 付款金额
-------------------------------
2018110101 现金 100
2018110102 信用卡 100
2018110102 现金 100
2018110201 现金 300
2018110202 信用 400
想要达查询是
日期 金额 现金 信用卡
-------------------------------
20181101 300 200 100
20181102 700 300 400
用Access语法可以成功
transform 付款金额
Select 日期,Sum(Price) from A join B on A.orderno = B.orderno
Group by 日期
PIVOT 付款方式
但是SQL语法我试出来的结果是
日期 金额 现金 信用卡
-------------------------------
20181101 200 200 null
20181101 100 null 100
20181102 300 300 null
20181102 400 null 400
以下是我目前的语法
Select * from
(Select 日期,Sum(Price),Sum(付款金额)
from A join B on A.orderno = B.orderno
Group by 日期,付款方式
) as temp
PIVOT (SUM(付款金额)
FOR 付款方式
IN ([现金],[信用卡]))
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.135.30.56
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1542171721.A.EEA.html