【问题标题】:MySQL Union query: PASSING TEMP ClaimID field value to TEMP1 table ClaimID in one union queryMySQL 联合查询:在一个联合查询中将 TEMP ClaimID 字段值传递给 TEMP1 表 ClaimID
【发布时间】:2020-05-13 09:28:20
【问题描述】:
CREATE DEFINER=`root`@`localhost` PROCEDURE `SP_DupAuditCriteria_1`()
BEGIN
    set @RowNbr=concat(date_format(curdate(),'%Y%m%d'),'0000000');

select temp.* from
(SELECT c.*, concat(c.VendorID,a.VendorID) as MergeH200A, concat(a.VendorID,c.VendorID) as MergeH200B, 'New' as Record_Type, @RowNbr:= @RowNbr + 1 AS ClaimID
FROM tbldupaudit_currentitems AS c
    inner join tbldupaudit_archiveitems a 
        on c.InvoiceID = a.InvoiceID
        and c.GrossAmount = a.GrossAmount) as temp
    inner join tbldupaudit_archiveitems b
        on temp.InvoiceID = b.InvoiceID
        and temp.GrossAmount = b.GrossAmount
        and temp.VendorID = b.VendorID
        and temp.VoucherID <> b.VoucherID
Union all
select temp1.* from
(SELECT f.*, concat(f.VendorID,d.VendorID) as MergeH200A, concat(d.VendorID,f.VendorID) as MergeH200B, 'ARCHIVE' as Record_Type, 1 AS ClaimID
FROM tbldupaudit_archiveitems AS f
    inner join tbldupaudit_currentitems d 
        on f.InvoiceID = d.InvoiceID
        and f.GrossAmount = d.GrossAmount) as temp1
    inner join tbldupaudit_currentitems e
        on temp1.InvoiceID = e.InvoiceID
        and temp1.GrossAmount = e.GrossAmount
        and temp1.VendorID = e.VendorID
        and temp1.VoucherID <> e.VoucherID
order by InvoiceID, Record_Type DESC;

END 

尝试为每对创建唯一的 ClaimID。我能够为前半联合所有生成序列号,但相同的 ClaimID 不能在另一半联合全部生成。 请帮助我了解如何为匹配的项目创建/生成一个唯一 ID。

谢谢!]1

【问题讨论】:

  • 尝试为每对创建唯一的 ClaimID。 什么PAIR匹配项什么是匹配标准?
  • What PAIR: Current items vs Archive items,逻辑在当前和存档表中识别匹配的项目。现在我需要通过创建一个唯一 ID 来告诉用户它们是一对的。
  • 逻辑识别匹配项什么逻辑?什么表达式(在数据集上唯一)标识记录并允许设置记录对匹配?
  • '''标准相同InvoiceID和相同量的输入表CurrentItems ArchiveItems InvoiceID InvoiceID InvoiceID金额12345 3000 12345 3000 54321 4560 123456 2000 12345 3000 12345678 4000结果是这样ClaimID的InvoiceID InvoiceID UNQ12345 12345 3000 UNQ12345 12345 3000 ''' 这个 ClaimID 对于每个 PAIR 都应该是唯一的。如果我不了解 MySQL 和数据库世界,我很抱歉。
  • 请将事实添加到问题文本中,而不是添加到 cmets 中。格式正确。

标签: mysql parameter-passing union union-all


【解决方案1】:

在第二次选择中(在 UNION ALL 之后),您不会增加 var,因此如果您希望两个值都增加,请尝试在第二次选择中使用 var

select temp.* 
from (
    SELECT c.*, concat(c.VendorID,a.VendorID) as MergeH200A, concat(a.VendorID,c.VendorID) as MergeH200B, 'New' as Record_Type
        , @RowNbr:= @RowNbr + 1 AS ClaimID
    FROM tbldupaudit_currentitems AS c
    inner join tbldupaudit_archiveitems a on c.InvoiceID = a.InvoiceID
        and c.GrossAmount = a.GrossAmount
    ) as temp
inner join tbldupaudit_archiveitems b on temp.InvoiceID = b.InvoiceID
    and temp.GrossAmount = b.GrossAmount
        and temp.VendorID = b.VendorID
            and temp.VoucherID <> b.VoucherID
Union all
select temp1.* 
from ( 
    SELECT f.*, concat(f.VendorID,d.VendorID) as MergeH200A, concat(d.VendorID,f.VendorID) as MergeH200B, 'ARCHIVE' as Record_Type
        , @RowNbr:= @RowNbr + 1 AS ClaimID
    FROM tbldupaudit_archiveitems AS f
    inner join tbldupaudit_currentitems d on f.InvoiceID = d.InvoiceID
        and f.GrossAmount = d.GrossAmount
) as temp1
inner join tbldupaudit_currentitems e on temp1.InvoiceID = e.InvoiceID
      and temp1.GrossAmount = e.GrossAmount
        and temp1.VendorID = e.VendorID
            and temp1.VoucherID <> e.VoucherID
order by InvoiceID, Record_Type DESC;

【讨论】:

  • ArchiveItems 有多个匹配的机会,我猜这会是个问题..
  • var .. 应该返回不同的值 .. 所以你应该尝试......我猜......
  • 已尝试,但结果与所附图片中的预期不符
  • ClaimID 是在运行查询时添加的列。您可以看到 Results 表的第一列是 ClaimID。
【解决方案2】:

建模示例 - 请参阅 fiddle

SELECT CONCAT( 'prefix', LPAD( CASE @prev 
                               WHEN @prev := InvoiceID 
                               THEN @num
                               ELSE @num := @num + 1
                               END, 8, '0' ) ) ClaimID, InvoiceID, Amount, FromTable
FROM ( SELECT InvoiceID, Amount, 'CurrentItems' FromTable
       FROM CurrentItems
       UNION ALL
       SELECT InvoiceID, Amount, 'ArchivedItems'
       FROM ArchivedItems
       WHERE EXISTS ( SELECT NULL 
                      FROM CurrentItems
                      WHERE CurrentItems.InvoiceID = ArchivedItems.InvoiceID) ) unioned,
     ( SELECT @prev := 0, @num := 1000 ) variables
ORDER BY InvoiceID, FromTable DESC;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    相关资源
    最近更新 更多