DB2 SQL。使用Alpha和Numeric字段在订单上使用订单上的案例语句

发布于 2025-02-01 18:42:53 字数 366 浏览 1 评论 0原文

我正在尝试按子句中的命令语句使用案例语句,但没有成功,因为一个字段是char,一个字段是数字。如果我在数字周围放一个char(),我不会得到我需要的排序 有没有一种方法可以与零一起使用以使其正确分类? 代码:

Order by                                  
  Case                                    
     When :SortBy = 'I' then LProd    <-- Char
     When :SortBy = 'L' then Char(LLine)  <-- Numeric
  End;     

有人对如何执行此操作有任何想法吗?

I am attempting to use a case statement in my order by clause but am unsuccessful because one field is char and one field is numeric. If I put a Char() around the numeric, I don't get the sort I need because it is three byte numeric, 1, 2, 3. Which sorts in char() like 1, 10, 100, 2, etc
Is there a way to pad with zeros so that it sorts properly?
Code:

Order by                                  
  Case                                    
     When :SortBy = 'I' then LProd    <-- Char
     When :SortBy = 'L' then Char(LLine)  <-- Numeric
  End;     

Does anyone have an idea on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日裸衫吸 2025-02-08 18:42:53

尝试digits()

Order by                                  
  Case                                    
     When :SortBy = 'I' then LProd    <-- Char
     When :SortBy = 'L' then digits(LLine)  <-- Numeric
  End; 

try DIGITS()

Order by                                  
  Case                                    
     When :SortBy = 'I' then LProd    <-- Char
     When :SortBy = 'L' then digits(LLine)  <-- Numeric
  End; 
尾戒 2025-02-08 18:42:53

您可以使用CTE(常见的表表达式)按LPROD进行一阶,并给出等级(数字值)。然后从该CTE中选择并使用您需要的东西订购,因为两者都是数字。

with cte as(select id, rank() over (order by LProd) as LProd_ord, LProd, LLine from test)
select id, LProd, LLine
from cte
order by Case                                    
     When 'I' = 'I' then LProd_ord    
     When 'I' = 'L' then LLine
  End ; 

You can use cte(common table expression) to first order by LProd and give it a rank(numeric value). And then select from that cte and order with what ever you need because both will be numeric.

with cte as(select id, rank() over (order by LProd) as LProd_ord, LProd, LLine from test)
select id, LProd, LLine
from cte
order by Case                                    
     When 'I' = 'I' then LProd_ord    
     When 'I' = 'L' then LLine
  End ; 

Here is a demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文