Sql 当函数
我正在尝试创建一个查询,将结果分成不同的列。我能找到的最好的公式是Case When函数,但它说方程的Then部分必须是布尔值(或真/假结果)。例如,Then有没有办法计算数字3-1Sql 当函数,sql,case-when,Sql,Case When,我正在尝试创建一个查询,将结果分成不同的列。我能找到的最好的公式是Case When函数,但它说方程的Then部分必须是布尔值(或真/假结果)。例如,Then有没有办法计算数字3-1 Case when DATEDIFF(day, T0.[DocDueDate], getdate()) > 0 AND DATEDIFF(day, T0.[DocDueDate], getdate()) < 30 then (T0.[DocTotal] - T0.[PaidToDat
Case
when
DATEDIFF(day, T0.[DocDueDate], getdate()) > 0
AND DATEDIFF(day, T0.[DocDueDate], getdate()) < 30
then
(T0.[DocTotal] - T0.[PaidToDate])
else
' '
end
as "Greater than 1",
Case
when
DATEDIFF(day, T0.[DocDueDate], getdate()) > 30
AND DATEDIFF(day, T0.[DocDueDate], getdate()) < 60
then
(T0.[DocTotal] - T0.[PaidToDate])
else
' '
end
as "Greater than 30"
案例
什么时候
DATEDIFF(天,T0.[DocDueDate],getdate())>0
和DATEDIFF(day,T0.[DocDueDate],getdate())<30
然后
(T0.[DocTotal]-T0.[PaidToDate])
其他的
' '
结束
作为“大于1”,
案例
什么时候
DATEDIFF(天,T0.[DocDueDate],getdate())>30
和DATEDIFF(day,T0.[DocDueDate],getdate())<60
然后
(T0.[DocTotal]-T0.[PaidToDate])
其他的
' '
结束
“大于30”
如果您使用sql server,那么您的代码剪辑将如下所示。
您必须在之后
和else
之后保持与使用else之后使用的字符串类型相同的数据类型,因此您必须在以后的之后
Case
when
DATEDIFF(day, T0.[DocDueDate], getdate()) > 0
AND DATEDIFF(day, T0.[DocDueDate], getdate()) < 30
then
( convert(varchar(255), T0.[DocTotal] - T0.[PaidToDate]))
else
' '
end
as Greater_than_1,
Case
when
DATEDIFF(day, T0.[DocDueDate], getdate()) > 30
AND DATEDIFF(day, T0.[DocDueDate], getdate()) < 60
then
(convert(varchar(255), T0.[DocTotal] - T0.[PaidToDate]))
else
' '
end
as Greater_than_30
案例
什么时候
DATEDIFF(天,T0.[DocDueDate],getdate())>0
和DATEDIFF(day,T0.[DocDueDate],getdate())<30
然后
(转换(varchar(255),T0.[DocTotal]-T0.[PaidToDate]))
其他的
' '
结束
当_大于_1时,
案例
什么时候
DATEDIFF(天,T0.[DocDueDate],getdate())>30
和DATEDIFF(day,T0.[DocDueDate],getdate())<60
然后
(转换(varchar(255),T0.[DocTotal]-T0.[PaidToDate]))
其他的
' '
结束
大于30
您的类型兼容性有问题。我建议您仅使用NULL
表示不匹配:
(case when DATEDIFF(day, T0.[DocDueDate], getdate()) > 0 AND DATEDIFF(day, T0.[DocDueDate], getdate()) < 30
then (T0.[DocTotal] - T0.[PaidToDate])
end) as Greater_than_1,
(case when DATEDIFF(day, T0.[DocDueDate], getdate()) > 30 and DATEDIFF(day, T0.[DocDueDate], getdate()) < 60
then (T0.[DocTotal] - T0.[PaidToDate])
end) as Greater_than_30
(DATEDIFF(day,T0.[DocDueDate],getdate())>0和DATEDIFF(day,T0.[DocDueDate],getdate())小于30时的情况
然后(T0.[DocTotal]-T0.[PaidToDate])
结束)大于1时,
(DATEDIFF(天,T0.[DocDueDate],getdate())大于30,DATEDIFF(天,T0.[DocDueDate],getdate())小于60时的情况
然后(T0.[DocTotal]-T0.[PaidToDate])
结束)大于30
我还想你打算
发现我需要将方程转换为一个整数,如下所示
Case when
DATEDIFF(day, T0.[DocDueDate], getdate()) > 0
AND DATEDIFF(day, T0.[DocDueDate], getdate()) <30
then
cast( (T0.[DocTotal]-T0.[PaidToDate]) as varchar(12) )
else
' '
end as "Greater than 1"
发生时的情况
DATEDIFF(天,T0.[DocDueDate],getdate())>0
和DATEDIFF(day,T0.[DocDueDate],getdate())下面是如何使用各种方法实现这一点。TSET正在为测试生成从6月1日到今天的日期
WITH tset(td)
AS (
SELECT CAST('2018-08-01' AS DATE) AS td
UNION ALL
SELECT DATEADD(d, -1, td)
FROM tset
WHERE td > CAST('2018-06-01' AS DATE))
-- Actual examples start here
SELECT td
-- Diff is just showing the difference in days so you can see the group assignements
, DATEDIFF(d, td, GETDATE()) AS diff
-- This column groups into < 30, 31-60, and > 60
, CASE
WHEN DATEDIFF(d, td, GETDATE()) < 30 THEN 1 ELSE CASE
WHEN DATEDIFF(d, td, GETDATE()) < 60 THEN 2 ELSE 3
END
END three_sets
-- this example will group into any number of 30 day sets.
, cast ( DATEDIFF(d, td, GETDATE()) as int) / 30 any_number_of_sets
FROM tset
ORDER BY td;
您使用的是什么数据库管理系统?Then不一定是布尔值,但我认为您的问题是Then和ELSE中的类型不同(即Then是decimal,ELSE是varchar)。插入代码时,可以使用“{}”按钮添加格式。
td diff three_sets any_number_of_sets
2018-06-01 66 3 2
2018-06-02 65 3 2
2018-06-03 64 3 2
. . .
2018-06-12 55 2 1
2018-06-13 54 2 1
2018-06-14 53 2 1
. . .
2018-07-09 28 1 0
2018-07-10 27 1 0
2018-07-11 26 1 0