iT邦幫忙

0

請問,sql指令中,可以將查詢結果,加入一行嗎?

sql

請問,sql語法要產生像圖檔中的表格時,由於現金、刷卡、其他三段語法都很像,只是其中where子句參數不同,請問有指令可以將查詢結果加一行(刷卡、其他)嗎?

select 指令
按個 讚
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
danielp2
iT邦新手 4 級 ‧ 2016-04-14 16:46:56

可以試用<or>嗎?

SELECT * FROM ABC where A=現金 or A=刷卡 or A=其他

賽門 iT邦超人 1 級 ‧ 2016-04-15 12:50:38 檢舉

無言

2
純真的人
iT邦大師 1 級 ‧ 2016-04-15 06:50:38

try

&lt;pre class="c" name="code">
select 日期
,Sum(現金) as 現金
,Sum(刷卡) as 刷卡
,Sum(其他) as 其他
from ((
	select 日期
	,現金
	,0 as 刷卡
	,0 as 其他
	where 現金 > 0
)union(
	select 日期
	,0 as 現金
	,刷卡
	,0 as 其他
	where 刷卡 > 0
)union(
	select 日期
	,0 as 現金
	,0 as 刷卡
	,其他
	where 其他 > 0
)) as k
group by 日期

少了資料表..

&lt;pre class="c" name="code">
select 日期
,Sum(現金) as 現金
,Sum(刷卡) as 刷卡
,Sum(其他) as 其他
from ((
	select 日期
	,現金
	,0 as 刷卡
	,0 as 其他
	from 資料表
	where 現金 > 0
)union(
	select 日期
	,0 as 現金
	,刷卡
	,0 as 其他
	from 資料表
	where 刷卡 > 0
)union(
	select 日期
	,0 as 現金
	,0 as 刷卡
	,其他
	from 資料表
	where 其他 > 0
)) as k
group by 日期

我要發表回答

立即登入回答