SQL 在子查询上选择计数的服务器错误
SQL Server error selecting count on a subquery
我正在尝试 select 计算查询中的行数 return。查询是
Select
a.itm_cd, max(b.doc_num) ,max(c.text)
from
ist b, itm_trn a, ist_cmnt c
where
a.ist_seq_num = b.ist_seq_num
and a.ist_seq_num = c.ist_seq_num
and a.ist_wr_dt = b.ist_wr_dt
and a.new_loc_cd like 'BOX115'
and a.ITT_CD = 'XFR' and a.create_dt >'21-AUG-16'
group by
a.itm_cd;
对于我 return 3 行的这个特定查询,我需要编写一个查询 return 有多少行 returned。
我已经试过了:
Select
count(*)
from
(Select
a.itm_cd, max(b.doc_num), max(c.text)
from
ist b,itm_trn a, ist_cmnt c
where
a.ist_seq_num = b.ist_seq_num
and a.ist_seq_num = c.ist_seq_num
and a.ist_wr_dt = b.ist_wr_dt
and a.new_loc_cd like 'BOX115'
and a.ITT_CD = 'XFR'
and a.create_dt > '21-AUG-16'
group by
a.itm_cd);
这会导致语法错误
Msg 102, level 15, state 1 line 1
Incorrect syntax near ')'.
我不确定我做错了什么,我有一个类似的 SQL 语句在 Oracle 中以这种方式工作,但没有找到我在 SQL 服务器中搞砸的地方。
更新:
根据我收到的第一个建议,我尝试了:
Select
count(*)
from
(Select
a.itm_cd, max(b.doc_num), max(c.text)
from
ist b, itm_trn a, ist_cmnt c
where
a.ist_seq_num = b.ist_seq_num
and a.ist_seq_num = c.ist_seq_num
and a.ist_wr_dt = b.ist_wr_dt
and a.new_loc_cd like 'BOX115'
and a.ITT_CD = 'XFR'
and a.create_dt > '21-AUG-16'
group by
a.itm_cd) as T
错误 returned 是
Msg 8155, Level 16, State 2, Line 12
No column was specified for column 2 of 'T'.
Msg 8155, Level 16, State 2, Line 12
No column was specified for column 3 of 'T'.
根据错误消息,您应该为聚合列添加别名。像这样尝试,
SELECT count(*)
FROM (
SELECT a.itm_cd
,max(b.doc_num) AS MaxDoc_num
,max(c.TEXT) AS MaxText
FROM ist b
,itm_trn a
,ist_cmnt c
WHERE a.ist_seq_num = b.ist_seq_num
AND a.ist_seq_num = c.ist_seq_num
AND a.ist_wr_dt = b.ist_wr_dt
AND a.new_loc_cd LIKE 'BOX115'
AND a.ITT_CD = 'XFR'
AND a.create_dt > '21-AUG-16'
GROUP BY a.itm_cd
) T
您可以使用此查询轻松找到计数。
SELECT count(DISTINCT a.itm_cd)
FROM ist b
,itm_trn a
,ist_cmnt c
WHERE a.ist_seq_num = b.ist_seq_num
AND a.ist_seq_num = c.ist_seq_num
AND a.ist_wr_dt = b.ist_wr_dt
AND a.new_loc_cd LIKE 'BOX115'
AND a.ITT_CD = 'XFR'
AND a.create_dt > '21-AUG-16'
我正在尝试 select 计算查询中的行数 return。查询是
Select
a.itm_cd, max(b.doc_num) ,max(c.text)
from
ist b, itm_trn a, ist_cmnt c
where
a.ist_seq_num = b.ist_seq_num
and a.ist_seq_num = c.ist_seq_num
and a.ist_wr_dt = b.ist_wr_dt
and a.new_loc_cd like 'BOX115'
and a.ITT_CD = 'XFR' and a.create_dt >'21-AUG-16'
group by
a.itm_cd;
对于我 return 3 行的这个特定查询,我需要编写一个查询 return 有多少行 returned。
我已经试过了:
Select
count(*)
from
(Select
a.itm_cd, max(b.doc_num), max(c.text)
from
ist b,itm_trn a, ist_cmnt c
where
a.ist_seq_num = b.ist_seq_num
and a.ist_seq_num = c.ist_seq_num
and a.ist_wr_dt = b.ist_wr_dt
and a.new_loc_cd like 'BOX115'
and a.ITT_CD = 'XFR'
and a.create_dt > '21-AUG-16'
group by
a.itm_cd);
这会导致语法错误
Msg 102, level 15, state 1 line 1
Incorrect syntax near ')'.
我不确定我做错了什么,我有一个类似的 SQL 语句在 Oracle 中以这种方式工作,但没有找到我在 SQL 服务器中搞砸的地方。
更新:
根据我收到的第一个建议,我尝试了:
Select
count(*)
from
(Select
a.itm_cd, max(b.doc_num), max(c.text)
from
ist b, itm_trn a, ist_cmnt c
where
a.ist_seq_num = b.ist_seq_num
and a.ist_seq_num = c.ist_seq_num
and a.ist_wr_dt = b.ist_wr_dt
and a.new_loc_cd like 'BOX115'
and a.ITT_CD = 'XFR'
and a.create_dt > '21-AUG-16'
group by
a.itm_cd) as T
错误 returned 是
Msg 8155, Level 16, State 2, Line 12
No column was specified for column 2 of 'T'.Msg 8155, Level 16, State 2, Line 12
No column was specified for column 3 of 'T'.
根据错误消息,您应该为聚合列添加别名。像这样尝试,
SELECT count(*)
FROM (
SELECT a.itm_cd
,max(b.doc_num) AS MaxDoc_num
,max(c.TEXT) AS MaxText
FROM ist b
,itm_trn a
,ist_cmnt c
WHERE a.ist_seq_num = b.ist_seq_num
AND a.ist_seq_num = c.ist_seq_num
AND a.ist_wr_dt = b.ist_wr_dt
AND a.new_loc_cd LIKE 'BOX115'
AND a.ITT_CD = 'XFR'
AND a.create_dt > '21-AUG-16'
GROUP BY a.itm_cd
) T
您可以使用此查询轻松找到计数。
SELECT count(DISTINCT a.itm_cd)
FROM ist b
,itm_trn a
,ist_cmnt c
WHERE a.ist_seq_num = b.ist_seq_num
AND a.ist_seq_num = c.ist_seq_num
AND a.ist_wr_dt = b.ist_wr_dt
AND a.new_loc_cd LIKE 'BOX115'
AND a.ITT_CD = 'XFR'
AND a.create_dt > '21-AUG-16'