Difference between revisions of "ByQuarter"

From SQLZOO
Jump to navigation Jump to search
(Created page with "<p>Distribute values across quarters</p> <div class='ht'> <div class=params>schema:scott</div> <source lang=sql class='tidy'>DROP TABLE Insurance;</source> <source lang=sql cl...")
 
 
(9 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
<div class='ht'>
 
<div class='ht'>
 
<div class=params>schema:scott</div>
 
<div class=params>schema:scott</div>
<source lang=sql class='tidy'>DROP TABLE Insurance;</source>
+
<source lang=sql class='tidy'>DROP TABLE Insurance;DROP TABLE quarters;</source>
 
<source lang=sql class='setup'>CREATE TABLE Insurance (
 
<source lang=sql class='setup'>CREATE TABLE Insurance (
 
   policynumber VARCHAR(10) NOT NULL PRIMARY KEY,
 
   policynumber VARCHAR(10) NOT NULL PRIMARY KEY,
 
   premium INT,
 
   premium INT,
   TransEff DATE,
+
   TransEff datetime,
   TransExp DATE
+
   TransExp datetime
 
);
 
);
INSERT INTO Insurance VALUES
+
INSERT INTO Insurance VALUES ('PACA1',490,'2012-04-27','2013-04-27');
('PACA1',490,'2012-04-27','2013-04-27');
+
INSERT INTO Insurance VALUES ('PACA2',100,'2012-04-27','2012-08-05');
 +
CREATE TABLE quarters(
 +
  lo DATETIME NOT NULL PRIMARY KEY,
 +
  hi DATETIME NOT NULL
 +
);
 +
INSERT INTO quarters VALUES ('2012-01-01','2012-04-01');
 +
INSERT INTO quarters VALUES ('2012-04-01','2012-07-01');
 +
INSERT INTO quarters VALUES ('2012-07-01','2012-10-01');
 +
INSERT INTO quarters VALUES ('2012-10-01','2013-01-01');
 +
INSERT INTO quarters VALUES ('2013-01-01','2013-04-01');
 +
INSERT INTO quarters VALUES ('2013-04-01','2013-07-01');
 +
INSERT INTO quarters VALUES ('2013-07-01','2013-10-01');
 +
INSERT INTO quarters VALUES ('2013-10-01','2014-01-01');
 
</source>
 
</source>
 
<div>
 
<div>
Line 16: Line 28:
 
</div>
 
</div>
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT policynumber,
+
SELECT policynumber,datepart(YEAR,l1) y,datepart(quarter,l1) q,l1,h1,
          CASE TransEff < '2012-04-01' AND TransEff>= '2012-01-01' THEN 1 END
+
  CASE WHEN l1>l2 THEN l1 ELSE l2 END AS maxstart,
FROM Insurance
+
  CASE WHEN h1>h2 THEN h2 ELSE h1 END AS minend
 +
FROM
 +
(SELECT policynumber,TransEff,
 +
  CAST(lo AS INT) l1,CAST(transeff AS INT)l2,CAST(hi AS INT)h1,
 +
CAST(transexp AS INT)h2
 +
FROM Insurance JOIN quarters ON(hi>transeff AND lo<transexp)
 +
) AS i;
 +
 
 +
SELECT policynumber,y,q,minend-maxstart
 +
FROM(
 +
SELECT policynumber,datepart(YEAR,l1) y,datepart(quarter,l1) q,l1,h1,
 +
  CASE WHEN l1>l2 THEN l1 ELSE l2 END AS maxstart,
 +
  CASE WHEN h1>h2 THEN h2 ELSE h1 END AS minend
 +
FROM
 +
(SELECT policynumber,TransEff,
 +
  CAST(lo AS INT) l1,CAST(transeff AS INT)l2,CAST(hi AS INT)h1,
 +
CAST(transexp AS INT)h2
 +
FROM Insurance JOIN quarters ON(hi>transeff AND lo<transexp)
 +
) AS i
 +
) as x
 
</source>
 
</source>
 
</div>
 
</div>
 
{{Hacks Ref}}
 
{{Hacks Ref}}

Latest revision as of 22:38, 17 December 2016

Distribute values across quarters

schema:scott
DROP TABLE Insurance;DROP TABLE quarters;
CREATE TABLE Insurance (
  policynumber VARCHAR(10) NOT NULL PRIMARY KEY,
  premium INT,
  TransEff datetime,
  TransExp datetime
);
INSERT INTO Insurance VALUES ('PACA1',490,'2012-04-27','2013-04-27');
INSERT INTO Insurance VALUES ('PACA2',100,'2012-04-27','2012-08-05');
CREATE TABLE quarters(
  lo DATETIME NOT NULL PRIMARY KEY,
  hi DATETIME NOT NULL
);
INSERT INTO quarters VALUES ('2012-01-01','2012-04-01');
INSERT INTO quarters VALUES ('2012-04-01','2012-07-01');
INSERT INTO quarters VALUES ('2012-07-01','2012-10-01');
INSERT INTO quarters VALUES ('2012-10-01','2013-01-01');
INSERT INTO quarters VALUES ('2013-01-01','2013-04-01');
INSERT INTO quarters VALUES ('2013-04-01','2013-07-01');
INSERT INTO quarters VALUES ('2013-07-01','2013-10-01');
INSERT INTO quarters VALUES ('2013-10-01','2014-01-01');

In this example you want to distribute the income across several quarters

SELECT policynumber,datepart(YEAR,l1) y,datepart(quarter,l1) q,l1,h1,
  CASE WHEN l1>l2 THEN l1 ELSE l2 END AS maxstart,
  CASE WHEN h1>h2 THEN h2 ELSE h1 END AS minend
FROM
(SELECT policynumber,TransEff, 
  CAST(lo AS INT) l1,CAST(transeff AS INT)l2,CAST(hi AS INT)h1,
 CAST(transexp AS INT)h2
FROM Insurance JOIN quarters ON(hi>transeff AND lo<transexp)
) AS i;

SELECT policynumber,y,q,minend-maxstart
FROM(
SELECT policynumber,datepart(YEAR,l1) y,datepart(quarter,l1) q,l1,h1,
  CASE WHEN l1>l2 THEN l1 ELSE l2 END AS maxstart,
  CASE WHEN h1>h2 THEN h2 ELSE h1 END AS minend
FROM
(SELECT policynumber,TransEff, 
  CAST(lo AS INT) l1,CAST(transeff AS INT)l2,CAST(hi AS INT)h1,
 CAST(transexp AS INT)h2
FROM Insurance JOIN quarters ON(hi>transeff AND lo<transexp)
) AS i
) as x