查询列的是/否格式

qlckcl4x  于 2021-07-26  发布在  Java
关注(0)|答案(5)|浏览(246)

对于这个问题,我有以下简化的问题:

SELECT convert(bit, Substring(Max(convert(CHAR(8), tt.transaction_dt, 112) + convert(CHAR(1), tt.trans_live)), 9, 1)) AS is_live
FROM transaction_t tt

该查询旨在根据商户最新交易记录的“trans\u live”值来确定特定商户是否处于“活动”状态。
返回“1”或“0”。但是,我需要一个“yes/no”格式的列值。
注:查询部分如下:

Max(convert(CHAR(8), tt.transaction_dt, 112) + convert(CHAR(1), trans_live))

返回以下格式的结果:

202006271

相关表格结构如下:
交易记录

id     transaction_dt     trans_live     merchant_id
-----------------------------------------------------
1      2020-04-02         0              4
2      2020-04-02         1              4
3      2020-04-03         1              4
4      2020-04-04         0              4
4      2020-06-27         1              4
tcomlyy6

tcomlyy61#

请使用下面的case语句,

SELECT CASE (convert(bit, Substring(Max(convert(CHAR(8), tt.transaction_dt, 112) + convert(CHAR(1), tt.trans_live)), 9, 1))) 
   WHEN '0' THEN 'no' 
   WHEN '1' THEN 'yes' 
END AS is_live 
FROM transaction_t tt

语法可能错误,但这会有所帮助

bwleehnv

bwleehnv2#

你可以简单地使用 iif 如下所示:

SELECT iif(convert(bit, Substring(Max(convert(CHAR(8), tt.transaction_dt, 112) + convert(CHAR(1), tt.trans_live)), 9, 1)) = 1, 'Yes', 'No')  AS is_live
FROM transaction_t tt

db<>fillde演示。

svmlkihl

svmlkihl3#

我猜投票被否决的原因是你在这里做了一些非常“奇怪”的事情。但你的问题很清楚。
我理解您试图解决的问题:您希望获得具有最新事务日期的行的trans\u live值。但是你看不到如何使用max,因为 trans_live, max(transaction_dt) group by trans_live 不会做你想做的,也不会 max(transaction_dt), max(trans_live) .
因此,您提出了一个相当“聪明”的解决方案:将两者转换为字符值,将它们串联起来,对它们进行排序,然后从串联的值中分离出最不重要的“数字”。
但问题是,在语言中有更自然的方法来做这种事情。你的方法是一个聪明的解决方法,但它是神秘的。这也有点危险,除非你的trasaction\u dt列是一个日期(而不是datetime),否则你将截断时间部分,在这种情况下,同一日期不同时间的两个事务将具有相同的截断值,然后trans\u live=1的排序总是高于trans\u live=0的排序
更简单的解决方案如下:

create table transaction_t(merchant_id int, transaction_dt datetime, trans_live bit)
go

;with merchants as 
(
   select   distinct 
            merchant_id 
   from     transaction_t
)
select      m.merchant_id,
            is_live = t.trans_live
from        merchants m
cross apply (
               select   top 1 trans_live
               from     transaction_t
               where    merchant_id = m.merchant_id
               order by transaction_dt desc
            ) t

-- OR

select   distinct
         merchant_id, 
         is_live = first_value(trans_live) over
                   (
                      partition by merchant_id
                      order by transaction_dt desc
                   )
from     transaction_t

-- OR

select   merchant_id,
         is_live
from     (
            select   merchant_id,
                     is_live = trans_live,
                     rn = row_number() over 
                     (
                        partition by merchant_id 
                        order by transaction_dt desc
                     )
            from    transaction_t
         ) t 
where    t.rn = 1

编辑:如果你只是在寻找单一商家的结果。。。

declare @merchant_id int = 1;

select      top 1 
            is_live = trans_live
from        transaction_t
where       merchant_id = @merchant_id
order by    transaction_dt desc
jgzswidk

jgzswidk4#

请使用下面的查询,您必须使用case语句才能得到您的结果。您可以在select语句中使用必需的列。希望这是你的期望,如果不让我知道

select 
convert(bit, Substring(Max(convert(CHAR(8), tt.transaction_dt, 112) + convert(CHAR(1), 
tt.trans_live)), 9, 1)) AS is_live,
case when trans_live = 1 then 'Yes' 
   when trans_live = 0 then 'No' end as  trans_live
from transaction_t tt
wkftcu5l

wkftcu5l5#

该查询旨在根据商户最新交易记录的“trans\u live”值来确定特定商户是否处于“活动”状态。
我希望有这样的问题:

select merchant_id, 
       (case when max(tt.transaction_dt) = max(case when tt.trans_live = 1 then tt.transaction_dt)
             then 'Y' else 'N'
        end) as is_live
from transaction_t tt
group by merchant_id;

我对你的问题有点困惑。

相关问题