我正在写一个查询,其中一个字段是DIAL_NUMBER。 某些值为11位,有些值为该字段中的10位数。它是11位数我需要第2到第7个字符,而它是10位数我需要第1到第6个字符。 然后我需要计算每个系列。我尝试使用以下方法,这是错误的。 请帮我确定解决方案。
select dialled number, case
when length(Dialled_Number) = '11' then Substr(Dialled_Number, 2, 7)
else Substr(Dialled_Number, 1, 6)
end
count(*)
from Error_Event
答案 0 :(得分:0)
在不知道您的预期结果的情况下,我想您需要在查询中使用group by
。
也许是这样的:
select case
when length(Dialled_Number) = 11 then Substr(Dialled_Number, 2, 7)
else Substr(Dialled_Number, 1, 6)
end,
count(*)
from Error_Event
group by case
when length(Dialled_Number) = 11 then Substr(Dialled_Number, 2, 7)
else Substr(Dialled_Number, 1, 6)
end
答案 1 :(得分:0)
如果是MS SQL,您可以使用ROW_NUMBER()
和COUNT
函数来获得所需的输出。
DECLARE @TABLE TABLE(DIAL_NUMBER VARCHAR(20))
INSERT INTO @TABLE
SELECT '81243193812' UNION
SELECT '1829321874' UNION
SELECT '182932'
SELECT NPANXX, [Count] FROM
(
SELECT NPANXX,
COUNT(NPANXX) OVER (PARTITION BY NPANXX) AS [Count], DIAL_NUMBER,
ROW_NUMBER() OVER (PARTITION BY NPANXX ORDER BY DIAL_NUMBER) RN
FROM
(
SELECT DIAL_NUMBER,
CASE
WHEN LEN(DIAL_NUMBER) = 11 THEN SUBSTRING(DIAL_NUMBER,2, 7)
ELSE SUBSTRING(DIAL_NUMBER,1, 6)
END AS NPANXX
FROM @table
) Tmp
)FTMp
WHERE RN = 1