gpt4 book ai didi

sql - 如何在case语句中引用多列

转载 作者:行者123 更新时间:2023-12-03 23:57:45 26 4
gpt4 key购买 nike

我有一个查询,我试图在其中使用引用两列的 case 语句。

数据:

用户表:

ID_User       sUserName
1 Test
2 Test2
3 Test3

客户表:
ID        last name       Redeemed        paid
1 Cust1 10/1/16 9/15/16
2 Cust2 9/16/16 9/14/16
3 Cust3 10/4/16 9/30/16
4 Cust4 10/5/16 10/1/16

询问:
SELECT     Users.sUserName, Customers.LastName, CASE WHEN customers.Redeemed < '9/28/16' AND 
customers.paid < '9/28/16' THEN '3' ELSE '4' END AS Amount
FROM Customers LEFT OUTER JOIN
Users ON Customers.lGreeterId = Users.ID_User
WHERE (Customers.Redeemed BETWEEN @startdate AND @enddate)

查询的输出:
susername  last name      amount

Test Cust1 4
Test2 Cust2 4
Test3 Cust3 4
Test3 Cust4 4

输出应该是:
susername  last name      amount

Test Cust1 3
Test2 Cust2 3
Test3 Cust3 4
Test3 Cust4 4

为什么我得到所有这些的“其他”结果?如果兑换的日期小于指定日期,我希望返回“3” OR付款少于指定日期。

付费和赎回都是数据类型 datetime

最佳答案

根据评论,尝试在查询中使用不同的日期格式

SELECT     Users.sUserName, Customers.LastName, 
CASE
WHEN customers.Redeemed < '2016-09-28' THEN '3'
WHEN customers.paid < '2016-09-28' THEN '3'
ELSE '4'
END AS Amount
FROM Customers
LEFT JOIN Users
ON Customers.lGreeterId = Users.ID_User
WHERE (Customers.Redeemed BETWEEN @startdate AND @enddate)

关于sql - 如何在case语句中引用多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39895966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com