Php SQL Server与分组依据的联接查询

Php SQL Server与分组依据的联接查询,php,sql,join,group-by,Php,Sql,Join,Group By,我有这个查询,我想检索结果,只按Banksphere.servicio_id分组,不做任何sum(XXX),因为每个Banksphere.servicio_id都有自己的值,由hora_id、peticion_id和valor分隔 所以结果一定是这样的 [servicio_id 1] (hora_id 1) (hora_id 2) (hora_id 3) ... (peticion_id 1) (peticion_id 2) ... (media 1) (media 1) ...

我有这个查询,我想检索结果,只按
Banksphere.servicio_id
分组,不做任何
sum(XXX)
,因为每个
Banksphere.servicio_id
都有自己的值,由
hora_id
peticion_id
valor
分隔

所以结果一定是这样的

[servicio_id 1]
(hora_id 1) (hora_id 2) (hora_id 3)   ... 
(peticion_id 1) (peticion_id 2)    ...
(media 1) (media 1) ...    
(valor 1) (valor 1) ...    
...
[servicio_id 2]
(hora_id 1)    
(peticion_id 1) (peticion_id 2) ...    
(media 2)    
(valor 2)    
...
我的问题到目前为止-

$sqlQuery = sqlsrv_query($conn, "
    SELECT
        Umbrales.minimo as media,
        uok.minimo as media_ok,
        Banksphere.id as valor_id,
        Umbrales.id as umbral_id,
        Banksphere.servicio_id,
        Banksphere.peticion_id,
        Banksphere.hora_id,
        Banksphere.valor
    FROM
        Banksphere
    LEFT JOIN
        Umbrales
    ON Banksphere.hora_id = Umbrales.hora_id 
    AND Banksphere.dia_id = Umbrales.dia_id 
    AND Banksphere.servicio_id = Umbrales.servicio_id
    AND Banksphere.entidad_id = Umbrales.entidad_id 
    AND Banksphere.peticion_id = Umbrales.peticion_id
    LEFT JOIN
        Umbrales uok
    ON Banksphere.hora_id = uok.hora_id 
    AND Banksphere.dia_id = uok.dia_id 
    AND Banksphere.servicio_id = uok.servicio_id
    AND Banksphere.entidad_id = uok.entidad_id 
    AND uok.peticion_id = 0
    WHERE
        Banksphere.entidad_id = '$entidad_id'
    AND
        Banksphere.reconocido = 0
    AND
        Banksphere.fecha = '$fecha'
    ORDER BY
        Banksphere.servicio_id ASC,
        Banksphere.hora_id DESC,
        Banksphere.peticion_id ASC
");

短篇小说:使用顺序,而不是分组;使用MySQL;或者使用PostgreSQL并密切关注您的函数依赖关系。(我不确定PostgreSQL是否适用于您的情况。)

如果Banksphere.servicio_id在结果集中是唯一的,请使用ORDER BY而不是GROUP BY。如果Banksphere.servicio_id在此上下文中是唯一的,则使用ORDER BY的结果集是确定的,但使用GROUP BY的结果集不是确定的

首先,看看这个简单的表

create table test (
  column_1 integer,
  column_2 integer,
  column_3 integer 
  );

insert into test values
(1, 2, 3),
(1, 4, 5),
(1, 6, 7),
(1, 8, 9);
按列分组会为该列中的每个值返回一行

select column_1, column_2, column_3
from test
group by column_1
此查询应返回一行。当然,“column_1”中的值将是1。“第2列”和“第3列”中将显示什么值

你不知道。事实上,在任何符合标准的SQL dbms中,这个查询都会给您一个语法错误,因为您没有告诉它如何处理“column_2”和“column_3”。在兼容的SQL dbms中,“column_2”和“column_3”必须是GROUP BY的一部分,或者在聚合函数中使用

MySQL将毫无错误地执行这种查询,但是。“服务器可以从每个组中自由选择任何值,因此,除非它们相同,否则选择的值是不确定的。”MySQL将此称为功能。我称之为“按设计破坏”——不确定在SQL dbms中不是一个可防御的特性

在稍有不同的条件下,兼容dbms可以返回未聚合的列,而无需将它们显式地包含在GROUP BY中。例如,PostgreSQL将允许您

存在“分组依据”时,它对“选择”列表无效 用于引用未分组列(聚合中除外)的表达式 函数或如果未分组列在功能上依赖于 分组列,因为否则可能会有多个列 要为未分组的列返回的值。函数依赖关系 如果分组列(或其子集)是主列,则存在 包含未分组列的表的键。[重点补充]

因此,在PostgreSQL 9.1+中,可以查询具有如下主键的表

create table test (
  column_1 integer,
  column_2 integer,
  column_3 integer,
  column_4 integer,
  primary key (column_1, column_2)
  );

insert into test values
(1, 1, 2, 3),
(1, 2, 4, 5),
(1, 3, 6, 7),
(1, 4, 8, 9);

select column_1, column_2, column_3, column_4
from test
group by column_1, column_2
order by column_1, column_2

COLUMN_1  COLUMN_2  COLUMN_3  COLUMN_4
--
1         1         2         3
1         2         4         5
1         3         6         7
1         4         8         9
列“column_3”和“column_4”不是GROUP BY的一部分,也不是任何聚合函数的一部分。但是GROUPBY中的列和select列表中的未聚合列之间存在函数依赖关系,因此结果是确定的


据我所知,PostgreSQL是目前唯一实现SQL标准这一部分的SQL dbms。(标准比PostgreSQL更进一步。)

您想要分组;但是GROUP BY语句丢失了?我不能使用GROUP BY,因为连接已经尝试过了,它返回了一个错误,因为左连接本影…那么什么错误?!你对你的问题很模糊。