SQL查询以映射年份和月份数据

huangapple go评论58阅读模式
英文:

SQL query to map year and month data

问题

以下是翻译好的部分:

CREDITAMOUNT DEBITAMOUNT MONTH
0.00 0.00 一月2017
0.00 143.60 一月2018
0.00 0.00 一月2022
50.90 0.00 一月2023
0.00 0.00 二月2017
90.60 0.00 二月2018
0.00 10.20 二月2022
0.00 100.20 二月2023
98.80 0.00 三月2017
0.00 102.90 三月2018
0.00 60.80 三月2022
4.64 0.00 三月2023
10.10 0.00 四月2017
20.20 0.00 四月2018
0.00 30.30 四月2022
0.00 40.40 四月2023

英文:

Want to write SQL query on below sample data.

id company yr   Jan      Feb          Mar      Apr
1   1      17   0.00     0.00         98.80    10.10
2   1      18   -143.60  90.60        -102.90  20.20
3   1      22   0.00     -10.20	      -60.80   -30.30
4   2      23   50.90    -100.00       4.64    -40.40

If value in month column is positive then input in CREDITAMOUNT field. If the value is negative then input in DEBITAMOUNT by removing (-) from front. Add month and year in the month column as shown in the expected output.

Expected result:

CREDITAMOUNT   DEBITAMOUNT   MONTH
0.00	       0.00          Jan2017
0.00	       143.60        Jan2018
0.00           0.00          Jan2022
50.90          0.00 	     Jan2023
0.00           0.00          Feb2017
90.60          0.00 	     Feb2018
0.00           10.20         Feb2022 
0.00           100.20        Feb2023
98.80          0.00          Mar2017
0.00           102.90 	     Mar2018
0.00           60.80         Mar2022 
4.64           0.00          Mar2023
10.10          0.00          Apr2017
20.20          0.00 	     Apr2018
0.00           30.30         Apr2022 
0.00           40.40         Apr2023

答案1

得分: 1

以下是翻译好的部分:

select id ,company ,yr, cast(Jan as numeric(19,2)) as Jan, cast(Feb as numeric(19,2)) as Feb, cast(Mar as numeric(19,2)) as Mar, cast(Apr as numeric(19,2)) as Apr
into #data
from 
(
	VALUES	(1,1,17,0.00    , 0.00      ,   98.80   , 10.10)
	,	(2,1,18,-143.60  ,90.60     ,   -102.90 , 20.20)
	,	(3,1,22,0.00     ,-10.20    ,   -60.80  , -30.30)
	,	(4,2,23,50.90    ,-100.00   ,    4.64   , -40.40)
) t (id ,company ,yr, Jan, Feb, Mar, Apr)

select	case when val < 0 then 0 else val end
,	case when val > 0 then 0 else -val end
, x.yr
from #data
CROSS APPLY (
	VALUES	(Jan, concat('Jan20', yr),1)
	,	(Feb, concat('Feb20', yr),2)
	,	(Mar, concat('Mar20', yr),3)
	,	(Apr, concat('Apr20', yr),4)
	) x (val, yr,sort)
order by sort, x.yr

-- Unpivot version:
select	case when val < 0 then 0 else val end
,	case when val > 0 then 0 else -val end
,	concat(mon, yr)
from	#data
unpivot (val for mon in (Jan, Feb,Mar,Apr)) upv 

drop table #data

希望这对你有帮助。如果你需要进一步的翻译或有其他问题,请随时提问。

英文:

Something like this perhaps:

select id ,company ,yr, cast(Jan as numeric(19,2)) as Jan, cast(Feb as numeric(19,2)) as Feb, cast(Mar as numeric(19,2)) as Mar, cast(Apr as numeric(19,2)) as Apr
into #data
from 
(
	VALUES	(1,1,17,0.00    , 0.00      ,   98.80   , 10.10)
	,	(2,1,18,-143.60  ,90.60     ,   -102.90 , 20.20)
	,	(3,1,22,0.00     ,-10.20    ,   -60.80  , -30.30)
	,	(4,2,23,50.90    ,-100.00   ,    4.64   , -40.40)
) t (id ,company ,yr, Jan, Feb, Mar, Apr)

select	case when val < 0 then 0 else val end
,	case when val > 0 then 0 else -val end
, x.yr
from #data
CROSS APPLY (
	VALUES	(Jan, concat('Jan20', yr),1)
	,	(Feb, concat('Feb20', yr),2)
	,	(Mar, concat('Mar20', yr),3)
	,	(Apr, concat('Apr20', yr),4)
	) x (val, yr,sort)
order by sort, x.yr

-- Unpivot version:
select	case when val < 0 then 0 else val end
,	case when val > 0 then 0 else -val end
,	concat(mon, yr)
from	#data
unpivot (val for mon in (Jan, Feb,Mar,Apr)) upv 

drop table #data

I prefer to use the VALUES cross apply for UNPIVOTing, cause it's a bit more terse

EDIT: Added an UNPIVOT example as well

答案2

得分: 1

你可以使用 unpivot

WITH CTE AS (
  SELECT company, MONTH + CAST(yr AS VARCHAR(2)) AS [MONTH], amount
  FROM mytable  
  UNPIVOT  
   (amount FOR MONTH IN   
      (Jan, Feb, Mar, Apr)  
  ) AS unpvt
)
SELECT company, [MONTH], CASE WHEN amount >= 0 THEN amount ELSE 0 END AS CREDITAMOUNT,
                       CASE WHEN amount < 0 THEN ABS(amount) ELSE 0 END AS DEBITAMOUNT 
FROM CTE

演示在此处

英文:

You can use unpivot :

WITH CTE AS (
  SELECT company, MONTH + cast(yr as varchar(2)) AS [MONTH], amount
  FROM mytable  
  UNPIVOT  
   (amount FOR MONTH IN   
      (Jan, Feb, Mar, Apr)  
  ) AS unpvt
)
SELECT company, MONTH, CASE WHEN amount &gt;= 0 then amount else 0 end as  CREDITAMOUNT,
                       CASE WHEN amount &lt; 0 then abs(amount) else 0 end as  DEBITAMOUNT 
FROM CTE

Demo here

答案3

得分: 0

尝试这个:

SELECT
  CASE
    WHEN Jan >= 0 THEN Jan
    ELSE 0
  END AS CREDITAMOUNT,
  CASE
    WHEN Jan < 0 THEN ABS(Jan)
    ELSE 0
  END AS DEBITAMOUNT,
  CONCAT('Jan', yr) AS MONTH
FROM table_name
UNION ALL
SELECT
  CASE
    WHEN Feb >= 0 THEN Feb
    ELSE 0
  END AS CREDITAMOUNT,
  CASE
    WHEN Feb < 0 THEN ABS(Feb)
    ELSE 0
  END AS DEBITAMOUNT,
  CONCAT('Feb', yr) AS MONTH
FROM table_name
UNION ALL
SELECT
  CASE
    WHEN Mar >= 0 THEN Mar
    ELSE 0
  END AS CREDITAMOUNT,
  CASE
    WHEN Mar < 0 THEN ABS(Mar)
    ELSE 0
  END AS DEBITAMOUNT,
  CONCAT('Mar', yr) AS MONTH
FROM table_name
UNION ALL
SELECT
  CASE
    WHEN Apr >= 0 THEN Apr
    ELSE 0
  END AS CREDITAMOUNT,
  CASE
    WHEN Apr < 0 THEN ABS(Apr)
    ELSE 0
  END AS DEBITAMOUNT,
  CONCAT('Apr', yr) AS MONTH
FROM table_name;
英文:

Try this:

SELECT
  CASE
    WHEN Jan &gt;= 0 THEN Jan
    ELSE 0
  END AS CREDITAMOUNT,
  CASE
    WHEN Jan &lt; 0 THEN ABS(Jan)
    ELSE 0
  END AS DEBITAMOUNT,
  CONCAT(&#39;Jan&#39;, yr) AS MONTH
FROM table_name
UNION ALL
SELECT
  CASE
    WHEN Feb &gt;= 0 THEN Feb
    ELSE 0
  END AS CREDITAMOUNT,
  CASE
    WHEN Feb &lt; 0 THEN ABS(Feb)
    ELSE 0
  END AS DEBITAMOUNT,
  CONCAT(&#39;Feb&#39;, yr) AS MONTH
FROM table_name
UNION ALL
SELECT
  CASE
    WHEN Mar &gt;= 0 THEN Mar
    ELSE 0
  END AS CREDITAMOUNT,
  CASE
    WHEN Mar &lt; 0 THEN ABS(Mar)
    ELSE 0
  END AS DEBITAMOUNT,
  CONCAT(&#39;Mar&#39;, yr) AS MONTH
FROM table_name
UNION ALL
SELECT
  CASE
    WHEN Apr &gt;= 0 THEN Apr
    ELSE 0
  END AS CREDITAMOUNT,
  CASE
    WHEN Apr &lt; 0 THEN ABS(Apr)
    ELSE 0
  END AS DEBITAMOUNT,
  CONCAT(&#39;Apr&#39;, yr) AS MONTH
FROM table_name;

huangapple
  • 本文由 发表于 2023年7月24日 16:25:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定