[SQL ] SQL 的查詢問題

看板Database作者 (仁武田信玄)時間11年前 (2015/02/05 16:16), 編輯推噓3(301)
留言4則, 2人參與, 最新討論串1/1
資料庫名稱: 資料庫版本: MSSQL2008 內容/問題描述: 目前遇到一個指令的問題 不知道要如何下 以簡單的圖來表是大概就是長這個樣子 http://i.imgur.com/yF4D2lf.png
因為單子有分出貨跟退貨 導致必須在總和之前在數量做正負的分別 已經想了一陣子了 希望版上的人可以幫忙 目前語法如下 select os_itno,os_crno,os_szno,os_thname,os_bdno ,sum (os_amount) ,case when os_trnmode in ('04') then -1*os_amount else 1*os_amount end from odrinvos where (os_trnmode ='03' OR os_trnmode = '04') and (os_trnno > '010205080000' and os_trnno < '010402059999' ) group by os_itno,os_crno,os_szno,os_thname,os_bdno order by os_itno,os_crno,os_szno,os_thname,os_bdno -- Sent from my Misaka 10032 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.71.170.97 ※ 文章網址: https://www.ptt.cc/bbs/Database/M.1423124209.A.BEC.html

02/05 17:40, , 1F
select c1,c2,sum(case when c3='正' then c4*1 whe
02/05 17:40, 1F

02/05 17:41, , 2F
when c3='負' then c4*-1 end)"總和" from testtabl
02/05 17:41, 2F

02/06 10:11, , 3F
感謝S大 完美解答阿!!!
02/06 10:11, 3F

02/06 11:55, , 4F
少了最後一句 group by c1,c2 order by c1; ^^"
02/06 11:55, 4F
文章代碼(AID): #1KqoRnli (Database)