英文:
how to merge or combine a column name based on date group in multiple rows using union in MySQL
问题
我正在努力生成关于MySQL的报告,使用联合查询来合并多个表格,但结果并不符合我的预期。我在下面提到了一个查询:
SELECT * FROM
(
SELECT
raw_material_payment.paying_date AS date,
raw_materials.bill_no AS raw_material_bill_no ,
raw_material_payment.paying_amount + raw_material_payment.less AS raw_material_payment ,
"" AS billings_no ,
"" AS billing_payment ,
"" AS expense_no ,
"" AS expense_payment ,
"" AS old_payment_no ,
"" AS old_payment
from raw_materials
INNER JOIN raw_material_payment on raw_material_payment.raw_material_id = raw_materials.id
UNION ALL
SELECT
billings.date AS date ,
"" AS raw_material_bill_no ,
"" AS raw_material_payment ,
billings.id AS billings_no ,
billing_payments.paying_amount + billing_payments.less AS billing_payment ,
"" AS expense_no ,
"" AS expense_payment ,
"" AS old_payment_no ,
"" AS old_payment
FROM billings
INNER JOIN billing_payments on billing_payments.billing_id=billings.id
UNION ALL
SELECT
expense.bill_date AS date ,
"" AS raw_material_bill_no ,
"" AS raw_material_payment ,
"" AS billings_no ,
"" AS billing_payment ,
expense.bill_no AS expense_no ,
"" AS expense_payment ,
"" AS old_payment_no ,
"" AS old_payment
FROM expense
UNION ALL
SELECT
old_payments.bill_date AS date ,
"" AS raw_material_bill_no ,
"" AS raw_material_payment ,
"" AS billings_no ,
"" AS billing_payment ,
"" AS expense_no ,
"" AS expense_payment ,
old_payments.bill_no AS old_payment_no ,
old_payments.total AS old_payment
FROM old_payments
) as report GROUP BY date,raw_material_bill_no,billings_no,expense_no,old_payment_no;
结果如下:
我需要基于日期合并列值,如下所示:
单行包含了基于日期的所有字段值。
英文:
I am working on generating reports on MySQL in combining multiple tables using a union query but it comes to a result it was not suitable for my expected one I have mentioned a query below
SELECT * FROM
(
SELECT
raw_material_payment.paying_date AS date,
raw_materials.bill_no AS raw_material_bill_no ,
raw_material_payment.paying_amount + raw_material_payment.less AS raw_material_payment ,
"" AS billings_no ,
"" AS billing_payment ,
"" AS expense_no ,
"" AS expense_payment ,
"" AS old_payment_no ,
"" AS old_payment
from raw_materials
INNER JOIN raw_material_payment on raw_material_payment.raw_material_id = raw_materials.id
UNION ALL
SELECT
billings.date AS date ,
"" AS raw_material_bill_no ,
"" AS raw_material_payment ,
billings.id AS billings_no ,
billing_payments.paying_amount + billing_payments.less AS billing_payment ,
"" AS expense_no ,
"" AS expense_payment ,
"" AS old_payment_no ,
"" AS old_payment
FROM billings
INNER JOIN billing_payments on billing_payments.billing_id=billings.id
UNION ALL
SELECT
expense.bill_date AS date ,
"" AS raw_material_bill_no ,
"" AS raw_material_payment ,
"" AS billings_no ,
"" AS billing_payment ,
expense.bill_no AS expense_no ,
"" AS expense_payment ,
"" AS old_payment_no ,
"" AS old_payment
FROM expense
UNION ALL
SELECT
old_payments.bill_date AS date ,
"" AS raw_material_bill_no ,
"" AS raw_material_payment ,
"" AS billings_no ,
"" AS billing_payment ,
"" AS expense_no ,
"" AS expense_payment ,
old_payments.bill_no AS old_payment_no ,
old_payments.total AS old_payment
FROM old_payments
) as report GROUP BY date,raw_material_bill_no,billings_no,expense_no,old_payment_no;
it comes as a result
I Need a Combine column value based on the date like this
A single Row contains all the field values in one row based on the date
答案1
得分: 1
基于GROUP BY dateColumn
,似乎你描述了一个SUM(columnName) AS columnName
。这引出了一个问题:考虑日期为2023-07-10
,在这个日期下有多行数据,它们的old_payment_no
值分别显示为INV001、INV002以及多个空值(其他列也有多个值)。在对应于该日期的单元格中,应该放入哪个值?按字典顺序最高的?最低的?使用GROUP_CONCAT()
全部合并?
英文:
It looks like you describe a SUM(columnName) AS columnName
based on a GROUP BY dateColumn
.
Which begs the question: consider the date of 2023-07-10
, where you have several rows, whose old_payment_no
values are shown as INV001, INV002 and several empty values (other columns have multiple values too). Which value should go in the single cell of old_payment_no corresponding to that date? The lexicographically highest? The lowest? All of them using GROUP_CONCAT()
?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论