This is what i have in table xyz
NAME AMOUNT BEGIN_DATE END_DATE
ABC 5.0 2013-05-11 2014-06-20
following is what i want using IBM DB2 database
NAME AMOUNT BEGIN_DATE END_DATE
ABC 5.0 2013-05-11 2013-12-31
ABC 5.0 2014-01-01 2014-06-30
instead of just one row from xyz table, i need to fetch 2 rows as above output. How do I split one row into two ?
The following will only list rows where the begin and end dates span exactly two years or within the same year.
SELECT
NAME,
AMOUNT,
BEGIN_DATE,
DATE(YEAR(BEGIN_DATE)||'-12-31') AS END_DATE
FROM xyz
WHERE YEAR(END_DATE)-YEAR(BEGIN_DATE)=1
UNION
SELECT
NAME,
AMOUNT,
DATE(YEAR(END_DATE)||'-01-01') AS BEGIN_DATE,
END_DATE
FROM xyz
WHERE YEAR(END_DATE)-YEAR(BEGIN_DATE)=1
UNION
SELECT
NAME,
AMOUNT,
BEGIN_DATE,
END_DATE
FROM xyz
WHERE YEAR(END_DATE)-YEAR(BEGIN_DATE)=0
ORDER BY BEGIN_DATE
You can make two SQL statements, to select the first, using '2013-12-31' as a constant for the end-date, then to select a second time, using '2014-01-01' as a constant start date. Then use UNION ALL to put them together.
If you also have some records that start and end within 2013, and therefore do not need to be split, you can get those separately, and exclude them from the other two queries. Other variations in your data might require some extra conditions, but this example should get you going:
select NAME, AMOUNT, BEGIN_DATE, END_DATE
from xyz
where END_DATE <= '2013-12-31'
UNION ALL
select NAME, AMOUNT, BEGIN_DATE, '2013-12-31'
from xyz
where END_DATE >= '2014-01-01'
UNION ALL
select NAME, AMOUNT, '2014-01-01', END_DATE
from xyz
where END_DATE >= '2014-01-01'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.