There is a table on the payment schedule:
| PaySum | PlanDate |
+----------+------------+
| 23928.38 | 14.10.2019 |
| 24347.13 | 12.11.2019 |
| 24773.20 | 12.12.2020 |
| 25206.73 | 13.01.2020 |
Need to pull forthcoming amount for 3 months
My request for example:
select sum(s.PaySum)
from L_DEA s
where s.PlanDate between trunc(sysdate + 1) and
ADD_months(trunc(sysdate + 1), 3)
and ID = :iId;
This query returns 4 months if run sysdate = 12.10.19 or 13.10.19
in other cases shows correctly for 3 months
How to form select correctly
Perhaps try changing the inequality:
SELECT SUM(s.PaySum)
FROM L_DEA s
WHERE
s.PlanDate >= TRUNC(SYSDATE) AND
s.PlanDate < ADD_MONTHS(TRUNC(SYSDATE + 1), 3) AND
ID = :iId;
This would include all plan dates on or after midnight of today, but before midnight of three months from now.
I don't agree with you. At least in the given sample data, It is working correctly.
For 12-Oct-2019
SQL> with L_DEA (PaySum, PlanDate) as
2 (select 23928.38 , to_date('14.10.2019','dd.mm.yyyy') from dual union all
3 select 24347.13 , to_date('12.11.2019','dd.mm.yyyy') from dual union all
4 select 24773.20 , to_date('12.12.2020','dd.mm.yyyy') from dual union all
5 select 24773.20 , to_date('10.02.2020','dd.mm.yyyy') from dual union all -- added this
6 select 25206.73 , to_date('13.01.2020','dd.mm.yyyy') from dual )
7 select * --sum(s.PaySum)
8 from L_DEA s
9 where s.PlanDate between trunc(date '2019-10-12' + 1) and
10 ADD_months(trunc(date '2019-10-12' + 1), 3)
11 --and ID = :iId;
PAYSUM PLANDATE
---------- ---------
23928.38 14-OCT-19
24347.13 12-NOV-19
25206.73 13-JAN-20
SQL>
for another date i.e. 13-Oct-2019
SQL> with L_DEA (PaySum, PlanDate) as
2 (select 23928.38 , to_date('14.10.2019','dd.mm.yyyy') from dual union all
3 select 24347.13 , to_date('12.11.2019','dd.mm.yyyy') from dual union all
4 select 24773.20 , to_date('12.12.2020','dd.mm.yyyy') from dual union all
5 select 24773.20 , to_date('10.02.2020','dd.mm.yyyy') from dual union all -- added this
6 select 25206.73 , to_date('13.01.2020','dd.mm.yyyy') from dual )
7 select * --sum(s.PaySum)
8 from L_DEA s
9 where s.PlanDate between trunc(date '2019-10-13' + 1) and
10 ADD_months(trunc(date '2019-10-13' + 1), 3)
11 --and ID = :iId;
PAYSUM PLANDATE
---------- ---------
23928.38 14-OCT-19
24347.13 12-NOV-19
25206.73 13-JAN-20
SQL>
Cheers!!
One abbreviate option to cover the period starting from the present day to three months later would be :
select sum(s.PaySum)
from l_dea s
where s.PlanDate between trunc(systimestamp) and trunc(systimestamp) + interval '3' month
and ID = :iId
select s.PlanDate, s.PaySum
from L_DEA s
inner join (select PlanDate
from (select distinct s.PlanDate
from L_DEA s
where ID = :iId
and s.PlanDate >= (to_date('10.10.2019'))
order by s.PlanDate) tt
where rownum <= 3) A2
on s.PlanDate = A2.PlanDate
where ID = :iId);
Related
I receive a string from decoding codebar-128, once I parse all data in the code readed I get a date in a strange 4 digits format: 'YDDD'
The 'Y' digit represents the last digit of the Year (0-9). The 'DDD' digits represent the Day of year (1-366).
The issue is the ambiguous value of the Year. The rule to solve that issue must be the follow:
The Year computed for 'Y' digit must be the nearest year to Sysdate year.
Never the difference from Sysdate year and computed year for the 'Y' digit will be greater than 4.
My code:
SELECT SYSDATE, TO_DATE('0213', 'YDDD'), TO_DATE('1212', 'YDDD'),
TO_DATE('2212', 'YDDD'), TO_DATE('3212', 'YDDD'), TO_DATE('4213', 'YDDD'),
TO_DATE('6212', 'YDDD'), TO_DATE('7212', 'YDDD'), TO_DATE('8213', 'YDDD'),
TO_DATE('9212', 'YDDD')
FROM dual;
This is that I need to get:
+-----------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+
| SYSDATE | TO_DATE('20213','YYDDD') | TO_DATE('21212','YYDDD') | TO_DATE('22212','YYDDD') | TO_DATE('23212','YYDDD') | TO_DATE('24213','YYDDD') | TO_DATE('16213','YYDDD') | TO_DATE('17212','YYDDD') | TO_DATE('18212','YYDDD') | TO_DATE('19212','YYDDD') |
+-----------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+
| 26-JUN-20 | 31-JUL-20 | 31-JUL-21 | 31-JUL-22 | 31-JUL-23 | 31-JUL-24 | 31-JUL-16 | 31-JUL-17 | 31-JUL-18 | 31-JUL-19 |
+-----------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+
As you can see, if I had the penultimate digit of the year there would be no issue.
This is that I'm really getting:
+-----------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+
| SYSDATE | TO_DATE('0213','YDDD') | TO_DATE('1212','YDDD') | TO_DATE('2212','YDDD') | TO_DATE('3212','YDDD') | TO_DATE('4213','YDDD') | TO_DATE('6212','YDDD') | TO_DATE('7212','YDDD') | TO_DATE('8213','YDDD') | TO_DATE('9212','YDDD') |
+-----------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+
| 26-JUN-20 | 31-JUL-20 | 31-JUL-21 | 31-JUL-22 | 31-JUL-23 | 31-JUL-24 | 31-JUL-26 | 31-JUL-27 | 31-JUL-28 | 31-JUL-29 |
+-----------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+
You can compare the single-digit value with the last digit of the current year, and if the difference is more than 4, adjust but 10 years. But it needs to go both ways; once 'today' is in 2026, you'll be adding 10 years instead.
select column_value as val,
to_date(column_value, 'YDDD') as dt1,
to_number(substr(column_value, 1, 1)) as y,
mod(extract(year from sysdate), 10) as yy,
case
when to_number(substr(column_value, 1, 1)) - mod(extract(year from sysdate), 10) > 4 then -10
when mod(extract(year from sysdate), 10) - to_number(substr(column_value, 1, 1)) > 4 then 10
else 0
end as adj,
to_date(column_value, 'YDDD')
+ case
when to_number(substr(column_value, 1, 1)) - mod(extract(year from sysdate), 10) > 4 then -10
when mod(extract(year from sysdate), 10) - to_number(substr(column_value, 1, 1)) > 4 then 10
else 0
end * interval '1' year as dt2,
add_months(to_date(column_value, 'YDDD'),
12 * case
when to_number(substr(column_value, 1, 1)) - mod(extract(year from sysdate), 10) > 4 then -10
when mod(extract(year from sysdate), 10) - to_number(substr(column_value, 1, 1)) > 4 then 10
else 0
end) as dt2
from table(sys.odcivarchar2list('0213', '1212', '2212', '3212', '4213',
'5212', '6212', '7212', '8213', '9212'));
which gets
VAL DT1 Y YY ADJ DT2 DT2
---- ---------- ---------- ---------- ---------- ---------- ----------
0213 2020-07-31 0 0 0 2020-07-31 2020-07-31
1212 2021-07-31 1 0 0 2021-07-31 2021-07-31
2212 2022-07-31 2 0 0 2022-07-31 2022-07-31
3212 2023-07-31 3 0 0 2023-07-31 2023-07-31
4213 2024-07-31 4 0 0 2024-07-31 2024-07-31
5212 2025-07-31 5 0 -10 2015-07-31 2015-07-31
6212 2026-07-31 6 0 -10 2016-07-31 2016-07-31
7212 2027-07-31 7 0 -10 2017-07-31 2017-07-31
8213 2028-07-31 8 0 -10 2018-07-31 2018-07-31
9212 2029-07-31 9 0 -10 2019-07-31 2019-07-31
I haven't verified the future-year behaviour so you probably need to test and adjust that as necessary.
Split it up in multiple with clauses so it is easier to understand, you can join it into a single query if you want.
WITH sampledata (dt) AS
(
SELECT '0213' FROM DUAL UNION
SELECT '1212' FROM DUAL UNION
SELECT '2212' FROM DUAL UNION
SELECT '3212' FROM DUAL UNION
SELECT '4213' FROM DUAL UNION
SELECT '5213' FROM DUAL UNION
SELECT '6212' FROM DUAL UNION
SELECT '7212' FROM DUAL UNION
SELECT '8213' FROM DUAL UNION
SELECT '9212' FROM DUAL
), parsed_sampledata (yr, ddd) AS
(
SELECT substr(d.dt,1, 1) + TO_CHAR(SYSDATE,'YY') as yr, substr(d.dt,2,3) as ddd
FROM sampledata d
)
SELECT TO_DATE(ddd||yr - (CASE WHEN yr - TO_CHAR(SYSDATE,'YY') < 5 THEN 0 ELSE 10 END),'DDDYY')
FROM parsed_sampledata d;
31-JUL-2020
31-JUL-2021
31-JUL-2022
31-JUL-2023
31-JUL-2024
01-AUG-2015
30-JUL-2016
31-JUL-2017
01-AUG-2018
31-JUL-2019
This should give you some ideas:
WITH DATES_LIST AS
(
SELECT '0213' AS D FROM DUAL UNION
SELECT '1212' AS D FROM DUAL UNION
SELECT '2212' AS D FROM DUAL UNION
SELECT '3212' AS D FROM DUAL UNION
SELECT '4213' AS D FROM DUAL UNION
SELECT '5213' AS D FROM DUAL UNION
SELECT '6213' AS D FROM DUAL UNION
SELECT '7212' AS D FROM DUAL UNION
SELECT '8212' AS D FROM DUAL UNION
SELECT '9212' AS D FROM DUAL
)
SELECT TO_DATE(REGEXP_REPLACE(D,'^\d{1}',
CASE WHEN BOTT_R <= UPP_R THEN BOT ELSE UPP END),'YYDDD') AS YEAR
FROM (
select D,(TO_CHAR(SYSDATE,'RR') - 10) + regexp_substr(D, '^\d{1}') BOT,
ABS((TO_CHAR(SYSDATE,'RR') - 10) + regexp_substr(D, '^\d{1}')-TO_CHAR(SYSDATE,'RR')) BOTT_R,
TO_CHAR(SYSDATE,'RR') + regexp_substr(D, '^\d{1}') UPP,
(TO_CHAR(SYSDATE,'RR') + regexp_substr(D, '^\d{1}')) - TO_CHAR(SYSDATE,'RR') UPP_R
from DATES_LIST);
If you need to convert to many variables(many) my advise is to create a DETERMINISTIC function.
Regards.
I have a table with some data and a time period i.e. start date and end date
------------------------------
| id | start_date | end_date |
|------------------------------|
| 0 | 1-1-2019 | 3-1-2019 |
|------------------------------|
| 1 | 6-1-2019 | 8-1-2019 |
|------------------------------|
I want to run a query that will return the id and all the dates that are within those time periods. for instance, the result of the query for the above table will be:
------------------
| id | date |
|------------------|
| 0 | 1-1-2019 |
|------------------|
| 0 | 2-1-2019 |
|------------------|
| 0 | 3-1-2019 |
|------------------|
| 1 | 6-1-2019 |
|------------------|
| 1 | 7-1-2019 |
|------------------|
| 1 | 8-1-2019 |
------------------
I am using Redshift therefor I need it supported in Postgres and take this into consideration
Your help will be greatly appriciated
The common way this is done is to create a calendar table with a list of dates. In fact, a calendar table can be extended to include columns like:
Day number (in year)
Week number
First day of month
Last day of month
Weekday / Weekend
Public holiday
Simply create the table in Excel, save as CSV and then COPY it into Redshift.
You could then just JOIN to the table, like:
SELECT
table.id,
calendar.date
FROM table
JOIN calendar
WHERE
calendar.date BETWEEN table.start_date AND table.end_date
This question was originally tagged Postgres.
Use generate_series():
select t.id, gs.dte
from t cross join lateral
generate_series(t.start_date, t.end_date, interval '1 day') as gs(dte);
ok, It took me a while to get there but this is what I did (though not really proud of it):
I created a query that generates a calendar for the last 6 years, cross joined it with my table and then selected the relevant dates from my calendar table.
WITH
days AS (select 0 as num UNION select 1 as num UNION select 2 UNION select 3 UNION select 4 UNION select 5 UNION select 6 UNION select 7 UNION select 8 UNION select 9 UNION select 10 UNION select 11 UNION select 12 UNION select 13 UNION select 14 UNION select 15 UNION select 16 UNION select 17 UNION select 18 UNION select 19 UNION select 20 UNION select 21 UNION select 22 UNION select 23 UNION select 24 UNION select 25 UNION select 26 UNION select 27 UNION select 28 UNION select 29 UNION select 30 UNION select 31),
month AS (select num from days where num <= 12),
years AS (select num from days where num <= 6),
rightnow AS (select CAST( TO_CHAR(GETDATE(), 'yyyy-mm-dd hh24') || ':' || trim(TO_CHAR((ROUND((DATEPART (MINUTE, GETDATE()) / 5), 1) * 5 ),'09')) AS TIMESTAMP) as start),
calendar as
(
select
DATEADD(years, -y.num, DATEADD( month, -m.num, DATEADD( days, -d.num, n.start ) ) ) AS period_date
from days d, month m, years y, rightnow n
)
select u.id, calendar.period_date
from periods u
cross join calendar
where date_part(DAY, u.finishedat) >= date_part(DAY, u.startedat) + 1 and date_part(DAY, calendar.period_date) < date_part(DAY, u.finishedat) and date_part(DAY, calendar.period_date) > date_part(DAY, u.startedat) and calendar.period_date < u.finishedat and calendar.period_date > u.startedat
This was based on the answer here: Using sql function generate_series() in redshift
I want to make a query, which shows the progress of the number of users on my webpage by week.
I use following query to run the users database and get the number, grouped by a week:
SELECT TRUNC(FAB.LICENSE_DATE, 'IW'),
COUNT(DISTINCT FAB.STATEMENT_NUMBER) AS "Number of account statements"
FROM USERS FAB
GROUP BY TRUNC(FAB.LAST_UPDATED_TIME, 'IW');
This gives following output:
Date | Users
----------------------
2015/09/07 | 5
2015/09/14 | 4
2015/09/21 | 6
But this is actually not what I want to achieve. I want to have the following output:
Date | Users
----------------------
2015/09/07 | 5
2015/09/14 | 9 (5 + 4)
2015/09/21 | 15 (5 + 4 + 6)
How to modify the query so I get all the results?
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE USERS (
LICENSE_DATE,
LAST_UPDATED_TIME,
STATEMENT_NUMBER
) AS
SELECT DATE '2015-09-07', DATE '2015-09-07', 1 FROM DUAL
UNION ALL SELECT DATE '2015-09-08', DATE '2015-09-08', 2 FROM DUAL
UNION ALL SELECT DATE '2015-09-08', DATE '2015-09-08', 3 FROM DUAL
UNION ALL SELECT DATE '2015-09-09', DATE '2015-09-09', 4 FROM DUAL
UNION ALL SELECT DATE '2015-09-12', DATE '2015-09-12', 5 FROM DUAL
UNION ALL SELECT DATE '2015-09-14', DATE '2015-09-15', 6 FROM DUAL
UNION ALL SELECT DATE '2015-09-15', DATE '2015-09-16', 7 FROM DUAL
UNION ALL SELECT DATE '2015-09-16', DATE '2015-09-16', 8 FROM DUAL
UNION ALL SELECT DATE '2015-09-17', DATE '2015-09-18', 9 FROM DUAL
UNION ALL SELECT DATE '2015-09-21', DATE '2015-09-21', 10 FROM DUAL
UNION ALL SELECT DATE '2015-09-21', DATE '2015-09-26', 11 FROM DUAL
UNION ALL SELECT DATE '2015-09-22', DATE '2015-09-22', 12 FROM DUAL
UNION ALL SELECT DATE '2015-09-23', DATE '2015-09-25', 13 FROM DUAL
UNION ALL SELECT DATE '2015-09-24', DATE '2015-09-24', 14 FROM DUAL
UNION ALL SELECT DATE '2015-09-27', DATE '2015-09-27', 15 FROM DUAL;
Query 1:
SELECT LAST_UPDATED_WEEK,
SUM( NUM_STATEMENTS ) OVER ( ORDER BY LAST_UPDATED_WEEK ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS "Number of account statements"
FROM (
SELECT TRUNC(LAST_UPDATED_TIME, 'IW') AS LAST_UPDATED_WEEK,
COUNT(DISTINCT STATEMENT_NUMBER) AS NUM_STATEMENTS
FROM USERS
GROUP BY
TRUNC( LAST_UPDATED_TIME, 'IW')
)
Results:
| LAST_UPDATED_WEEK | Number of account statements |
|-----------------------------|------------------------------|
| September, 07 2015 00:00:00 | 5 |
| September, 14 2015 00:00:00 | 9 |
| September, 21 2015 00:00:00 | 15 |
SELECT TRUNC(FAB.LICENSE_DATE, 'IW'),
SUM(COUNT(DISTINCT FAB.STATEMENT_NUMBER)) OVER (ORDER BY TRUNC(FAB.LAST_UPDATED_TIME, 'IW')) as "Number of account statements"
FROM USERS FAB
GROUP BY TRUNC(FAB.LAST_UPDATED_TIME, 'IW');
You can use this code block for your problem :
select u.date
,(select sum(u1.users)
from users u1
where u1.ddate <= u.date) as users
from users u;
It gives this output :
07.09.2015 5
14.09.2015 9
21.09.2015 15
Good luck
Hello you can try this code too.
WITH t1 AS
( SELECT to_date('01/01/2015','mm/dd/yyyy') rn, 5 usrs FROM dual
UNION ALL
SELECT to_date('02/01/2015','mm/dd/yyyy') rn, 4 usrs FROM dual
UNION ALL
SELECT to_date('03/01/2015','mm/dd/yyyy') rn, 8 usrs FROM dual
UNION ALL
SELECT to_date('04/01/2015','mm/dd/yyyy') rn, 2 usrs FROM dual
)
SELECT rn,
usrs,
sum(usrs) over (order by rn ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) cumm_usrs
FROM t1
GROUP BY rn,
usrs;
SELECT
tsn_ref,
current_node
FROM rtdev.trip_bodies cnode
WHERE thr_id = 1
ORDER BY arrival_time
The above query gives me 9 records each with 2 columns, quite straigh forward
I want to add a 3rd column, which will contain the 'TSN_REF' field from the subsequent (time sorted) recordset.
So output would be along the lines of:
+---------+--------------+------+
| TSN_REF | CURRENT_NODE | TSN2 |
+---------+--------------+------+
| 1 | 1 | 2 |
| 2 | 2 | 4 |
| 4 | 10 | 8 |
| 8 | 13 | 13 |
| 13 | 16 | NULL | (no subsequent entry)
+---------+--------------+------+
Any suggestions on how I could modify the query to achieve this?
You can do this using the LEAD function: http://www.oracle-base.com/articles/misc/lag-lead-analytic-functions.php
it would go something like this (this is untested....)
SELECT TSN_REF,
CURRENT_NODE,
LEAD(TSN_REF, 1, '') OVER (ORDER BY ARRIVAL_TIME) TSN2
FROM RTDEV.TRIP_BODIES CNODE
WHERE (THR_ID = 1)
ORDER BY ARRIVAL_TIME
edited: fixed order by in lead
Update
See if this works....
select ts_id,
tsn_ref,
current_node,
case when ts_id = ts_id_next then tsn_ref_next else null end tsn_ref_next
from (
with q as (
select 1 ts_id, 1 tsn_ref, 1 current_node, sysdate - 20 arrival_time from dual
union
select 1 ts_id, 2 tsn_ref, 2 current_node, sysdate - 19 arrival_time from dual
union
select 1 ts_id, 4 tsn_ref, 10 current_node, sysdate - 18 arrival_time from dual
union
select 1 ts_id, 8 tsn_ref, 13 current_node, sysdate - 17 arrival_time from dual
union
select 1 ts_id, 13 tsn_ref, 16 current_node, sysdate - 16 arrival_time from dual
union
select 2 ts_id, 1 tsn_ref, 1 current_node, sysdate - 20 arrival_time from dual
union
select 2 ts_id, 2 tsn_ref, 2 current_node, sysdate - 19 arrival_time from dual
union
select 2 ts_id, 4 tsn_ref, 10 current_node, sysdate - 18 arrival_time from dual
union
select 2 ts_id, 8 tsn_ref, 13 current_node, sysdate - 17 arrival_time from dual
union
select 2 ts_id, 13 tsn_ref, 16 current_node, sysdate - 16 arrival_time from dual
)
select ts_id,
tsn_ref,
current_node,
lead(tsn_ref, 1, '') over (order by ts_id, arrival_time) tsn_ref_next,
lag(ts_id, 1, ts_id) over (order by ts_id, arrival_time) ts_id_next
from q
)
You will want to use your table, so something like this:
select ts_id,
tsn_ref,
current_node,
case when ts_id = ts_id_next then tsn_ref_next else null end tsn_ref_next
from (
select ts_id,
tsn_ref,
current_node,
arrival_time,
lead(tsn_ref, 1, '') over (order by ts_id, arrival_time) tsn_ref_next,
lag(ts_id, 1, ts_id) over (order by ts_id, arrival_time) ts_id_next
FROM RTDEV.TRIP_BODIES CNODE
ORDER BY ARRIVAL_TIME -- this order by may not be needed
)
ORDER BY ARRIVAL_TIME
I am attempting to write Oracle SQL.
I am looking for solution something similar. Please find below data I have
start_date end_date customer
01-01-2012 31-06-2012 a
01-01-2012 31-01-2012 b
01-02-2012 31-03-2012 c
I want the count of customer in that date period. My result should look like below
Month : Customer Count
JAN-12 : 2
FEB-12 : 2
MAR-12 : 2
APR-12 : 1
MAY-12 : 1
JUN-12 : 1
One option would be to generate the months separately in another query and join that to your data table (note that I'm assuming that you intended customer A to have an end-date of June 30, 2012 since there is no June 31).
SQL> ed
Wrote file afiedt.buf
1 with mnths as(
2 select add_months( date '2012-01-01', level - 1 ) mnth
3 from dual
4 connect by level <= 6 ),
5 data as (
6 select date '2012-01-01' start_date, date '2012-06-30' end_date, 'a' customer from dual union all
7 select date '2012-01-01', date '2012-01-31', 'b' from dual union all
8 select date '2012-02-01', date '2012-03-31', 'c' from dual
9 )
10 select mnths.mnth, count(*)
11 from data,
12 mnths
13 where mnths.mnth between data.start_date and data.end_date
14 group by mnths.mnth
15* order by mnths.mnth
SQL> /
MNTH COUNT(*)
--------- ----------
01-JAN-12 2
01-FEB-12 2
01-MAR-12 2
01-APR-12 1
01-MAY-12 1
01-JUN-12 1
6 rows selected.
WITH TMP(monthyear,start_date,end_date,customer) AS (
select LAST_DAY(start_date),
CAST(ADD_MONTHS(start_date, 1) AS DATE),
end_date,
customer
from data
union all
select LAST_DAY(start_date),
CAST(ADD_MONTHS(start_date, 1) AS DATE),
end_date,
customer
from TMP
where LAST_DAY(end_date) >= LAST_DAY(start_date)
)
SELECT TO_CHAR(MonthYear, 'MON-YY') TheMonth,
Count(Customer) Customers
FROM TMP
GROUP BY MonthYear
ORDER BY MonthYear;
SQLFiddle