同一列中多个值的组。

时间:2023-01-01 10:03:23

I have this SQL Query:

我有这个SQL查询:

select prefix, code, stat, complete, COUNT(*) as Count
from table
group by prefix, code, stat, complete
order by prefix, code, stat, complete

The column 'prefix' is an alphanumeric value (0-9a-zA-z). What I want is to make it so that if the value of prefix is a number, to make the number equal to 0. If it is a letter, it will keep its value. I have tried to add the following line beneath the group by clause:

列“前缀”是一个字母数字值(0-9a-zA-z)。我想要的是,如果前缀的值是一个数字,那么这个数字就等于0。如果是一封信,它就会保值。我试图在group by子句下面添加以下一行:

(case when prefix like '%[0-9]%' then 0 else prefix end)

But I get an error "Conversion failed when converting the varchar value 'J' to data type int.".

但是在将varchar值'J'转换为数据类型int时,我得到一个错误“转换失败”。

What is causing this error? How can I get the 'prefix' column to display either 0 or a letter?

是什么导致了这个错误?如何让“前缀”列显示0或字母?

4 个解决方案

#1


6  

case when prefix like '%[0-9]%' then '0' else prefix end

You obviously also need this as the expression in the GROUP BY:

显然,你也需要把它作为组中的表达式:

select 
    NewPrefix = case when prefix like '%[0-9]%' then '0' else prefix end, 
    code, 
    stat, 
    complete, 
    COUNT(*) as Count
from table
group by 
    case when prefix like '%[0-9]%' then '0' else prefix end, 
    code, stat, complete
order by 
    case when prefix like '%[0-9]%' then '0' else prefix end, 
    code, stat, complete

#2


1  

Try this :

试试这个:

select case when prefix not like '%[^0-9]%' then prefix else '0' end as prefix, code, stat, complete, COUNT(*) as Count
from table
group by case when prefix not like '%[^0-9]%' then prefix else '0' end, code, stat, complete
order by prefix, code, stat, complete

Check This. Looks similar "ISNUMERIC()"

检查这个。类似“ISNUMERIC()”

#3


0  

If prefix can have more than one character, then you might want something like:

如果前缀可以有多个字符,那么您可能需要以下内容:

(case when prefix not like '%[^0-9]%' then '0' else prefix end)

or:

或者:

(case when isnumeric(prefix) = 1 then '0' else prefix end)

#4


-1  

I just did something like this at work.

我只是在工作中做了这样的事。

select case when isnumeric(prefix) = '1' then '0' else prefix end as BEST_ANSWER
, code
, stat
, complete
, COUNT(*) as Count
from table
group by prefix, code, stat, complete
order by prefix, code, stat, complete

isnumeric is a TSQL function that tests if it is a valid number, and if so it returns 1. The case statement says, if it equals 1, make it a zero, else return the original value. BTW In general, you want to stay away from 'like' because it is bad for performance.

isnumeric是一个TSQL函数,它测试它是否是一个有效的数字,如果是,则返回1。case语句说,如果它等于1,设它为0,否则返回原始值。顺便说一句,你想要远离“喜欢”,因为它不利于性能。

#1


6  

case when prefix like '%[0-9]%' then '0' else prefix end

You obviously also need this as the expression in the GROUP BY:

显然,你也需要把它作为组中的表达式:

select 
    NewPrefix = case when prefix like '%[0-9]%' then '0' else prefix end, 
    code, 
    stat, 
    complete, 
    COUNT(*) as Count
from table
group by 
    case when prefix like '%[0-9]%' then '0' else prefix end, 
    code, stat, complete
order by 
    case when prefix like '%[0-9]%' then '0' else prefix end, 
    code, stat, complete

#2


1  

Try this :

试试这个:

select case when prefix not like '%[^0-9]%' then prefix else '0' end as prefix, code, stat, complete, COUNT(*) as Count
from table
group by case when prefix not like '%[^0-9]%' then prefix else '0' end, code, stat, complete
order by prefix, code, stat, complete

Check This. Looks similar "ISNUMERIC()"

检查这个。类似“ISNUMERIC()”

#3


0  

If prefix can have more than one character, then you might want something like:

如果前缀可以有多个字符,那么您可能需要以下内容:

(case when prefix not like '%[^0-9]%' then '0' else prefix end)

or:

或者:

(case when isnumeric(prefix) = 1 then '0' else prefix end)

#4


-1  

I just did something like this at work.

我只是在工作中做了这样的事。

select case when isnumeric(prefix) = '1' then '0' else prefix end as BEST_ANSWER
, code
, stat
, complete
, COUNT(*) as Count
from table
group by prefix, code, stat, complete
order by prefix, code, stat, complete

isnumeric is a TSQL function that tests if it is a valid number, and if so it returns 1. The case statement says, if it equals 1, make it a zero, else return the original value. BTW In general, you want to stay away from 'like' because it is bad for performance.

isnumeric是一个TSQL函数,它测试它是否是一个有效的数字,如果是,则返回1。case语句说,如果它等于1,设它为0,否则返回原始值。顺便说一句,你想要远离“喜欢”,因为它不利于性能。