Generating sequence skipping by 2 Oracle - sql

I have the below query.But I want to tweak the output to skip 2 consecutive numbers on every cycle.
For example skip 1,2 in 2nd cycle . Skip 3,4 in 3rd cycle . Skip 5,6 in 4th cycle and so on.
Also, there will be 2 input variables (i) the number to be removed/added (ii) from which date.
Once this is passed the sequence will have to be recalculate from that date on wards.
I am using Oracle version 12.1
SELECT TRUNC(sysdate, 'Y') + level - 1 AS "date", MOD(level - 1, 10) + 1 AS col1
FROM dual
CONNECT BY level <= ADD_MONTHS(TRUNC(sysdate, 'Y'), 12) - TRUNC(sysdate, 'Y')
Date Col1 Expected Output
1/1/2022 1 1
1/2/2022 2 2
1/3/2022 3 3
1/4/2022 4 4
1/5/2022 5 5
1/6/2022 6 6
1/7/2022 7 7
1/8/2022 8 8
1/9/2022 9 9
1/10/2022 10 10
1/11/2022 1 3
1/12/2022 2 4
1/13/2022 3 5
1/14/2022 4 6
1/15/2022 5 7
1/16/2022 6 8
1/17/2022 7 9
1/18/2022 8 10
1/19/2022 9 1
1/20/2022 10 2
1/21/2022 1 5
1/22/2022 2 6
1/23/2022 3 7
1/24/2022 4 8
1/25/2022 5 9
1/26/2022 6 10
1/27/2022 7 1
1/28/2022 8 2
1/29/2022 9 3
1/30/2022 10 4
1/31/2022 1 7
2/1/2022 2 8
2/2/2022 3 9
2/3/2022 4 10

You can use some more arithmetical functions such as FLOOR() and CEIL() along with precomputing unmatched values within a subquery, and then filter out by the incremented values upto the currently existing expression's value of
ADD_MONTHS(TRUNC(sysdate, 'Y'), 12) - TRUNC(sysdate, 'Y')
such as
WITH d AS
(
SELECT SUM(CASE
WHEN MOD(FLOOR((level-.1)/10),6) = CEIL((MOD(level - 1, 10) + 1)/2)
THEN
0
ELSE
1
END)
OVER (ORDER BY level) AS rn,
ADD_MONTHS(TRUNC(sysdate, 'Y'), 12) - TRUNC(sysdate, 'Y') AS day_diff,
MOD(level - 1, 10) + 1 AS col1,
MOD(FLOOR((level-.1)/10),6) AS col2,
CEIL((MOD(level - 1, 10) + 1)/2) AS col3
FROM dual
CONNECT BY level <= (ADD_MONTHS(TRUNC(sysdate, 'Y'), 12) - TRUNC(sysdate, 'Y'))*2
)
SELECT TRUNC(sysdate, 'Y') + rn - 1 AS "date", col1
FROM d
WHERE col2 != col3
AND rn <= day_diff
ORDER BY rn
Demo

Wrote below . Thanks for your answer as well. I just made some columns in excel and wrote some functions to get it
WITH Y AS
(SELECT TRUNC(SYSDATE, 'Y') + LEVEL - 1 dt,
MOD(LEVEL - 1, 10) + 1 AS grpid,
LEVEL rnum
FROM dual
CONNECT BY LEVEL <=
ADD_MONTHS(TRUNC(SYSDATE, 'Y'), 12) - TRUNC(SYSDATE, 'Y')),
Z AS
(SELECT dt,
rnum,
COUNT(CASE
WHEN grpid = 1 THEN
1
END) OVER(ORDER BY dt) - 1 as grp
FROM y)
SELECT z.*,
REGEXP_REPLACE(CASE
WHEN MOD(rnum, grp * 10) = 1 THEN
MOD(grp + grp + 1, 10)
WHEN MOD(rnum, grp * 10) = 2 THEN
MOD(grp + grp + 2, 10)
ELSE
MOD(rnum - grp * (10 - 2), 10)
END,
'^0$',
10) output
FROM Z;

Related

split from date and To date in multiple months using oracle sql

In need to split lines with From date and To date in multiple months.
I want to split like this.Target
Sample 1
10/02/2023 - 28/02/2023
Target
10/02/2023 - 28/02/2023
Sample 2
10/02/2023 - 29/08/2023
Target
10/02/2023 - 28/02/2023
01/03/2023 - 31/03/2023
01/04/2023 - 29/08/2023
Sample 3
01/04/2022 - 31/03/2023
Target
01/04/2022 - 28/02/2023
01/03/2023 - 31/03/2023
I succeed in first steps but I'm now stucked
For the moment I can only do like this[Existing]
But in yellow wrong values,
Here below my code
CASE WHEN qd.valid_from >= TRUNC(add_months(qd.valid_from,COLUMN_VALUE - 1),'MM')
THEN
TRUNC(qd.valid_from)
ELSE
TRUNC(add_months(qd.valid_from,COLUMN_VALUE - 1),'MM')
END new_start_date,
CASE WHEN last_day(TRUNC(add_months(qd.valid_from,COLUMN_VALUE - 1),'MM')) >= last_day(TRUNC(add_months(qd.valid_from,2),'MM'))
THEN
TRUNC(qd.valid_to)
ELSE
TRUNC(last_day(TRUNC(add_months(qd.valid_from,COLUMN_VALUE - 1),'MM')))
END new_end_date
FROM QUOTATIONS_UO QH
),
TABLE(
CAST(
MULTISET
(
SELECT LEVEL
FROM dual
CONNECT BY add_months(TRUNC(qd.valid_from,'MM'),LEVEL - 1) <= add_months(TRUNC(qd.valid_from,'MM'),2)
) AS sys.OdciNumberList
)
)
)
It is unclear what is it about months february and march vs all the others, but if you want to split dates by months then maybe this could help you:
Select DISTINCT
ID,
LEVEL "LVL",
CASE WHEN LEVEL = 1 THEN FROM_DATE END "FROM_DATE",
CASE WHEN LEVEL = 1 THEN UNTIL_DATE END "UNTIL_DATE",
CASE WHEN MONTH_UNTIL = MONTH_FROM THEN FROM_DATE
WHEN LEVEL = 1 THEN FROM_DATE
WHEN MONTHS > 1 And LEVEL < MONTHS THEN LAST_DAY(ADD_MONTHS(FROM_DATE, LEVEL - 2)) + 1
WHEN MONTHS > 1 And LEVEL = MONTHS THEN LAST_DAY(ADD_MONTHS(FROM_DATE, LEVEL - 2)) + 1
WHEN MONTHS > 0 And MONTHS < LEVEL THEN LAST_DAY(ADD_MONTHS(FROM_DATE, LEVEL - 2)) + 1
ELSE UNTIL_DATE
END "FROM_DATE_2",
--
CASE WHEN MONTH_UNTIL = MONTH_FROM THEN UNTIL_DATE
--WHEN LEVEL = 1 THEN LAST_DAY(ADD_MONTHS(FROM_DATE, LEVEL - 1))
WHEN MONTHS > 1 And LEVEL < MONTHS THEN LAST_DAY(ADD_MONTHS(FROM_DATE, LEVEL - 1))
WHEN MONTHS > 1 And LEVEL = MONTHS THEN LAST_DAY(ADD_MONTHS(FROM_DATE, LEVEL - 1))
ELSE UNTIL_DATE
END "UNTIL_DATE_2"
FROM
(
Select
ID, FROM_DATE, UNTIL_DATE,
To_Char(FROM_DATE, 'yyyymm') "MONTH_FROM",
To_Char(UNTIL_DATE, 'yyyymm') "MONTH_UNTIL",
FLOOR(MONTHS_BETWEEN(UNTIL_DATE, FROM_DATE)) "MONTHS"
From
tbl
)
CONNECT BY LEVEL < MONTHS + 2
ORDER BY ID, LVL
... which with sample data like here:
WITH
tbl (ID, FROM_DATE, UNTIL_DATE) AS
(
Select 1, To_Date('10.02.2023', 'dd.mm.yyyy'), To_Date('28.02.2023', 'dd.mm.yyyy') From Dual Union All
Select 2, To_Date('10.02.2023', 'dd.mm.yyyy'), To_Date('29.08.2023', 'dd.mm.yyyy') From Dual Union All
Select 3, To_Date('01.04.2022', 'dd.mm.yyyy'), To_Date('31.03.2023', 'dd.mm.yyyy') From Dual
)
... results as:
ID LVL FROM_DATE UNTIL_DATE FROM_DATE_2 UNTIL_DATE_2
---------- ---------- --------- ---------- ----------- ------------
1 1 10-FEB-23 28-FEB-23 10-FEB-23 28-FEB-23
2 1 10-FEB-23 29-AUG-23 10-FEB-23 28-FEB-23
2 2 01-MAR-23 31-MAR-23
2 3 01-APR-23 30-APR-23
2 4 01-MAY-23 31-MAY-23
2 5 01-JUN-23 30-JUN-23
2 6 01-JUL-23 31-JUL-23
2 7 01-AUG-23 29-AUG-23
3 1 01-APR-22 31-MAR-23 01-APR-22 30-APR-22
3 2 01-MAY-22 31-MAY-22
3 3 01-JUN-22 30-JUN-22
3 4 01-JUL-22 31-JUL-22
3 5 01-AUG-22 31-AUG-22
3 6 01-SEP-22 30-SEP-22
3 7 01-OCT-22 31-OCT-22
3 8 01-NOV-22 30-NOV-22
3 9 01-DEC-22 31-DEC-22
3 10 01-JAN-23 31-JAN-23
3 11 01-FEB-23 28-FEB-23
3 12 01-MAR-23 31-MAR-23

Case statement giving both values (the one in "then" and in "else" together) in Bigquery. What is wrong?

I want to create a flag in Bigquery which will return 1 when true and 0 when false. The statement works fine when it has to return the "else" value which is 0. However, when it satisfies the condition, it returns two rows with both 1 and 0 in them. Why is this happening?
Below is the code used:
table AS(
SELECT
id,
month,
ROUND((text/(month_days/7)), 2) AS value
FROM (SELECT id, extract(month FROM date) AS month,
(32 - EXTRACT(DAY FROM DATE_ADD(DATE_TRUNC(DATE(date), MONTH), INTERVAL 31 DAY))) AS month_days,
sum(text_sent) AS text
FROM table1
WHERE date BETWEEN '2020-01-01 00:00:00 UTC' AND '2020-06-30 00:00:00 UTC'
GROUP BY 1,2,3)),
table_flag AS(
SELECT
id,
CASE
WHEN month = 1 AND value > 100 THEN 1
WHEN month = 2 AND value > 150 THEN 1
WHEN month = 3 AND value > 130 THEN 1
WHEN month = 4 AND value > 200 THEN 1
WHEN month = 5 AND value > 235 THEN 1
WHEN month = 6 AND value > 125 THEN 1
WHEN month = 7 AND value > 324 THEN 1
WHEN month = 8 AND value > 160 THEN 1
WHEN month = 9 AND value > 350 THEN 1
WHEN month = 10 AND value > 80 THEN 1
WHEN month = 11 AND value > 245 THEN 1
ELSE 0
END AS value_flag
FROM
table)
SELECT
t.id,
t.value,
t.month,
tf.value_flag
FROM
table t
LEFT JOIN
table_flag tf
ON
t.id = tf.id
WHERE t.id IS NOT NULL
GROUP BY 1,2,3,4
ORDER BY 1
I have also tried nested IF, but that doesn't work either:
SELECT DISTINCT(id),
(IF((month = 1 AND value > 100), 1,
(IF((month = 2 AND value > 150), 1,
(IF((month = 3 AND value > 130), 1,
(IF((month = 4 AND value > 200), 1,
(IF((month = 5 AND value > 235), 1,
(IF((month = 6 AND value > 125), 1,
(IF((month = 7 AND value > 324), 1,
(IF((month = 8 AND value > 160), 1,
(IF((month = 9 AND value > 350), 1,
(IF((month = 10 AND value > 80), 1,
(IF((month = 11 AND value > 245), 1,0))))))))))))))))))))))
AS value_flag
FROM table)
This is how the output looks right now (This is NOT what I want):
enter image description here
The output is completely wrong. Please suggest alternate method (if any) to do it.
P.S.: This is my first question here, please let me know if any other information is needed. Thanks in advance for the help!
Both table and table_flag has several rows with identical id. BigQuery for each row in table finds several rows in table_flag. To remove duplicates we could add month to table_flag and to the ON clause. But we actually do not need the last LEFT JOIN. Try this:
WITH table AS(
SELECT
id,
month,
ROUND((text/(month_days/7)), 2) AS value
FROM (
SELECT
id,
extract(month FROM date) AS month,
(32 - EXTRACT(DAY FROM DATE_ADD(DATE_TRUNC(DATE(date), MONTH), INTERVAL 31 DAY))) AS month_days,
sum(text_sent) AS text
FROM table1
WHERE
date BETWEEN '2020-01-01 00:00:00 UTC' AND '2020-06-30 00:00:00 UTC'
AND id IS NOT NULL
GROUP BY 1,2,3
)
)
SELECT
id,
value,
month,
CASE
WHEN month = 1 AND value > 100 THEN 1
WHEN month = 2 AND value > 150 THEN 1
WHEN month = 3 AND value > 130 THEN 1
WHEN month = 4 AND value > 200 THEN 1
WHEN month = 5 AND value > 235 THEN 1
WHEN month = 6 AND value > 125 THEN 1
WHEN month = 7 AND value > 324 THEN 1
WHEN month = 8 AND value > 160 THEN 1
WHEN month = 9 AND value > 350 THEN 1
WHEN month = 10 AND value > 80 THEN 1
WHEN month = 11 AND value > 245 THEN 1
ELSE 0
END AS value_flag
FROM table
ORDER BY 1
or this:
SELECT
id,
month,
ROUND((text/(month_days/7)), 2) AS value,
CASE
WHEN month = 1 AND ROUND((text/(month_days/7)), 2) > 100 THEN 1
WHEN month = 2 AND ROUND((text/(month_days/7)), 2) > 150 THEN 1
WHEN month = 3 AND ROUND((text/(month_days/7)), 2) > 130 THEN 1
WHEN month = 4 AND ROUND((text/(month_days/7)), 2) > 200 THEN 1
WHEN month = 5 AND ROUND((text/(month_days/7)), 2) > 235 THEN 1
WHEN month = 6 AND ROUND((text/(month_days/7)), 2) > 125 THEN 1
WHEN month = 7 AND ROUND((text/(month_days/7)), 2) > 324 THEN 1
WHEN month = 8 AND ROUND((text/(month_days/7)), 2) > 160 THEN 1
WHEN month = 9 AND ROUND((text/(month_days/7)), 2) > 350 THEN 1
WHEN month = 10 AND ROUND((text/(month_days/7)), 2) > 80 THEN 1
WHEN month = 11 AND ROUND((text/(month_days/7)), 2) > 245 THEN 1
ELSE 0
END AS value_flag
FROM (
SELECT
id,
extract(month FROM date) AS month,
(32 - EXTRACT(DAY FROM DATE_ADD(DATE_TRUNC(DATE(date), MONTH), INTERVAL 31 DAY))) AS month_days,
sum(text_sent) AS text
FROM table1
WHERE
date BETWEEN '2020-01-01 00:00:00 UTC' AND '2020-06-30 00:00:00 UTC'
AND id IS NOT NULL
GROUP BY 1,2,3
)
ORDER BY 1

Calculate manual week number of year in Oracle SQL

i have a date column and along that i need to calculate another column in oracle for week number of they year, the weeks should be from sunday to saturday, starting first day of the year.
for example for current year
Week 1 : 1 Jan 2020 (Wednesday) - 4 Jan 2020(Saturday)
Week 2 : 5 Jan 2020 (Sunday) - 11 Jan 2020(Saturday)
. . . . .
Week 5 : 26 Jan 2020 (Sunday) - 1 Feb 2020 (Saturday)
and so on...
You need to write your own logic using a hierarchy query.
Something like the following:
SQL> SELECT WEEKNUMBER,
2 WEEK_START,
3 CASE WHEN WEEKNUMBER = 1 THEN FIRST_WEEKEND ELSE WEEK_START + 6 END AS WEEK_END
4 FROM
5 (SELECT
6 CASE
7 WHEN LEVEL = 1 THEN FIRST_DAY
8 ELSE FIRST_WEEKEND + ( LEVEL - 2 ) * 7 + 1
9 END AS WEEK_START,
10 FIRST_WEEKEND,
11 LEVEL AS WEEKNUMBER
12 FROM
13 ( SELECT
14 TRUNC(SYSDATE, 'YEAR') FIRST_DAY,
15 NEXT_DAY(TRUNC(SYSDATE, 'YEAR'), 'SATURDAY') FIRST_WEEKEND
16 FROM DUAL )
17 CONNECT BY
18 CASE WHEN LEVEL = 1 THEN FIRST_DAY
19 ELSE FIRST_WEEKEND + ( LEVEL - 2 ) * 7 + 1
20 END < ADD_MONTHS(TRUNC(SYSDATE, 'YEAR'), 12));
WEEKNUMBER WEEK_STAR WEEK_END
---------- --------- ---------
1 01-JAN-20 04-JAN-20
2 05-JAN-20 11-JAN-20
3 12-JAN-20 18-JAN-20
4 19-JAN-20 25-JAN-20
5 26-JAN-20 01-FEB-20
6 02-FEB-20 08-FEB-20
7 09-FEB-20 15-FEB-20
8 16-FEB-20 22-FEB-20
9 23-FEB-20 29-FEB-20
10 01-MAR-20 07-MAR-20
11 08-MAR-20 14-MAR-20
.......
.......
53 27-DEC-20 02-JAN-21
Cheers!!
One other option would be
with t as
(
select trunc(sysdate,'yyyy')+level-1 as day, to_char(trunc(sysdate,'yyyy')+level-1,'DY','NLS_DATE_LANGUAGE=AMERICAN') as weekday, level - 1 as lvl
from dual
connect by level <= 366
), t1 as
(
select case
when lvl = 0 then
day
else
case
when weekday = 'SUN' then
day
end
end as day1,
lvl
from t
), t2 as
(
select case
when weekday = 'SAT' then
day
end as day2,
lvl
from t
)
select concat( 'Week ', wk1) as week, day1, day2
from
(
select row_number() over (order by lvl) wk1, day1
from t1 where day1 is not null ) t1
left join
(
select row_number() over (order by lvl) wk2, day2
from t2 where day2 is not null ) t2
on wk2 = wk1
by using select .. from dual connect by level syntax and case..when expression to scan all the current year.
Demo

Select Where Date Between

I would like to SELECT a table calendar and combine the results with the days of the month.
I mean,
Table: Calendar
ID TEAM EMPLOYER START END
17 19 8 04/08/2014 18:01:00 11/08/2014 07:59:00
18 19 39 11/08/2014 18:01:00 18/08/2014 07:59:00
19 19 44 18/08/2014 18:01:00 25/08/2014 07:59:00
20 19 38 25/08/2014 18:01:00 01/09/2014 07:59:00
And I have a SELECT for the days of the month.
Select Days.Dt
From (Select Trunc(To_Date('2014', 'YYYY'), 'y') - 1 + Rownum Dt
From All_Objects
Where Rownum <= Add_Months(Trunc(To_Date('2014', 'YYYY'), 'y'), 12) -
Trunc(To_Date('2014', 'YYYY'), 'y')) Days
Where To_Char(Dt, 'mm/yyyy') = '08/2014'
What I want is something like this:
DAY EMPLOYER_END EMPLOYER_START
1 01/08/2014
2 02/08/2014
3 03/08/2014
4 04/08/2014 4
5 05/08/2014 4 4
6 06/08/2014 4 4
7 07/08/2014 4 4
8 08/08/2014 4 4
9 09/08/2014 4 4
10 10/08/2014 4 4
11 11/08/2014 4 39
12 12/08/2014 39 39
The employer starts at 18:01 (always) and end at 07:59 (always).
Does anyone know if it's possible?
And the way I can do that.
Thanks!
Your desired results do not match your sample data. However, I think you want something like this:
with dates as (
Select Days.Dt
From (Select Trunc(To_Date('2014', 'YYYY'), 'y') - 1 + Rownum Dt
From All_Objects
Where Rownum <= Add_Months(Trunc(To_Date('2014', 'YYYY'), 'y'), 12) -
Trunc(To_Date('2014', 'YYYY'), 'y')
) Days
Where To_Char(Dt, 'mm/yyyy') = '08/2014'
)
select d.dt,
sum(case when c.employer_start = d.ddt then 0 else 1 end) as employer_end,
sum(case when c.employer_end = d.dt then 1 else 0 end) as employer_start
from dates d left outer join
calendar c
on d.dt between c.employer_start and c.employer_end
group by d.dt
order by d.dt;
I guess this can be useful to you
WITH mindates AS
(SELECT TRUNC(MIN(startdate),'month') st_date,
TRUNC(MAX(enddate)) ed_date
FROM calendar
) ,
dates AS
(SELECT st_date+ rownum-1 AS dates_col
FROM mindates,
dual
CONNECT BY rownum <= (ed_date- st_date)+1
)
SELECT d.dates_col dates,
MIN((
CASE
WHEN d.dates_col=c.startdate
THEN NULL
ELSE c.employer
END)) AS employer_end,
MIN((
CASE
WHEN d.dates_col=c.enddate
THEN NULL
ELSE c.employer
END )) AS employer_start
FROM dates d
LEFT OUTER JOIN calendar c
ON d.dates_col BETWEEN c.startdate AND c.enddate
GROUP BY d.dates_col
ORDER BY d.dates_col;

Count parts of total value as columns per row (pivot table)

I'm stuck with a seemingly easy query, but couldn't manage to get it working the last hours.
I have a table files that holds file names and some values like records in this file, DATE of creation (create_date), DATE of processing (processing_date) and so on. There can be multiple files for a create date in different hours and it is likely that they will not get processed in the same day of creaton, in fact it can even take up to three days or longer for them to get processed.
So let's assume I have these rows, as an example:
create_date | processing_date
------------------------------
2012-09-10 11:10:55.0 | 2012-09-11 18:00:18.0
2012-09-10 15:20:18.0 | 2012-09-11 13:38:19.0
2012-09-10 19:30:48.0 | 2012-09-12 10:59:00.0
2012-09-11 08:19:11.0 | 2012-09-11 18:14:44.0
2012-09-11 22:31:42.0 | 2012-09-21 03:51:09.0
What I want in a single query is to get a grouped column truncated to the day create_date with 11 additional columns for the differences between the processing_date and the create_date, so that the result should roughly look like this:
create_date | diff0days | diff1days | diff2days | ... | diff10days
------------------------------------------------------------------------
2012-09-10 | 0 2 1 ... 0
2012-09-11 | 1 0 0 ... 1
and so on, I hope you get the point :)
I have tried this and so far it works getting a single aggregated column for a create_date with a difference of - for example - 3:
SELECT TRUNC(f.create_date, 'DD') as created, count(1) FROM files f WHERE TRUNC(f.process_date, 'DD') - trunc(f.create_date, 'DD') = 3 GROUP BY TRUNC(f.create_date, 'DD')
I tried combining the single queries and I tried sub-queries, but that didn't help or at least my knowledge about SQL is not sufficient.
What I need is a hint so that I can include the various differences as columns, like shown above. How could I possibly achieve this?
That's basically the pivoting problem:
SELECT TRUNC(f.create_date, 'DD') as created
, sum(case TRUNC(f.process_date, 'DD') - trunc(f.create_date, 'DD')
when 0 then 1 end) as diff0days
, sum(case TRUNC(f.process_date, 'DD') - trunc(f.create_date, 'DD')
when 1 then 1 end) as diff1days
, sum(case TRUNC(f.process_date, 'DD') - trunc(f.create_date, 'DD')
when 2 then 1 end) as diff2days
, ...
FROM files f
GROUP BY
TRUNC(f.create_date, 'DD')
SELECT CreateDate,
sum(CASE WHEN DateDiff(day, CreateDate, ProcessDate) = 1 THEN 1 ELSE 0 END) AS Diff1,
sum(CASE WHEN DateDiff(day, CreateDate, ProcessDate) = 2 THEN 1 ELSE 0 END) AS Diff2,
...
FROM table
GROUP BY CreateDate
ORDER BY CreateDate
As you are using Oracle 11g you can also get desired result by using pivot query.
Here is an example:
-- sample of data from your question
SQL> create table Your_table(create_date, processing_date) as
2 (
3 select '2012-09-10', '2012-09-11' from dual union all
4 select '2012-09-10', '2012-09-11' from dual union all
5 select '2012-09-10', '2012-09-12' from dual union all
6 select '2012-09-11', '2012-09-11' from dual union all
7 select '2012-09-11', '2012-09-21' from dual
8 )
9 ;
Table created
SQL> with t2 as(
2 select create_date
3 , processing_date
4 , to_date(processing_date, 'YYYY-MM-DD')
- To_Date(create_date, 'YYYY-MM-DD') dif
5 from your_table
6 )
7 select create_date
8 , max(diff0) diff0
9 , max(diff1) diff1
10 , max(diff2) diff2
11 , max(diff3) diff3
12 , max(diff4) diff4
13 , max(diff5) diff5
14 , max(diff6) diff6
15 , max(diff7) diff7
16 , max(diff8) diff8
17 , max(diff9) diff9
18 , max(diff10) diff10
19 from (select *
20 from t2
21 pivot(
22 count(dif)
23 for dif in ( 0 diff0
24 , 1 diff1
25 , 2 diff2
26 , 3 diff3
27 , 4 diff4
28 , 5 diff5
29 , 6 diff6
30 , 7 diff7
31 , 8 diff8
32 , 9 diff9
33 , 10 diff10
34 )
35 ) pd
36 ) res
37 group by create_date
38 ;
Result:
Create_Date Diff0 Diff1 Diff2 Diff3 Diff4 Diff5 Diff6 Diff7 Diff8 Diff9 Diff10
--------------------------------------------------------------------------------
2012-09-10 0 2 1 0 0 0 0 0 0 0 0
2012-09-11 1 0 0 0 0 0 0 0 0 0 1