看板 Database 關於我們 聯絡資訊
依目前的設計,應該沒有更聰明的作法 倒是覺得原來的設計不是很好 要是我的話,我會拆成二個表 Table 1: 記錄 AccountID 跟它的 Range 例: AccountID begin end ------------------------- 101010 101011 101019 Table 2: 記錄 AccountID 跟它的 Amount 例: AccountID Amount ----------------- 101011 10 101012 20 101013 30 101014 40 101015 50 101016 60 查詢 SQL: select t1.AccountID,sum(t2.Amount) as totalAmount from table1 t1 inner join table2 t2 on t2.AccountID between t1.begin and t1.end group by t1.AccountID ※ 引述《ChingLan326 (總舵主)》之銘言: : 資料庫名稱:MS SQL : 資料庫版本:2014 : 內容/問題描述: : 資料表如下 : AccountID Amount Totaling : ---------- -------------------- -------------------- : 101010 101011..101019 : 101011 10 : 101012 20 : 101013 30 : 101014 40 : 101015 50 : 101016 60 : 如果Totaling不為空,表示Amount為合計欄位 : 上表中 : AccountID = 101010的Amount為 : AccountID between 101011 and 101019 的 Amount 合計 : 我現在用CHARINDEX去拆Totaling為2個欄位 : 再用between條件去合計 : 請問有其他寫法嗎 : 覺得資料條件這樣設計應該有其他更聰明的寫法 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.221.80.36 ※ 文章網址: https://www.ptt.cc/bbs/Database/M.1492582586.A.F43.html
ChingLan326: 這是既有系統無法改schema了 後來發現那是條件欄位 04/19 17:53
ChingLan326: 還可以寫用><|=等邏輯運算子........ 04/19 17:54
ChingLan326: ..代表between 大概要先寫一個程式來解了>_< 04/19 17:54
streetbad: 再自己建一個對應的table呢? 04/20 09:48
ChingLan326: 是其他系統套裝的DB不能改 正在寫的是介接 04/20 10:40