mysql数据库排序依据

w7t8yxp5  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(281)
<table>
        <tr>
            <th>name</th>
            <th>sum(value)</th>
         </tr>
         <tr>
            <td>null</td>
            <td>85</td>
         </tr>
         <tr>
            <td>arun</td>
            <td>74</td>
        </tr>
        <tr>
            <td>dhin</td>
           <td>55</td>
       </tr>
    </table>

我有一个像上面这样的数据表。我想根据sum(value)desc对项目进行排序,如果名称为null,则必须显示在最后。如何在mysql中编写查询
我的查询是select p.class,p.buyref,p.no\u of \u mat\u id,p.tot\u cos from(select a.class,group\u concat(distinct a.buyref separator“,”)as buyref,count(*)as no\u of \u mat\u id,sum(tc)as tot\u cos from(select a.class,a.buyref,sum(b.total\u cost))as tc from(select distinct a.class,b.buyref from) material_master 加入 rxml_reference b on a.catŠid=b.catŠid,其中a.language='0161-1Šlg-000001Š1'和b.property='buyer reference'和b.buyref<>'',b.buyref in(“e268200006”、“e570500008”、“e330010072”、“1274266”、“1257657”、“1216933”、“1275795”、“1269085”、“1216931”、“1257656”、“1213833”、“5066725”、“1275794”、“506752”、“1257658”、“1236240”、“1274249”、“1213824”、“1275797”、“506764”、“1236239”,"1248625","1275796","5066724","5065949","1213989","1216932","1257659","1227545","1280565","1280549","1213829","1264792","1280550","1280551","1216943","1249846","1210250","1282003","1248620","5065948","1213830","1216942","1210217","1210247","1279855","1280567","1280553","1210292","1281546","1213846","1238253","1210323","1264791","5065610","1217304","1216973","1271479","1285240","1217303","5063937","5063934","1285241","1238255","1280552","1280566","1292638","5075432","1292635","1293721","1258418","1290188","5071130","1292061","1258417","1292634","1292631","1292632","1292633","1292639","1292060","5071580","1293251","5075431","1292597","36066","1268083","1219900","49194","46646","1272051","48743","1219891","49604","41693","1247650","1241850","41728","49588","1258736","44917","1219898","1266629","39660","1267486","1270445","1222352","1259901","36074","1272388","1275153","1240833","1237806","46645","1219897","1271400","1272385","41066","36073","1244303","1241848","41730","1220612","45332","41565","49204","1244246","45329","1272384","1222919","1231947","1225515","1272052","1271399","49664","46118","1241847","49691","1219899","1273498","52919","46350","49201","1272750","1237807","29632","1220800","36481","41709","1276924","40133","36062","36268","49684","1273492","1276923","36302","1281374","49603","1219892",“49480”))a join final\u transaction b on a.buyref=b.item其中b.accounting\u date>='2014-1-01 00:00'和b.accounting\u date<='2018-4-31 00:00:00'和b.supplier\u id in('globl-asp01-0000005443','globl-asp01-0000005443','globl-nft01-0000124166','globl-asp01-0000001239','apcpm-apcpm-a0012496','globl-asp01-0000001643','acm\u 388437','acm\u 11590352','acm\u 5174231','ecm\u 18494')按a.class分组,a.buyref)按a.class分组按sum排序(tc)desc)按p.tot\u cos desc排序

ecbunoof

ecbunoof1#

SELECT name,SUM(value) 
FROM TABLE1
ORDER BY CASE WHEN name IS NULL THEN 1 ELSE 0 END,SUM(value) Desc

输出

name    sum_value
trun    84
arun    74
dhin    55
(null)  85

演示链接
http://sqlfiddle.com/#!9/fa6eb7/1号机组

相关问题