时间:2018-04-13 09:12:18

标签: mysql sql-order-by



<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>
&#13;
&#13;
&#13;

我有一个像上面这样的表数据。我想基于sum(value)desc排序项目,如果名称为null,则必须最后显示。如何在mysql中编写查询

我的查询是SELECT p.class,p.buyref,p.no_of_mat_id,p.tot_cos from(select a.class,group_concat(distinct a.buyref SEPARATOR&#39;&#34;,&#34;& #39;)as buyref,count(*)as no_of_mat_id,sum(tc)as tot_cos from(select a.class,a.buyref,sum(b.total_cost)as tc from(SELECT distinct a.class,b.buyref来自material_master来自a.cat_id = b.cat_id的加入rxml_reference b,其中a.language =&#39; 0161-1#LG-000001#1&#39;和b.property =&# 39;买家参考&b;和b.buyref&lt;&gt;&#39;&#39;和b.buyref in(&#34; E268200006&#34;,&#34; E570500008&#34;,&#34; E330010072&#34;&#34; 1274266&#34;&#34; 1257657&#34;&#34; 1216933&#34;&#34; 1275795&#34;&#34; 1269085&#34 ;, &#34; 1216931&#34;&#34; 1257656&#34;&#34; 1213833&#34;&#34; 5066725&#34;&#34; 1275794&#34;&#34; 5067252& #34;&#34; 1257658&#34;&#34; 1236240&#34;&#34; 1274249&#34;&#34; 1213824&#34;&#34; 1275797&#34;,& #34; 5067664&#34;&#34; 1236239&#34;&#34; 1248625&#34;&#34; 1275796&#34;&#34; 5066724&#34;&#34; 5065949&# 34;,&#34; 1213989&#34;&#34; 1216932&#34;&#34; 1257659&#34;&#34; 122 7545&#34;&#34; 1280565&#34;&#34; 1280549&#34;&#34; 1213829&#34;&#34; 1264792&#34;&#34; 1280550&#34 ;, &#34; 1280551&#34;&#34; 1216943&#34;&#34; 1249846&#34;&#34; 1210250&#34;&#34; 1282003&#34;&#34; 1248620& #34;&#34; 5065948&#34;&#34; 1213830&#34;&#34; 1216942&#34;&#34; 1210217&#34;&#34; 1210247&#34;,& #34; 1279855&#34;&#34; 1280567&#34;&#34; 1280553&#34;&#34; 1210292&#34;&#34; 1281546&#34;&#34; 1213846&# 34;,&#34; 1238253&#34;&#34; 1210323&#34;&#34; 1264791&#34;&#34; 5065610&#34;&#34; 1217304&#34;&# 34; 1216973&#34;&#34; 1271479&#34;&#34; 1285240&#34;&#34; 1217303&#34;&#34; 5063937&#34;&#34; 5063934&#34 ;,&#34; 1285241&#34;&#34; 1238255&#34;&#34; 1280552&#34;&#34; 1280566&#34;&#34; 1292638&#34;&#34 ; 5075432&#34;&#34; 1292635&#34;&#34; 1293721&#34;&#34; 1258418&#34;&#34; 1290188&#34;&#34; 5071130&#34; &#34; 1292061&#34;&#34; 1258417&#34;&#34; 1292634&#34;&#34; 1292631&#34;&#34; 1292632&#34;&#34; 1292633&#34;&#34; 1292639&#34;&#34; 1292060&#34;&#34; 5071580&#34;&#34; 1293251&#34;&#34; 5075431&#34 ;, &#34; 1292597&#34;&#34; 36066&#34;&#34; 1268083&#34;&#34; 1219900&#34;&#34; 49194&#34;&#34; 46646& #34;&#34; 1272051&#34;&#34; 48743&#34;&#34; 1219891&#34;&#34; 49604&#34;&#34; 41693&#34;,& #34; 1247650&#34;&#34; 1241850&#34;&#34; 41728&#34;&#34; 49588&#34;&#34; 1258736&#34;&#34; 44917&# 34;,&#34; 1219898&#34;&#34; 1266629&#34;&#34; 39660&#34;&#34; 1267486&#34;&#34; 1270445&#34;&# 34; 1222352&#34;&#34; 1259901&#34;&#34; 36074&#34;&#34; 1272388&#34;&#34; 1275153&#34;&#34; 1240833&#34 ;,&#34; 1237806&#34;&#34; 46645&#34;&#34; 1219897&#34;&#34; 1271400&#34;&#34; 1272385&#34;&#34 ; 41066&#34;&#34; 36073&#34;&#34; 1244303&#34;&#34; 1241848&#34;&#34; 41730&#34;&#34; 1220612&#34; &#34; 45332&#34;&#34; 41565&#34;&#34; 49204&#34;&#34; 1244246&#34;&#34; 45329&#34;&#34; 1272384&#34;&#34; 1222919&#34;&#34; 1231947&#34;&#34; 1225515&#34;&#34; 1272052&#34;&#34; 1271399&#34 ;, &#34; 49664&#34;&#34; 46118&#34;&#34; 1241847&#34;&#34; 49691&#34;&#34; 1219899&#34;&#34; 1273498& #34;&#34; 52919&#34;&#34; 46350&#34;&#34 ; 49201&#34;&#34; 1272750&#34;&#34; 1237807&#34;&#34; 29632&#34;&#34; 1220800&#34;&#34; 36481&#34; &#34; 41709&#34;&#34; 1276924&#34;&#34; 40133&#34;&#34; 36062&#34;&#34; 36268&#34;&#34; 49684&#34;&#34; 1273492&#34;&#34; 1276923&#34;&#34; 36302&#34;&#34; 1281374&#34;&#34; 49603&#34 ;, &#34; 1219892&#34;,&#34; 49480&#34;))在a.buyref = b.item上加入final_transaction b,其中b.accounting_date&gt; =&#39; 2014-1-01 00:00 :00&#39;和b.accounting_date&lt; =&#39; 2018-4-31 00:00:00&#39;和b.supplier_id(&#39; GLOBL-ASP01-0000005443&#39;,&#39; GLOBL-ASP01-0000005443&#39;&#39; GLOBL-NFT01-0000124166&#39;,&#39; GLOBL -ASP01-0000001239&#39;,&#39; APCPM-APCPM-A0012496&#39;,&#39; GLOBL-ASP01-0000001643&#39;&#39; ACM_388437&#39;&#39; ACM_11590352&#39 ;,&#39; ACM_5174231&#39; ECM_18494&#39;)按a.class分组,a.buyref)按a.class顺序分组sum(tc)desc)p ORDER BY p.tot_cos降序

1 个答案:

答案 0 :(得分:0)

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