对于这个问题,我有以下简化的问题:
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
5条答案
按热度按时间tcomlyy61#
请使用下面的case语句,
语法可能错误,但这会有所帮助
bwleehnv2#
你可以简单地使用
iif
如下所示:db<>fillde演示。
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的排序
更简单的解决方案如下:
编辑:如果你只是在寻找单一商家的结果。。。
jgzswidk4#
请使用下面的查询,您必须使用case语句才能得到您的结果。您可以在select语句中使用必需的列。希望这是你的期望,如果不让我知道
wkftcu5l5#
该查询旨在根据商户最新交易记录的“trans\u live”值来确定特定商户是否处于“活动”状态。
我希望有这样的问题:
我对你的问题有点困惑。