不使用十进制18,6的sql顺序

问题描述

带有order by子句的SQL查询无法正常工作。

查询

select cast(actual_qty as decimal(18,2)) from `Bin`
        where warehouse = `Warehouse`.name
        order by actual_qty desc

上述查询输出

303.00
550.00
0.00
3.00

actual_qty的类型为decimal(18,6)。我尝试按顺序使用cast函数,但没有用。

非常感谢任何帮助!

Update1:​​

这是我被解雇的完整查询

select `tabWarehouse`.name,CONCAT_WS(" : ","Actual Qty",ifnull( ( select round(`tabBin`.actual_qty,2) as qty
from `tabBin` where `tabBin`.warehouse = `tabWarehouse`.name
and `tabBin`.item_code = '30440'
order by qty desc),0) ) as actual_qty
from `tabWarehouse` where `tabWarehouse`.`name` like '%%%%' and ifnull(`tabWarehouse`.company,'') in ('','TILE TEST') and `tabWarehouse`.is_group = 0.0
limit 0,20

输出仍然是相同的,数量实际上并没有以降序排列。

更新2:

这是我的代码的样子:

    query = """select tw.name,ifnull(round(`tabBin`.actual_qty,2),0 ) actual_qty
        from `tabWarehouse` tw left join `tabBin` tb
        on tb.warehouse = tw.name {bin_conditions}
        where
           tw.`{key}` like {txt}
            {fcond} {mcond}
        order by ifnull(round(tb.actual_qty,0) desc
        limit
            {start},{page_len}
        """.format(
            bin_conditions=get_filters_cond(doctype,filter_dict.get("Bin"),bin_conditions,ignore_permissions=True),# sub_query=sub_query,key=searchfield,fcond=get_filters_cond(doctype,filter_dict.get("Warehouse"),conditions),mcond=get_match_cond(doctype),start=start,page_len=page_len,txt=frappe.db.escape('%{0}%'.format(txt))
        )

解决方法

cast中的select不会更改列的类型-order by引用的是表中的列,而不是{{1 }}。

一种选择是给它起一个新名称并使用它:

select

或重复该表达式:

select cast(actual_qty as decimal(18,2)) as qty
from `Bin`
where warehouse = `Warehouse`.name
order by qty desc;
,

查询中没有ORDER BY子句。
相关子查询中的ORDER BY是无用的,因为该子查询仅在仅返回1行且不影响最终结果时才起作用。
试试这个:

select tw.name,CONCAT_WS(" : ","Actual Qty",ifnull(round(tb.actual_qty,2),0)) actual_qty
from tabWarehouse tw left join tabBin tb
on tb.warehouse = tw.name and tb.item_code = '30440'
where tw.name like '%%%%' 
  and ifnull(tw.company,'') in ('','TILE SELECT') 
  and tw.is_group = 0.0
order by ifnull(round(tb.actual_qty,0) desc limit 0,20

这又是什么条件:

`tabWarehouse`.`name` like '%%%%'