作者cutekid (可爱小孩子)
看板Database
标题Re: [SQL ] 「.. 」条件写法
时间Wed Apr 19 14:16:23 2017
依目前的设计,应该没有更聪明的作法
倒是觉得原来的设计不是很好
要是我的话,我会拆成二个表
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://webptt.com/cn.aspx?n=bbs/Database/M.1492582586.A.F43.html
1F:推 ChingLan326: 这是既有系统无法改schema了 後来发现那是条件栏位 04/19 17:53
2F:→ ChingLan326: 还可以写用><|=等逻辑运算子........ 04/19 17:54
3F:→ ChingLan326: ..代表between 大概要先写一个程式来解了>_< 04/19 17:54
4F:推 streetbad: 再自己建一个对应的table呢? 04/20 09:48
5F:→ ChingLan326: 是其他系统套装的DB不能改 正在写的是介接 04/20 10:40