简体   繁体   中英

Select a query within a query that leads to opposite result

I'm having issues trying to display the correct column from my select within select query

SELECT
      merchant.merchantname  'MerchantName'
    , COALESCE(COUNT(transaction.transactionid),0) 'NoofTransaction'
    , COALESCE(SUM(transaction.transactionamount),0) 'TotalAmount'
    , (         SELECT
                  statement.statementbalance
            FROM statement
            WHERE transaction.transactionid = statement.transactionid
            ORDER BY
                  statementbalance DESC LIMIT 1 
      )                      
      AS 'BalanceRemaining'
FROM merchant
LEFT JOIN transaction ON merchant.merchantid = transaction.merchantid
AND transaction.transactiondate = '2018-01-16'
GROUP BY
      merchant.merchantid
ORDER BY
      merchant.merchantid ASC;

This is the statementbalance queueing down from sorted by the transactionid. On my BalanceRemaining column for the date '2018-01-16' I should be getting 7. Instead, I kept getting 500 from all the merchants listed when the sql query specifically order by descending order and limit the row returned to 1. Same thing will happen if i change it to get from transactionid

    statementbalance
    ===
    500
    .
    .
    233
    90
    7

Sample data for tables: http://sqlfiddle.com/#!9/0ec29f

You are not ordering by the correct column. If you want a value for the latest date then you need to order by a DATE and TIME columns in descending order:

SELECT m.merchantname as MerchantName,
         COALESCE(COUNT(t.transactionid), 0) as NoofTransaction, 
       COALESCE(SUM(t.transactionamount), 0) as TotalAmount,
       (SELECT s.statementbalance
        FROM statement s
        
        LIMIT 1 
       ) as BalanceRemaining
FROM merchant m 
LEFT JOIN transaction t ON m.merchantid = t.merchantid
                       AND t.transactiondate = '2018-01-16'
GROUP BY m.merchantid
ORDER BY m.merchantid ASC;

To enable this also change the way the data is correlated, NOT by transactionid, but by merchantid instead.

| MerchantName | NoofTransaction | TotalAmount | BalanceRemaining |
|--------------|-----------------|-------------|------------------|
|         Dog1 |               0 |           0 |           (null) |
|         Cat2 |              13 |         115 |            24.16 |
|      Parrot3 |               1 |          20 |           299.25 |
|      Beaver4 |               0 |           0 |           (null) |

see: http://sqlfiddle.com/#!9/cc2440/3

If you want "7", perhaps you want an ASC sort instead of a DESC sort.

I would also recommend using table aliases:

SELECT m.merchantname as MerchantName,
       COUNT(t.transactionid), 0) as NoofTransaction, 
       COALESCE(SUM(t.transactionamount), 0) as TotalAmount,
       (SELECT s.statementbalance
        FROM statement s
        WHERE t.transactionid = s.transactionid
        ORDER BY statementbalance ASC
        LIMIT 1 
       ) as BalanceRemaining
FROM merchant m LEFT JOIN
     transaction t
     ON m.merchantid = t.merchantid AND t.transactiondate = '2018-01-16'
GROUP BY m.merchantid
ORDER BY m.merchantid ASC;

Notes:

  • Table aliases make the query easier to write and to read.
  • Only use single quotes for string and date constants. Do not use them for column aliases.
  • I recommend as for column aliases.
  • COUNT() never returns NULL , so COALESCE() is unnecessary.

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