【问题标题】:How to get unique record with Invoice Number in SQL如何在 SQL 中获取带有发票编号的唯一记录
【发布时间】:2020-01-22 01:12:46
【问题描述】:

我有一张这样的桌子;现在我需要带有逗号分隔的测试名称和所有列的不同发票编号。我尝试了这个查询,但我得到了一个错误。

选择列表中只能指定一个表达式,当 EXISTS 不引入子查询

SELECT DISTINCT
    PTD.InvoiceNumber, PL.PatientName, PTD.TestDate, PTD.Discount, PTD.Commission,
    (SELECT SUM(TestAmount) AS TestAmount 
     FROM tb_Patienttestdetails
     WHERE PTD.InvoiceNumber = InvoiceNumber) AS TotalAmount,  
    (TotalAmount - (PTD.Commission + PTD.Discount)) AS RealAmount,
    PTD.Remark, PL.PatientId,    
    (SELECT DISTINCT
         t1.InvoiceNumber,
         STUFF((SELECT DISTINCT '' + t2.Testname 
                FROM tb_PatientTestDetails t2  
                WHERE t1.InvoiceNumber = t2.InvoiceNumber
                FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 0, '') TestName
     FROM
         tb_PatientTestDetails t1)   
FROM 
    tb_Patienttestdetails PTD 
INNER JOIN
    tb_PatientList PL ON PTD.PatientId = PL.Id

【问题讨论】:

  • 我正在使用 sql server 2014。我正在为此使用交叉应用。

标签: sql sql-server


【解决方案1】:

答案似乎很明显。此子查询返回两列:

(SELECT DISTINCT
     t1.InvoiceNumber,
     STUFF((SELECT DISTINCT '' + t2.Testname 
            FROM tb_PatientTestDetails t2  
            WHERE t1.InvoiceNumber = t2.InvoiceNumber
            FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 0, '') TestName
 FROM
     tb_PatientTestDetails t1)   

大概,你打算这样:

SELECT PTD.InvoiceNumber, PL.PatientName, PTD.TestDate, PTD.Discount, PTD.Commission,
       SUM(PTD.TestAmount) AS TestAmount,
       SUM(PTD.TestAmount - (PTD.Commission + PTD.Discount)) AS RealAmount,
       PTD.Remark, PL.PatientId,    
       STUFF((SELECT DISTINCT '' + t2.Testname 
              FROM tb_PatientTestDetails ptd2 
              WHERE ptd.InvoiceNumber = ptd2.InvoiceNumber
              FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 0, ''
             )
            ) as tests 
FROM tb_PatientList PL JOIN
    tb_Patienttestdetails PTD 
    ON PTD.PatientId = PL.Id
GROUP BY PTD.InvoiceNumber, PL.PatientName, PTD.TestDate, PTD.Discount, PTD.Commission, PTD.Remark, PL.PatientId;

【讨论】:

    【解决方案2】:

    此代码解决的问题

    SELECT PTD.InvoiceNumber, PL.PatientName, PTD.TestDate, PTD.Discount, PTD.Commission,
           SUM(PTD.TestAmount) AS TestAmount,
           SUM(PTD.TestAmount - (PTD.Commission + PTD.Discount)) AS RealAmount,
           PTD.Remark, PL.PatientId,   
          STUFF((SELECT distinct '' + t2.Testname from tb_PatientTestDetails t2  where PTD.InvoiceNumber = t2.InvoiceNumber FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,0,'') TestName  FROM tb_PatientList PL JOIN
        tb_Patienttestdetails PTD 
        ON PTD.PatientId = PL.Id GROUP BY PTD.InvoiceNumber, PL.PatientName, PTD.TestDate, PTD.Discount, PTD.Commission, PTD.Remark, PL.PatientId;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      相关资源
      最近更新 更多