Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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_Tsql_Sql Server 2005 - Fatal编程技术网

Sql 按子查询的结果分组

Sql 按子查询的结果分组,sql,tsql,sql-server-2005,Sql,Tsql,Sql Server 2005,我试图在GROUPBY语句中使用子查询的结果,但发现这是不允许的。我认为用CTE可以做到这一点,但我不太确定。我正在为SQLServer2005创建此SQL代码。我显然不是专家。这是我写的SQL语句 select sum(paxon), (select id from runs as RS where RS.code = RL.runsegment) as port_id, date from runlogs as RL where date BETWEEN '07/09

我试图在GROUPBY语句中使用子查询的结果,但发现这是不允许的。我认为用CTE可以做到这一点,但我不太确定。我正在为SQLServer2005创建此SQL代码。我显然不是专家。这是我写的SQL语句

select
  sum(paxon), 
  (select id from runs as RS where RS.code = RL.runsegment) as port_id,
  date
from
  runlogs as RL
where 
  date BETWEEN '07/09/2012' and '07/16/2012'     
  and account in ('311,312,313')
  and runsegment in (select code from runsegments where org_id = 13)
group by
  date,
  port_id

当我尝试运行此程序时,我被告知port_id是无效的列名。我非常确定,通过创建临时表或CTE或可能使用联接,这是可以做到的,但我不确定如何做到这一点。任何帮助都将不胜感激。

下面是一个选项

select sum(sum_paxon), date, port_id from
(
select paxon,

(select id from runs as RS where RS.code = RL.runsegment) as port_id,

date from runlogs as RL

where

date BETWEEN '07/09/2012' and '07/16/2012'

and account in ('311,312,313')

and runsegment in (select code from runsegments where org_id = 13)
 ) Z
group by date, port_id

下面是一个选项

select sum(sum_paxon), date, port_id from
(
select paxon,

(select id from runs as RS where RS.code = RL.runsegment) as port_id,

date from runlogs as RL

where

date BETWEEN '07/09/2012' and '07/16/2012'

and account in ('311,312,313')

and runsegment in (select code from runsegments where org_id = 13)
 ) Z
group by date, port_id

像这样的怎么样?除非我遗漏了什么,否则我认为不需要子查询

SELECT SUM(rl.paxon)
    ,r.id AS port_id
    ,rl.[date]
FROM runlogs rl
    INNER JOIN runs r
        ON r.code = rl.runsegment
    INNER JOIN runsegments rs
        ON rs.code = rl.runsegment
WHERE rl.[date] BETWEEN '20120709' AND '20120716'  
    AND rl.account in ('311','312','313')
    AND rs.org_id = 13
GROUP BY rl.[date], r.id
我不确定“311312313”中的帐户试图做什么。在我的回答中,我假设帐户是nvarchar或varchar,并且这三个项目实际上是不同的帐户值。如果account是int,那么该行应该是:和311312313中的rl.account


我还更改了您的日期以使用首选日期。

这样的日期怎么样?除非我遗漏了什么,否则我认为不需要子查询

SELECT SUM(rl.paxon)
    ,r.id AS port_id
    ,rl.[date]
FROM runlogs rl
    INNER JOIN runs r
        ON r.code = rl.runsegment
    INNER JOIN runsegments rs
        ON rs.code = rl.runsegment
WHERE rl.[date] BETWEEN '20120709' AND '20120716'  
    AND rl.account in ('311','312','313')
    AND rs.org_id = 13
GROUP BY rl.[date], r.id
我不确定“311312313”中的帐户试图做什么。在我的回答中,我假设帐户是nvarchar或varchar,并且这三个项目实际上是不同的帐户值。如果account是int,那么该行应该是:和311312313中的rl.account


我还更改了您的日期以使用首选日期。

我错误地键入了顶部端口。所有实例读取端口\ id您是否尝试过…按日期分组,从运行中选择id作为RS,其中RS.code=RL.runsegment?并不是说它会是最优的,我想我也会使用[date],而不是date。告诉我不能在表达式中使用聚合或子查询,该表达式用于group by子句的group by列表,因为我担心它不起作用。不过值得一试:PI错误地输入了顶部端口。所有实例读取端口\ id您是否尝试过…按日期分组,从运行中选择id作为RS,其中RS.code=RL.runsegment?并不是说它会是最优的,我想我也会使用[date],而不是date。告诉我不能在表达式中使用聚合或子查询,该表达式用于group by子句的group by列表,因为我担心它不起作用。但是值得一试:PYES!!!谢谢你的帮助!我一直在推迟学习加入,因为我只是一个非常兼职的程序员,但这是一个很好的开始。甚至还添加了我自己的另一个连接,以从第三个表中获取端口的名称,这一切都成功了!很高兴听到这个消息,祝你好运!别忘了接受答案对谢谢你的帮助!我一直在推迟学习加入,因为我只是一个非常兼职的程序员,但这是一个很好的开始。甚至还添加了我自己的另一个连接,以从第三个表中获取端口的名称,这一切都成功了!很高兴听到这个消息,祝你好运!别忘了接受答案