[英]SQL Server: error Msg 156, Level 15, State 1, Line 12 Incorrect syntax near the keyword 'AS'
Getting an error, I'm sure it's syntax related. 遇到错误,我确定它与语法有关。
DECLARE @Rank AS INT = 0;
SELECT
asn_key,
asn_code,
asn_name,
asn_eweb_description_ext,
@Rank = CASE
WHEN asn_name = 'AONE' THEN 1
WHEN asn_name = 'ACHI' THEN 1
WHEN asn_name = 'ATLARGE' THEN 1
WHEN asn_name = 'IFD' THEN 1
ELSE 0
END AS ASN_Rank
FROM
mb_association
JOIN
mb_association_ext (nolock) ON asn_key = asn_key_ext
WHERE
asn_eweb_description_ext IS NOT NULL
ORDER BY
ASN_Rank ASC
I need to "group" my associations. 我需要对我的协会进行“分组”。 If its not one of those associations listed then it needs to be listed first in ASC order else I need it to be listed lastly in ASC order.
如果它不是列出的那些协会之一,那么它需要首先按ASC顺序列出,否则我需要将其最后按ASC顺序列出。 It looks like its telling me I have a syntax error but I'm not sure why its giving me the error.
看起来它告诉我我有语法错误,但是我不确定为什么它会给我错误。 I don't see any issues with the code and I've reviewed similar code.
我看不到代码有任何问题,并且已经查看过类似的代码。
Edit: I tried to approach it a different direction but got another error. 编辑:我试图以不同的方向来处理它,但是又遇到了另一个错误。 I'm not very good with group by clause.
我对group by子句不太满意。
New Code: 新代码:
select asn_key, asn_code, asn_name, asn_eweb_description_ext
from mb_association join
mb_association_ext (nolock)
on asn_key = asn_key_ext
where asn_eweb_description_ext is not null
--order by (CASE WHEN asn_name in ('AONE', 'ACHI', 'ATLARGE', 'IFD') then 1 else 0 end);
GROUP BY (CASE WHEN asn_name in ('AONE', 'ACHI', 'ATLARGE', 'IFD') then 1 else 0 end);
Error: Msg 8120, Level 16, State 1, Line 1 Column 'mb_association.asn_key' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. 错误:消息8120,级别16,状态1,行1在选择列表中的列mb_association.asn_key'无效,因为它既不包含在聚合函数中也不在GROUP BY子句中。
You can't assign value to variable twice with alias : 您不能使用别名两次为变量赋值:
SELECT asn_key, asn_code, asn_name, asn_eweb_description_ext,
(CASE WHEN asn_name IN ('AONE', 'ACHI', 'ATLARGE', 'IFD')
THEN 1 ELSE 0
END) AS ASN_Rank
FROM mb_association JOIN
mb_association_ext
ON asn_key = asn_key_ext
WHERE asn_eweb_description_ext IS NOT NULL
ORDER BY ASN_Rank ASC;
Also, i have just simplified the multiple WHEN
with IN
clause. 另外,我刚刚简化了带有
IN
子句的多个WHEN
。
EDIT : Your syntax error fixes with first version of query, if you want this as in particular order then i would do : 编辑:您的语法错误修复与查询的第一个版本,如果您想要按特定的顺序,那么我会做:
SELECT asn_key, asn_code, asn_name, asn_eweb_description_ext, ASN_Rank
FROM mb_association JOIN
mb_association_ext
ON asn_key = asn_key_ext CROSS APPLY
( VALUES (CASE WHEN asn_name IN ('AONE', 'ACHI', 'ATLARGE', 'IFD')
THEN 1 ELSE 0
END)
) t(ASN_Rank)
WHERE asn_eweb_description_ext IS NOT NULL
ORDER BY ASN_Rank ASC;
You seem to want something like this: 您似乎想要这样的东西:
select asn_key, asn_code, asn_name, asn_eweb_description_ext
from mb_association join
mb_association_ext (nolock)
on asn_key = asn_key_ext
where asn_eweb_description_ext is not null
order by (case when asn_name in ('AONE', 'ACHI', 'ATLARGE', 'IFD') then 1 else 0 end);
I'm not sure why you would think of using variables for this. 我不确定为什么您会考虑为此使用变量。 Variables aren't appropriate.
变量不合适。 And, SQL Server does not allow you to assign a value to a variable in a query that returns a result set -- return results or assign a variable, but not both.
而且,SQL Server不允许您在返回结果集的查询中为变量分配值-返回结果或分配变量,但不能同时分配。
Thanks to my co worker getting me on the right track I was able to resolve the issue and figure it out. 多亏了我的同事让我走上了正确的道路,我得以解决问题并解决了。
SQL: SQL:
SELECT
asn_key,
asn_code,
asn_name,
asn_eweb_description_ext,
CASE WHEN asn_code NOT IN ('AHE', 'SHSMD', 'AHRMM', 'ASHRM', 'ASHHRA', 'AHVRP') THEN asn_code END AS Other_Individual_Membership_Organizations,
CASE WHEN asn_code IN ('AHE', 'SHSMD', 'AHRMM', 'ASHRM', 'ASHHRA', 'AHVRP') THEN asn_code END AS Professional_Membership_Groups
FROM
mb_association
JOIN mb_association_ext (nolock) ON asn_key=asn_key_ext
WHERE
asn_eweb_description_ext IS NOT NULL
ORDER BY
Other_Individual_Membership_Organizations, Professional_Membership_Groups ASC
Its pretty stright forward now that I see the working code. 现在,我看到了有效的代码,这非常明智。 Using the CASE statement to filter out the first set of data and use the second CASE statement to filter the data I want for second column data.
使用CASE语句筛选出第一组数据,并使用第二条CASE语句筛选第二列数据所需的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.