Sql server 使用聚合或子查询进行分组
我如何做到这一点(不是制作,知道它不漂亮): 输出是 Msg 130,级别15,状态1,第7行Sql server 使用聚合或子查询进行分组,sql-server,tsql,Sql Server,Tsql,我如何做到这一点(不是制作,知道它不漂亮): 输出是 Msg 130,级别15,状态1,第7行无法对包含聚合或子查询的表达式执行聚合函数 我只希望mat_no的子文档编码为“SUMS” 编辑2 不带子查询的输出 Index Cnt mat_no(not useful) 112565/11 25 12-61692 16601/11 12 12-58850 34934/11 12
无法对包含聚合或子查询的表达式执行聚合函数 我只希望mat_no的子文档编码为“SUMS” 编辑2 不带子查询的输出
Index Cnt mat_no(not useful)
112565/11 25 12-61692
16601/11 12 12-58850
34934/11 12 12-58854
34935/11 12 12-61983
704612/12 12 12-55487
712166/12 11 12-55613
707588/12 9 12-55604
91394/11 8 12-57115
渴望的
Index Cnt Mat_no that contains doc 'SUMS'
112565/11 25 12-61692
16601/11 12 12-58850
34934/11 12 12-58854
34935/11 12 12-61983
704612/12 12 12-55487
712166/12 11 12-55613
707588/12 10 12-55604
物料编号是与索引的多对一关系,具有子文档“SUMS”的特定物料编号是主记录。我需要关注的那个。对不起,我弄糊涂了。这就是你想要的吗
SELECT mat1_01_06, count(mat1_01_06), MAX(mat_no),
Mat_no = (
SELECT
MAX(document.mat_no)
FROM TimeMatters11.lntmu11.document
WHERE matter.sysid = document.mat_id and ccode = 'SUMS'
)
FROM TimeMatters11.lntmu11.matter
WHERE con_no in
('PR12-221' , '...', '...')
AND mat1_01_06 != ''
GROUP BY mat1_01_06
HAVING count(mat1_01_06) > 1
ORDER BY count(mat1_01_06) desc
请提供示例数据和所需输出。@RedFilter请参阅上面的编辑,对不起。我知道错误消息是什么。我想看到的是@RedFilter要求的相同的东西-示例数据和所需的输出。@很抱歉,我正在尝试格式化此表。。。你怎么把桌子放在这里,哈哈?
SELECT mat1_01_06, count(mat1_01_06), MAX(mat_no),
Mat_no = (
SELECT
MAX(document.mat_no)
FROM TimeMatters11.lntmu11.document
WHERE matter.sysid = document.mat_id and ccode = 'SUMS'
)
FROM TimeMatters11.lntmu11.matter
WHERE con_no in
('PR12-221' , '...', '...')
AND mat1_01_06 != ''
GROUP BY mat1_01_06
HAVING count(mat1_01_06) > 1
ORDER BY count(mat1_01_06) desc