dongwei7048 2017-10-09 05:59 采纳率: 100%
浏览 75
已采纳

如何在sql和php中填充min值? [关闭]

I have a table in SQL:

Table 1

date       name  s1   s2   s3   s4   min_value
7/10/2017  ram   10   11   12   13   11
7/10/2017  tom   17   16   15   14   16
7/10/2017  ara   13   9    26   22   9

If I total all fields in a single date then

s1 total value 40
s2 total value 36
s3 total value 53
s4 total value 49

Here minimum value is 36. It means minimum value column is s2.

I inserted s2 field value in min_value field respectively.

My question is how will I fill min_value field?

Help me please.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongzhun4898 2017-10-09 06:12
    关注

    Here's code to achieve that:

    SELECT [date], s1, s2, s3, s4,
        case when s1 < s2 and s1 < s3 and s1 < s4 then s1
             when s2 < s1 and s2 < s3 and s2 < s4 then s2
             when s3 < s1 and s3 < s2 and s3 < s4 then s3
             else s4 end as [min_value]
    FROM (
        SELECT [date], sum(s1) AS s1, sum(s2) AS s2, sum(s3) AS s3, sum(s4) AS s4 FROM [Table1]
        GROUP BY [date]
    ) AS A
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)