小弟学sql不久,对下面的语句有点搞不清,请大家指教下 1 select name 姓名,
(case subject when '语文' then result else 0 end) 语文,
(case subject when '数学' then result else 0 end) 数学,
(case subject when '物理' then result else 0 end) 物理
from tb 2 select name 姓名,
max(case subject when '语文' then result else 0 end) 语文,
max(case subject when '数学' then result else 0 end) 数学,
max(case subject when '物理' then result else 0 end) 物理
from tb
group by name我想问下为什么第一条sql语句不要用group by ,而第二条语句要用group by呢
(case subject when '语文' then result else 0 end) 语文,
(case subject when '数学' then result else 0 end) 数学,
(case subject when '物理' then result else 0 end) 物理
from tb 2 select name 姓名,
max(case subject when '语文' then result else 0 end) 语文,
max(case subject when '数学' then result else 0 end) 数学,
max(case subject when '物理' then result else 0 end) 物理
from tb
group by name我想问下为什么第一条sql语句不要用group by ,而第二条语句要用group by呢
聚合函数对一组值执行计算并返回单一的值。除 COUNT 函数之外,聚合函数忽略空值。聚合函数经常与 SELECT 语句的 GROUP BY 子句一同使用。所有聚合函数都具有确定性。任何时候用一组给定的输入值调用它们时,都返回相同的值。有关函数确定性的更多信息,请参见确定性函数和非确定性函数。仅在下列项中聚合函数允许作为表达式使用: SELECT 语句的选择列表(子查询或外部查询)。
COMPUTE 或 COMPUTE BY 子句。
HAVING 子句。
Transact-SQL 编程语言提供下列聚合函数:AVG MAX
BINARY_CHECKSUM MIN
CHECKSUM SUM
CHECKSUM_AGG STDEV
COUNT STDEVP
COUNT_BIG VAR
GROUPING VARP
第一句没有用到max,第二句有用到max。。
用到聚合函数时需要用到group by