简体   繁体   中英

how to sort 2 different column in sql

can you guys show me how to sort user define column and fixed column name in sql. i need to display the highest transaction and outletid, instead i only get the highest transaction but the oulet id is not in grouping.

pardon me, im very bad at english

here is the problem

outlet id | revenue code | total transaction | total amount

6837      |     014  |        326        |   39158.94
6821      |     408  |        291        |   48786.50
6814      |     014  |        285        |   74159.76
6837      |     452  |        282        |   8846.80

and here is my sql

SELECT 
                outletid, 
                revcode,
                count(receiptnumbe) as Transactions,
                sum(amount) as total
            FROM 
                user_payment
            WHERE
                date = (SELECT MAX(date) FROM user_payment GROUP BY date desc LIMIT 0, 1)
            GROUP BY 
                outletid, revcode
            ORDER BY Transactions desc

i need it to be like this. sort by outlet id and highest transactions.

outlet id | revenue code | total transaction | total amount

    6837      |     014  |        326        |   39158.94
    6837      |     452  |        282        |   8846.80
    6821      |     408  |        291        |   48786.50
    6814      |     014  |        285        |   74159.76

Is this what you want?

   ORDER BY OutletId, Transactions desc

EDIT:

If I understand correctly, you want it sorted by the outlet that has the most total transactions. Then by transactions within that group. To do that, you need to summarize again at the outlet level and join back the results:

select outor.*
from (SELECT up.outletid, up.revcode, count(up.receiptnumbe) as Transactions,
             sum(up.amount) as total
      FROM user_payment up
      WHERE date = (SELECT MAX(date) FROM user_payment)
      GROUP BY outletid, revcode
     ) outor join
     (SELECT up.outletid, count(up.receiptnumbe) as Transactions,
             sum(up.amount) as total
      FROM user_payment up
      WHERE date = (SELECT MAX(date) FROM user_payment)
      GROUP BY outletid
     ) o
     on outor.outletid = o.outletid
order by o.Transactions desc, outor.outletid, outor.Transactions desc;

1)The first thing to do is make sure that you are sorting the fields the way you want to. Do you want them sorted numerically or alphabetically? See Sorting Lexical and Numeric Count should be numerical, but you should check outletid. If you have access to the tables, you could change the field to a number type for it to be sorted numerically or a string for it to be sorted alphabetically. You might have to use cast or convert. See Oracle Cast Documentation.

2)If you want the whole table sorted by outlet id and amount of transactions you might consider removing the group by clause.

3)The third thing I would look at even if this did work is renaming column names that had reserved words to the tables that were reserved words. I noticed transaction highlighted in blue.

When these things are checked Melon's comment should work.

Good question. Feel free to comment so I can follow up.

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