简体   繁体   中英

Combining multiple rows in a single row

I have SQL Query like this in SSMS

select distinct (b.TransactionNumber),
(case when b.Amount > 0 then c.total else 0 end) as 'Total Sales',
(case when b.TenderID = 1 then b.Amount else 0 end) as 'Cash',
(case when b.TenderID = 20 then b.Amount else 0 end) as 'Gift Certificates'
from [Transaction] c
inner join TenderEntry b on c.TransactionNumber = b.TransactionNumber

but the output is(see image for reference)

SQL 查询输出

This should be the expected output(see image for reference)

预期产出

I would expect one row per transaction number, especially given your use of select distinct :

select t.TransactionNumber, te.total as total_sales,
       sum(case when t.TenderID = 1 then t.Amount else 0 end) as Cash,
       sum(case when t.TenderID = 20 then t.Amount else 0 end) as Gift_Certificates
from TenderEntry te join
     Transaction t
     on te.TransactionNumber = t.TransactionNumber
group by t.TransactionNumber, te.total;

This produces one row per transaction.

Note the changes to the query:

  • The table aliases are meaningful (ie abbreviations of table names) rather than arbitrary letters.
  • The column aliases do not use single quotes. Only use single quotes for string and date constants.
  • The column aliases have been simplified so they do not need to be escaped.

It occurs to me that you might want to "list" the cash and gifts in the two columns. This would look like:

select TransactionNumber,
       max(case when seqnum = 1 then total end) as total_sales,
       sum(case when tenderId = 1 then amount end) as cash,
       sum(case when tenderId = 20 then amount end) as Gift_Certificates
from (select t.TransactionNumber, te.total, t.amount, t.TenderID,
           row_number() over (partition by t.TransactionNumber, t.TenderId order by t.amount) as seqnum
      from TenderEntry te join
           Transaction t
           on te.TransactionNumber = t.TransactionNumber
      where tenderid in (1, 20)
     ) x
group by t.TransactionNumber, seqnum;

This is only a partial answer, but I put it here because I cannot fit it into comments well enough.

It is likely that you do not want the DISTINCT component in the select. SELECT DISTINCT find all unique rows . So if you have 3 rows which all have some differences, it will show all three. If, on the other hand, there were two rows the same (eg, they paid for a $100 item with two $50 vouchers) it would just ignore one of them.

Instead, you probably need to become familiar with 'GROUP BY' which allows you to find totals etc across multiple rows.

For example (although not tested)

select 
    (b.TransactionNumber),
    (case when b.Amount > 0 then c.total else 0 end) as 'Total Sales',
    SUM(case when b.TenderID = 1 then b.Amount else 0 end) as 'Cash',
    SUM(case when b.TenderID = 20 then b.Amount else 0 end) as 'Gift Certificates'
from [Transaction] c
inner join TenderEntry b on c.TransactionNumber = b.TransactionNumber
GROUP BY (b.TransactionNumber, (case when b.Amount > 0 then c.total else 0 end))

In the above, I have removed the DISTINCT, added 'SUM' for the two transaction values (cash/certificates) and the GROUP BY across the TransactionNumber and Total sales (as that seems to be common across the transaction).

For the above data, what that would produce is 1 line of data for TransactionNumber = 1, with sales = 250 (as all the lines have that), and totals for cash and gift certificates (150 and 100 respectively if my maths is correct).

However, that is not your desired answer, - you want this transaction to go over two lines. Firstly, are you sure of that?

If you do, then we need another criteria by which to group them and you will need to specify that eg, why does this transaction's report need to go over two lines rather than just one?

Other notes

  • 'Transaction' is a special word within SQL Server. You are allowed to use it, but I would consider renaming the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM