对于每个ID_Number,都有bill_date,然后是两种类型的账单。我想为每个ID号返回最新日期(最长日期),然后将两种类型的帐单金额加在一起。因此,根据下表,它应该返回:
| 1 | 201604 | 10.00 | |
| 2 | 201701 | 28.00 | |
tbl_charges
+-----------+-----------+-----------+--------+
| ID_Number | Bill_Date | Bill_Type | Amount |
+-----------+-----------+-----------+--------+
| 1 | 201601 | A | 5.00 |
| 1 | 201601 | B | 7.00 |
| 1 | 201604 | A | 4.00 |
| 1 | 201604 | B | 6.00 |
| 2 | 201701 | A | 15.00 |
| 2 | 201701 | B | 13.00 |
+-----------+-----------+-----------+--------+
然后,如果可能的话,我希望能够在另一个查询的连接中执行此操作,使用ID_Number作为连接的列。这会改变查询吗?
注意:我最初只想在大约1000万个中运行大约200个不同ID_Numbers的查询。我将为这些ID添加“IN”子句。当我为最终产品加入时,我将需要知道如何从所有其他连接可能性中获取最新日期。 (即,如何让ID_Number 1加入201604而不是201601?)
答案 0 :(得分:1)
我会使用NOT EXISTS
和GROUP BY
select, t1.id_number, max(t1.bill_date), sum(t1.amount)
from tbl_charges t1
where not exists (
select 1
from tbl_charges t2
where t1.id_number = t2.id_number and
t1.bill_date < t2.bill_date
)
group by t1.id_number
NOT EXISTS
过滤掉不相关的行,GROUP BY
过滤总和。
答案 1 :(得分:0)
我倾向于过滤int r = getChannelValue(codeR);
int g = getChannelValue(codeG);
int b = getChannelValue(codeB);
if (r != -1 && g != -1 && b != -1)
centreName.setForeground(new Color(r, g, b));
:
where
或者,另一种有趣的方法是使用带有元组的select id_number, sum(c.amount)
from tbl_charges c
where c.date = (select max(c2.date)
from tbl_charges c2
where c2.id_number = c.id_number and c2.bill_type = c.bill_type
)
group by id_number;
:
in