(MSSQL)
안녕하세요 질문이있습니다.
Row별 소수점 자리수를 다르게 가져가고싶은데 통일이 되버리네요.
방법이 있나요?
with t as (
select 'a' as item_code,
30.1356 as amount
union all
select 'b' as item_code,
50.341 as amount
)
select item_code,
amount,
-- case when rown = 1 then round(amount,1) else round(amount,3) end as result
--case when rown = 1 then cast(amount as numeric(12,1) ) else cast(amount as numeric(12,3) ) end as result
from
(select item_code,
amount,
row_number() over(order by item_code) as rown
from t) a
동일 컬럼에 대한 데이터 타입이 행별로 다를 수는 없겠죠.
자리수를 다르게 표시하려면 숫자가 아닌 문자형을 이용하셔야 할 듯.
SELECT item_code , amount , CASE item_code WHEN 'a' THEN CAST(CAST(amount AS NUMERIC(12, 1)) AS VARCHAR) WHEN 'b' THEN CAST(CAST(amount AS NUMERIC(12, 3)) AS VARCHAR) END result1 , LEFT(amount, CHARINDEX('.', amount) + CASE item_code WHEN 'a' THEN 1 WHEN 'b' THEN 3 END ) result2 FROM t ;