Sql server SQL查询,根据现有列上的几个条件获取计算列
我正在处理一个查询,以获得一个分数,该分数将作为添加到现有表中的列,并且只有在五个项目(item1、item2、item3、item4、item5)中至少有三个项目(值为0、1或2)已完成时才进行计算。other wise score设置为missing并设置为99。 分数=(项目值之和/有效完成项目数)x项目数 下图中给出的输入来自派生表 我应该如何实现查询以获得预期结果? 任何帮助都将不胜感激。试试这个:Sql server SQL查询,根据现有列上的几个条件获取计算列,sql-server,sql-server-2008,sql-server-2012,Sql Server,Sql Server 2008,Sql Server 2012,我正在处理一个查询,以获得一个分数,该分数将作为添加到现有表中的列,并且只有在五个项目(item1、item2、item3、item4、item5)中至少有三个项目(值为0、1或2)已完成时才进行计算。other wise score设置为missing并设置为99。 分数=(项目值之和/有效完成项目数)x项目数 下图中给出的输入来自派生表 我应该如何实现查询以获得预期结果? 任何帮助都将不胜感激。试试这个: SELECT item1, item2, item3, item4, item5,
SELECT item1, item2, item3, item4, item5,
CASE
WHEN t.n >= 3 THEN t.s/(t.n * 1.0)
ELSE 99
END AS Score
FROM mytable
CROSS APPLY (
SELECT SUM(CASE WHEN item IN (0, 1, 2) THEN item ELSE 0 END),
COUNT(CASE WHEN item IN (0, 1, 2) THEN 1 END)
FROM (VALUES (item1), (item2), (item3), (item4), (item5)) AS x (item) ) AS t(s, n)
试试这个:
SELECT item1, item2, item3, item4, item5,
CASE
WHEN t.n >= 3 THEN t.s/(t.n * 1.0)
ELSE 99
END AS Score
FROM mytable
CROSS APPLY (
SELECT SUM(CASE WHEN item IN (0, 1, 2) THEN item ELSE 0 END),
COUNT(CASE WHEN item IN (0, 1, 2) THEN 1 END)
FROM (VALUES (item1), (item2), (item3), (item4), (item5)) AS x (item) ) AS t(s, n)
交叉应用的用法稍有不同
with d as (
-- sample data
select *
from (
values
(1,1,2,2,4),
(9,1,9,9,4)
) t(item1,item2,item3,item4,item5)
)
-- query
select *, case when n >=3 then (s + .0)/n else 99 end score
from d
cross apply (
select sum(itm) s, count(*) n from(
select item1 itm where item1 between 0 and 2
union all
select item2 itm where item2 between 0 and 2
union all
select item3 itm where item3 between 0 and 2
union all
select item4 itm where item4 between 0 and 2
union all
select item5 itm where item5 between 0 and 2
) t2
) t
交叉应用的用法稍有不同
with d as (
-- sample data
select *
from (
values
(1,1,2,2,4),
(9,1,9,9,4)
) t(item1,item2,item3,item4,item5)
)
-- query
select *, case when n >=3 then (s + .0)/n else 99 end score
from d
cross apply (
select sum(itm) s, count(*) n from(
select item1 itm where item1 between 0 and 2
union all
select item2 itm where item2 between 0 and 2
union all
select item3 itm where item3 between 0 and 2
union all
select item4 itm where item4 between 0 and 2
union all
select item5 itm where item5 between 0 and 2
) t2
) t
我想您显示的是表行,item1到item5是表的列 一种非常简单易读的方法是查看项目,计算值为0、1和2的项目,然后将它们相加。然后使用此结果应用公式:
select item1, item2, item3, item4, item5,
case when count_items >= 3 then sum_items / count_items * 5 else 99 end
from
(
select item1, item2, item3, item4, item5,
case when item1 in (0,1,2) then item1 else 0 end +
case when item2 in (0,1,2) then item2 else 0 end +
case when item3 in (0,1,2) then item3 else 0 end +
case when item4 in (0,1,2) then item4 else 0 end +
case when item5 in (0,1,2) then item5 else 0 end as sum_items,
case when item1 in (0,1,2) then 1 else 0 end +
case when item2 in (0,1,2) then 1 else 0 end +
case when item3 in (0,1,2) then 1 else 0 end +
case when item4 in (0,1,2) then 1 else 0 end +
case when item5 in (0,1,2) then 1 else 0 end as count_items
from mytable
) summed;
我想您显示的是表行,item1到item5是表的列 一种非常简单易读的方法是查看项目,计算值为0、1和2的项目,然后将它们相加。然后使用此结果应用公式:
select item1, item2, item3, item4, item5,
case when count_items >= 3 then sum_items / count_items * 5 else 99 end
from
(
select item1, item2, item3, item4, item5,
case when item1 in (0,1,2) then item1 else 0 end +
case when item2 in (0,1,2) then item2 else 0 end +
case when item3 in (0,1,2) then item3 else 0 end +
case when item4 in (0,1,2) then item4 else 0 end +
case when item5 in (0,1,2) then item5 else 0 end as sum_items,
case when item1 in (0,1,2) then 1 else 0 end +
case when item2 in (0,1,2) then 1 else 0 end +
case when item3 in (0,1,2) then 1 else 0 end +
case when item4 in (0,1,2) then 1 else 0 end +
case when item5 in (0,1,2) then 1 else 0 end as count_items
from mytable
) summed;
需要更多specific@Ullas你能告诉我我还需要提供什么吗?需要更多吗specific@Ullas你能告诉我我还需要提供什么吗?谢谢,我在查询结尾的“')”附近得到了错误的语法,并且你在子查询中使用了“item”(如果item在(0,1,2)中)。请告诉我遗漏了什么?很抱歉,关键字“AS”附近的语法不正确。“此处-->AS x(项),我认为与其他解决方案相比,您的做法是经过优化的。@Giorgos Betsos真棒,它可以工作!!这是经过优化的一个。谢谢,你能解释一下这段代码“FROM(VALUES(item1),(item2),(item3),(item4),(item5))AS x(item))AS t(s,n)”@mano its a:它使用一个值列表创建了一个内嵌表。谢谢你的帮助。谢谢,我在查询的末尾使用了不正确的语法,你使用了“item”在子查询中(项目位于(0、1、2)中时的情况)。请告诉我遗漏了什么?很抱歉,关键字“AS”附近的语法不正确。“此处-->AS x(项),我认为与其他解决方案相比,您的做法是经过优化的。@Giorgos Betsos真棒,它可以工作!!这是优化的一个谢谢,你能解释一下这个代码“从(值(项目1),(项目2),(项目3),(项目4),(项目5))到x(项目))到t(s,n)”吗@mano Its a:它使用值列表创建一个内嵌表。感谢您帮助我完成Giorgos给出了优化的方法,请原谅,因此我接受他的回答无需担心;-)去你喜欢的,并考虑更可读和可维护。我喜欢Giorgos的解决方案,但还是更喜欢我的,因为它很容易理解。你看着它,立刻明白它是如何工作的(我想)。但我相信很多人更喜欢Giorgos的方法,因为它的优雅。谢谢你理解Giorgos给出了优化的方法,所以请原谅,因此我接受他的回答无需担心;-)去你喜欢的,并考虑更可读和可维护。我喜欢Giorgos的解决方案,但还是更喜欢我的,因为它很容易理解。你看着它,立刻明白它是如何工作的(我想)。但我相信很多人更喜欢Giorgos的优雅风格。谢谢你的理解