SQL查询中的多个案例 [英] Multiple Cases in SQL query

查看:60
本文介绍了SQL查询中的多个案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给出了一个SQL查询,该查询通过根据Card ar选择最大和最小日期来执行DateDIff操作,你可以说id,

这里'查询

There is a SQL query given below, that query performs DateDIff operation by slecting max and min date according to Card ar you can say id,
here' the query

SELECT
--COUNT(DIFF)
Count(DATEDIFF(D, MinDate, MaxDate)/30) AS DIFF

FROM (
SELECT

   MAX(TDate) AS MaxDate,
   MIN(TDate) AS MinDate
   FROM EDATA
   GROUP BY TCard
)a
Group by DATEDIFF(D, MinDate, MaxDate)/30





现在我想在此查询中应用某种排序之前执行DateDiff操作(排序可以基于几个参数,如年龄,名称等,但根据案例一次1)

现在我想知道如何通过此查询应用案例的地方?



Now i want to apply some kind of sorting in this query before performing DateDiff operation (sorting can be based on several parameters like age, name etc. but 1 at a time according to the cases)
Now I want to know How to apply cases over this query an where?

推荐答案

您好,



试试这个...

< br $> b $ b

Hi,

Try this...


ORDER BY
CASE
   WHEN cond.1 = 0 THEN 5
   WHEN cond.1 <> 0 THEN 4
   ELSE 1 END desc,
CASE
   WHEN cond.2 = 0 THEN 5
   WHEN cond.2 <> 0 THEN 4
   ELSE 9 END desc,
col1 ASC, col2 ASC, col3 ASC





希望这会对你有所帮助。



干杯



Hope this will help you.

Cheers


这篇关于SQL查询中的多个案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆