Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server:联接上的子查询_Sql_Sql Server - Fatal编程技术网

SQL Server:联接上的子查询

SQL Server:联接上的子查询,sql,sql-server,Sql,Sql Server,我有两个带有模式和数据的表,如下所示。表A有一个id和一个相关的名称。表B将表A中的id与价格和其他属性关联起来。对于表A中的每个条目,表B中可能有多个具有不同价格的条目,但是otherAttr对于每个条目都是相同的 给定表A的id,我想选择名称,其他属性,以及最低价格 下面的查询返回多个结果,我需要编写一个查询,以最低价格返回单个结果 SELECT a.name, b.price, b.otherAttr FROM A a LEFT Join B b on b.idFromA = 1 WHE

我有两个带有模式和数据的表,如下所示。表A有一个
id
和一个相关的
名称
。表B将
表A
中的
id
价格和
其他属性关联起来。对于表A中的每个条目,表B中可能有多个具有不同价格的条目,但是
otherAttr
对于每个条目都是相同的

给定表A的
id
,我想选择
名称
其他属性
,以及最低
价格

下面的查询返回多个结果,我需要编写一个查询,以最低价格返回单个结果

SELECT a.name, b.price, b.otherAttr 
FROM A a
LEFT Join B b on b.idFromA = 1
WHERE a.id = 1


    Table A        Table B 
    id | name      idFromA | price | otherAttr
    --------       ---------------------------
    1 | A          1       | 200   | abc
    2 | B          1       | 300   | abc
                   1       | 400   | abc
                   2       | 20    | def
                   2       | 30    | def
                   2       | 40    | ef
我过分简化了我的例子。除了从表B中选择最小价格和其他属性外,我还必须从其他表上的联接中选择一组其他属性。这就是为什么我认为GROUPBY和Min应该是表B上联接的子查询,以避免按我正在选择的所有属性进行分组(因为为其选择的属性会以编程方式发生变化)

实际查询看起来更像:

SELECT a.name, b.price, b.otherAttr, c.x, c.y, d.e, d.f, g.h....
FROM A a
LEFT Join B b on b.idFromA = 1
LEFT Join C c on something...
LEFT Join D d on something...
LEFT Join G g on something...

WHERE a.id = 1

要获得此信息,可以在内部查询中使用GROUP BY:

SELECT gd.name, gd.price,gd.otherAttr, c.x, c.y, d.e, d.f, g.h....
FROM 
(SELECT a.id,a.name, MIN(b.price) as price, b.otherAttr 
FROM A a
LEFT Join B b on b.idFromA = 1
WHERE a.id = 1
GROUP BY a.id,a.name,b.otherAttr) gd
LEFT Join B b on b.idFromA = 1
LEFT Join C c on something...
LEFT Join D d on something...
LEFT Join G g on something...

要获得此信息,可以在内部查询中使用GROUP BY:

SELECT gd.name, gd.price,gd.otherAttr, c.x, c.y, d.e, d.f, g.h....
FROM 
(SELECT a.id,a.name, MIN(b.price) as price, b.otherAttr 
FROM A a
LEFT Join B b on b.idFromA = 1
WHERE a.id = 1
GROUP BY a.id,a.name,b.otherAttr) gd
LEFT Join B b on b.idFromA = 1
LEFT Join C c on something...
LEFT Join D d on something...
LEFT Join G g on something...
尝试:-

尝试:-


您可以这样做:

SELECT a.name, MIN(b.price), MIN(b.otherAttr) 
FROM TableA a
LEFT JOIN TableB b on b.idFromA = a.id
GROUP BY a.name
HAVING a.id = 1;

您可以这样做:

SELECT a.name, MIN(b.price), MIN(b.otherAttr) 
FROM TableA a
LEFT JOIN TableB b on b.idFromA = a.id
GROUP BY a.name
HAVING a.id = 1;

除了子查询上的id之外,还需要对price进行内部联接,以便将正确的记录与最低价格相交。然后TOP(1)将只返回其中一条记录。如果可以展开子查询中的条件和group by字段,则可以避免使用TOP(1),这样架构就可以确保只为该属性组合返回一条记录。最后,在与集合相交时避免左连接

SELECT TOP(1) p.id, p.price, b.OtherAttr
FROM B as b
INNER JOIN
    (SELECT A.id, min(B.price) as price
     FROM B
     INNER JOIN A on A.id=B.idFromA and A.ID=1
     GROUP BY A.id) as p on b.idFromA=p.id and b.price=p.price

除了子查询上的id之外,还需要对price进行内部联接,以便将正确的记录与最低价格相交。然后TOP(1)将只返回其中一条记录。如果可以展开子查询中的条件和group by字段,则可以避免使用TOP(1),这样架构就可以确保只为该属性组合返回一条记录。最后,在与集合相交时避免左连接

SELECT TOP(1) p.id, p.price, b.OtherAttr
FROM B as b
INNER JOIN
    (SELECT A.id, min(B.price) as price
     FROM B
     INNER JOIN A on A.id=B.idFromA and A.ID=1
     GROUP BY A.id) as p on b.idFromA=p.id and b.price=p.price