简体   繁体   English

如何在SQL中对2个不同的列进行排序

[英]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. 你们可以告诉我如何在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 这是我的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: 为此,您需要在outlet级别再次进行汇总,然后将结果重新加入:

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. 1)首先要做的是确保您要对字段进行排序。 Do you want them sorted numerically or alphabetically? 您要它们按数字还是字母排序? See Sorting Lexical and Numeric Count should be numerical, but you should check outletid. 请参阅对词法和数字计数排序应为数字,但应检查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. 请参阅Oracle Cast文档。

2)If you want the whole table sorted by outlet id and amount of transactions you might consider removing the group by clause. 2)如果您希望整个表按出口ID和交易量排序,则可以考虑删除group by子句。

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. 3)即使这样做,我还要看的第三件事是将具有保留字的列名重命名为保留字的表。 I noticed transaction highlighted in blue. 我注意到交易以蓝色突出显示。

When these things are checked Melon's comment should work. 选中这些内容后,Melon的评论应该起作用。

Good question. 好问题。 Feel free to comment so I can follow up. 请随时发表评论,以便我跟进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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