在做了sql查询后付出了很多努力我最终陷入了混乱,到目前为止我所做的是这是table1
select bill_no='2016-2017', B_id, COALESCE(sum(amount-gr),0) as amount, COALESCE (sum(tax),0) as tax, COALESCE(sum(amount_paid),0) as amount_paid, COALESCE(sum(gr),0) as gr,'sun4269' as forUser from tbl_addbill
where forUser='sun4269' and bill_date between '2016-04-01' and '2017-03-31' group by B_id;
bill_no B_id amount tax amount_paid gr forUser
2016-2017 B-1 34875 0 0 0 sun4269
2016-2017 L-1 21014 0 19363 0 sun4269
2016-2017 P-1 10217 0 0 0 sun4269
2016-2017 S-1 30000 0 14000 0 sun4269
2016-2017 T-1 66237 0 21426 0 sun4269
这就是表2
select bill_no,B_id,amount,tax,amount_paid,gr,forUser from tbl_addbill where forUser='sun4269' and bill_date is null and bill_no='2015-2016'
bill_no B_id amount tax amount_paid gr forUser
2015-2016 K-1 181523 0 22159 0 sun4269
2015-2016 L-1 25266 0 4644 0 sun4269
2015-2016 P-1 122383 0 122383 113162 sun4269
2015-2016 A-1 38367 1827 0 0 sun4269
2015-2016 S-1 698262 18575 577120 113449 sun4269
可以看出,table1和table2中的列'B_id'具有相同和不同的值(L-1重复但B-1不重复),我想要的是如果L-1重复表格,然后金额,amount_paid,tax,gr sum与两个表中的'2016-2017'相加,如果B-1在任何表格中都是唯一的那么它也应该是'2016-2017的价值“
答案 0 :(得分:0)
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
let pointSize = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .title1).pointSize
titleLabel.font = titleLabel.font.withSize(pointSize)
}
您的table1和table2查询,并在UNION
表格上执行GROUP BY
及相关的汇总功能。
如果你只是从table1和table2的UNION
开始,你会看到我的意思,因为结果将是一个表,来自table1和table2的所有行;然后,您可以添加UNION
和所需的聚合函数(例如GROUP BY B_id
)。
答案 1 :(得分:0)
不确定您到底在寻找什么,但有些条件聚合会有帮助吗?
select..., sum(case when bill_date between '2016-04-01' and '2017-03-31'then amount-gr else null end) [2016billdata],
sum(case when bill_date between '2015-04-01' and '2016-03-31'then amount-gr else null end) [2015billdata]....from tbl_addbill
group by...