如何在MySQL中使用UNION基于日期分组合并或组合多行的列名。

huangapple go评论75阅读模式
英文:

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;

结果如下:

如何在MySQL中使用UNION基于日期分组合并或组合多行的列名。

我需要基于日期合并列值,如下所示:

如何在MySQL中使用UNION基于日期分组合并或组合多行的列名。

单行包含了基于日期的所有字段值。

英文:

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

如何在MySQL中使用UNION基于日期分组合并或组合多行的列名。

I Need a Combine column value based on the date like this

如何在MySQL中使用UNION基于日期分组合并或组合多行的列名。

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()?

huangapple
  • 本文由 发表于 2023年7月18日 12:21:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定