SQL Server 2008 查询中按错误分组

发布于 2024-12-12 09:03:23 字数 549 浏览 0 评论 0原文

我有这个查询:

select row_number() over(order by Name) as Number,Name
,COUNT(Code) as Tqty from WorkTbl group by Name order by Tqty
,COUNT(Code) as Tqty2 from WorkTbl Where Status <> 'AA' and Status <> 'BB' group by Name order by Tqty2

并且收到错误:

关键字“as”附近的语法不正确。

但如果我运行此查询,它工作正常:

select row_number() over(order by Name) as Number,Name
,COUNT(Code) as Tqty from WorkTbl group by Name order by Tqty

我必须拥有查询中的所有列

如何做到这一点?

I have this query:

select row_number() over(order by Name) as Number,Name
,COUNT(Code) as Tqty from WorkTbl group by Name order by Tqty
,COUNT(Code) as Tqty2 from WorkTbl Where Status <> 'AA' and Status <> 'BB' group by Name order by Tqty2

and I get an error:

Incorrect syntax near the keyword 'as'.

but if I run this query it works fine:

select row_number() over(order by Name) as Number,Name
,COUNT(Code) as Tqty from WorkTbl group by Name order by Tqty

I must have all columns in the query

How to do it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光是把杀猪刀 2024-12-19 09:03:23

你可能需要类似的东西

select row_number() over(order by Name) as Number,Name
,COUNT(Code) as Tqty,
COUNT(CASE WHEN Status<>'AA' AND Status <>'BB' THEN Code END) AS Tqty2
from WorkTbl group by Name order by Tqty

You probably need something like

select row_number() over(order by Name) as Number,Name
,COUNT(Code) as Tqty,
COUNT(CASE WHEN Status<>'AA' AND Status <>'BB' THEN Code END) AS Tqty2
from WorkTbl group by Name order by Tqty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文