我正在尝试填写与每张发票上有唯一行的客户相关的所有发票 - tval 列应添加(总和)发票编号重复的总和,总tval总和应为根据指示的标签(列)显示别名。只有两种类型的标签“crn”& “acr”如果两者都没有那么值可以是Null
我尝试了很多方法来实现这一点,但我失败了,这是我能得到的最接近的
SELECT
i.si_num AS `id`,
DATE_FORMAT(i.si_date, '%d-%b-%Y') AS `date`,
i.si_tr AS `tr`,
d.dl_name AS `customer`,
i.si_net_value AS `net`,
DATEDIFF(CURDATE(), si_date) AS Days,
t.value AS tval,
t.label AS label
FROM
invoices AS i
LEFT JOIN
dealer AS d ON i.si_tr = d.dl_id
LEFT JOIN
transactions AS t ON i.si_num = t.invoice
WHERE
i.si_tr = 'TR580494'
ORDER BY `si_num` DESC;
请帮助。
答案 0 :(得分:0)
使用SQL语句,尝试在tval上使用DISTINCT示例:
SELECT DISTINCT tval
答案 1 :(得分:0)
SELECT
i.si_num AS `id`, DATE_FORMAT(i.si_date, '%d-%b-%Y') AS `date`,
i.si_tr AS `tr`, d.dl_name AS `customer`, i.si_net_value AS `net`,
DATEDIFF(CURDATE(), si_date) AS Days, SUM(t.value) AS tval,
GROUP_CONCAT(t.label) AS label
FROM invoices AS i
INNER JOIN dealer AS d ON i.si_tr = d.dl_id
INNER JOIN transactions AS t ON i.si_num = t.invoice
WHERE i.si_tr = 'TR580494'
GROUP BY id, date, tr, customer, net, Days
ORDER BY `si_num` DESC;