SQL列计算最小值
SQL column calculation mininimum value
我有一个名为 calcu
的 table
id date name s1 s2 s3 s4 min_value
1 10/10/2017 dicky 7 4 8 9 [4]
2 10/10/2017 acton 12 15 17 19 [15]
3 10/10/2017 adney 28 13 19 14 [13]
------------------------------------------------------------------
when total by date 47 32 44 42
此处的最小列值是 s2
[32],这就是为什么 s2
值 = min_value
列。
现在没有问题了。但是,当 s1, s2, s3, s4
值中的任何字段与任何字段的 [see below example
] 相等时,min_value 字段加倍,所有列加倍。
示例:
id date name s1 s2 s3 s4 min_value
1 10/10/2017 dicky 7 24 8 11 [8]/[11]
2 10/10/2017 acton 12 15 17 19 [17]/[19]
3 10/10/2017 adney 28 13 19 14 [19]/[14]
------------------------------------------------------------------
when total by date 47 52 44 44
此处的最小值列是 s3
和 s4
、
我需要 s3 or s4
中的任何列,这意味着 s3
或 s4
列将填充到 min_value
列中。
see the problem here with sqlfiddle
我正在使用 MySQL.
根据您的 sqlfiddle,您需要在嵌套查询之外添加一个 GROUP BY 以实现您想要的结果。
select c.id, c.date, c.name, c.s1, c.s2, c.s3, c.s4,
case v.s
when 1 then c.s1
when 2 then c.s2
when 3 then c.s3
when 4 then c.s4
end as min_value
from calcu c
join (
select date, s, sum(val) val_sum
from ( #unpivot your data
select date, s1 as val, 1 as s
from calcu
union all
select date, s2 as val, 2 as s
from calcu
union all
select date, s3 as val, 3 as s
from calcu
union all
select date, s4 as val, 4 as s
from calcu
) x
group by date, s
) v on c.date = v.date
where not exists ( #we are only interested in the minimum val_sum above
select 1
from ( #note this is the same derived table as above
select date, s, sum(val) val_sum
from (
select date, s1 as val, 1 as s
from calcu
union all
select date, s2 as val, 2 as s
from calcu
union all
select date, s3 as val, 3 as s
from calcu
union all
select date, s4 as val, 4 as s
from calcu
) x
group by date, s
) v2
where v2.date = v.date
and v2.val_sum < v.val_sum
) GROUP BY c.id # This is the addition you need
查看 运行 解决方案 here
我有一个名为 calcu
id date name s1 s2 s3 s4 min_value
1 10/10/2017 dicky 7 4 8 9 [4]
2 10/10/2017 acton 12 15 17 19 [15]
3 10/10/2017 adney 28 13 19 14 [13]
------------------------------------------------------------------
when total by date 47 32 44 42
此处的最小列值是 s2
[32],这就是为什么 s2
值 = min_value
列。
现在没有问题了。但是,当 s1, s2, s3, s4
值中的任何字段与任何字段的 [see below example
] 相等时,min_value 字段加倍,所有列加倍。
示例:
id date name s1 s2 s3 s4 min_value
1 10/10/2017 dicky 7 24 8 11 [8]/[11]
2 10/10/2017 acton 12 15 17 19 [17]/[19]
3 10/10/2017 adney 28 13 19 14 [19]/[14]
------------------------------------------------------------------
when total by date 47 52 44 44
此处的最小值列是 s3
和 s4
、
我需要 s3 or s4
中的任何列,这意味着 s3
或 s4
列将填充到 min_value
列中。
see the problem here with sqlfiddle
我正在使用 MySQL.
根据您的 sqlfiddle,您需要在嵌套查询之外添加一个 GROUP BY 以实现您想要的结果。
select c.id, c.date, c.name, c.s1, c.s2, c.s3, c.s4,
case v.s
when 1 then c.s1
when 2 then c.s2
when 3 then c.s3
when 4 then c.s4
end as min_value
from calcu c
join (
select date, s, sum(val) val_sum
from ( #unpivot your data
select date, s1 as val, 1 as s
from calcu
union all
select date, s2 as val, 2 as s
from calcu
union all
select date, s3 as val, 3 as s
from calcu
union all
select date, s4 as val, 4 as s
from calcu
) x
group by date, s
) v on c.date = v.date
where not exists ( #we are only interested in the minimum val_sum above
select 1
from ( #note this is the same derived table as above
select date, s, sum(val) val_sum
from (
select date, s1 as val, 1 as s
from calcu
union all
select date, s2 as val, 2 as s
from calcu
union all
select date, s3 as val, 3 as s
from calcu
union all
select date, s4 as val, 4 as s
from calcu
) x
group by date, s
) v2
where v2.date = v.date
and v2.val_sum < v.val_sum
) GROUP BY c.id # This is the addition you need
查看 运行 解决方案 here