Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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查询_Sql_Sql Server 2008 - Fatal编程技术网

复杂的SQL查询

复杂的SQL查询,sql,sql-server-2008,Sql,Sql Server 2008,我的数据库如下所示: 客户c_id、c_名称 产品p_id、p_名称、p_成本 购买p_id参考产品,c_id参考客户 我想在数据库中查询以下内容: 产品数量最多的客户:如果有两个或两个以上产品数量相同的客户,则考虑所有产品的总价 我已经尝试过了,但它显示了一个错误,即:附近的语法不正确 我正试图调试它,但毫无收获 任何人都可以调试这个查询,甚至建议一个更优化的查询吗 select a1.c_id, a1.c_name from (select c.c_id, c.c_name

我的数据库如下所示:

客户c_id、c_名称 产品p_id、p_名称、p_成本 购买p_id参考产品,c_id参考客户 我想在数据库中查询以下内容:

产品数量最多的客户:如果有两个或两个以上产品数量相同的客户,则考虑所有产品的总价 我已经尝试过了,但它显示了一个错误,即:附近的语法不正确

我正试图调试它,但毫无收获

任何人都可以调试这个查询,甚至建议一个更优化的查询吗

select a1.c_id, a1.c_name 
from
    (select c.c_id, c.c_name
     from Customer c
     where c.c_id in
         (select b.c_id
          from Buys b
          group by b.c_id
          having COUNT(b.p_id) >= all
          (
              select COUNT(b.p_id)
              from Buys b
              group by b.c_id
          )
    )
) 
as a1 
join 
   (select b.c_id, SUM(p.p_cost) as 'SumCost'
    from Buys b 
    join Products p on b.p_id = p.p_id
    group by b.c_id
   ) as a2 on a1.c_id = a2.c_id
where 
    a2.SumCost = (select MAX(SumCost)
                  from 
                      (select b.c_id, SUM(p.p_cost) as 'SumCost' 
                       from Buys b 
                       join Products p on b.p_id = p.p_id
                       group by b.c_id
                      )
                 )
更新:

mohdowais已成功调试上述查询。 我现在认为这个查询效率不高。你们能推荐一个更有效的吗?
我没有使用任何索引。

您需要为最后一次选择添加派生表说明符:

select a1.c_id,a1.c_name 
    from
    (
    select c.c_id,c.c_name
    from Customer c
    where c.c_id in
    (
        select b.c_id
        from Buys b
        group by b.c_id
        having COUNT(b.p_id)>=all
        (
            select COUNT(b.p_id)
            from Buys b
            group by b.c_id
        )
    )
) 
as a1 join   (
                    select b.c_id,SUM(p.p_cost) as 'SumCost'
                    from Buys b join Products p on (b.p_id=p.p_id)
                    group by b.c_id
                 )  
                 as a2  on (a1.c_id=a2.c_id) 

                 where a2.SumCost=
                 (
                    select MAX(SumCost)
                    from 
                    (
                        select b.c_id,SUM(p.p_cost) as 'SumCost' 
                        from Buys b join Products p on (b.p_id=p.p_id)
                        group by b.c_id
                    ) maxTmp    -- <-----------------
                 )

[如果没有表模式、索引和示例数据,我无法评论您的查询的正确性或效率]

@Mitch-Wheat:谢谢。成功了。但我需要更多地了解它。请您详细说明一下maxTmp好吗?当从选择结果中选择时,您必须为选择结果提供别名:select*from select*from blah someAlias