Sample Table
CREATE TABLE SAMPLE_TABLE (WEEK DATE, TYPE VARCHAR(50), Movie VARCHAR(50), Question VARCHAR(50), Answer VARCHAR(50), value NUMBER(38,0));
Sample Data
INSERT INTO sample_table VALUES('10/1/2020', 'A', 'Contaco', '1', 'N/A', '1'),
('10/1/2020', 'A', 'Contaco', '1', 'Definitely not', '4'),
('10/1/2020', 'A', 'Contaco', '1', 'Definitely', '2'),
('10/1/2020', 'A', 'Contaco', '1', 'Probably', '2'),
('10/1/2020', 'A', 'Contaco', '1', 'Maybe', '1'),
('10/8/2020', 'A', 'Contaco', '1', 'N/A', '3'),
('10/8/2020', 'A', 'Contaco', '1', 'Definitely not', '1'),
('10/8/2020', 'A', 'Contaco', '1', 'Definitely', '2'),
('10/8/2020', 'A', 'Contaco', '1', 'Probably', '4'),
('10/8/2020', 'A', 'Contaco', '1', 'Maybe', '1'),
('10/15/2020', 'A', 'Contaco', '1', 'N/A', '2'),
('10/15/2020', 'A', 'Contaco', '1', 'Definitely not', '1'),
('10/15/2020', 'A', 'Contaco', '1', 'Definitely', '2'),
('10/15/2020', 'A', 'Contaco', '1', 'Probably', '3'),
('10/15/2020', 'A', 'Contaco', '1', 'Maybe', '2'),
('10/1/2020', 'B', 'Contaco', '1', 'N/A', '1'),
('10/1/2020', 'B', 'Contaco', '1', 'Definitely not', '4'),
('10/1/2020', 'B', 'Contaco', '1', 'Definitely', '2'),
('10/1/2020', 'B', 'Contaco', '1', 'Maybe', '1'),
('10/8/2020', 'B', 'Contaco', '1', 'N/A', '3'),
('10/8/2020', 'B', 'Contaco', '1', 'Definitely', '1'),
('10/8/2020', 'B', 'Contaco', '1', 'Probably', '2'),
('10/8/2020', 'B', 'Contaco', '1', 'Maybe', '1'),
('10/15/2020', 'B', 'Contaco', '1', 'N/A', '2'),
('10/15/2020', 'B', 'Contaco', '1', 'Definitely not', '1'),
('10/15/2020', 'B', 'Contaco', '1', 'Definitely', '2'),
('10/15/2020', 'B', 'Contaco', '1', 'Maybe', '2') ;
Current query
Select week, type, movie, question, answer, value,
LAG(value, 1, 0) OVER (PARTITION BY movie, question, answer, type ORDER BY movie, type, week ASC) AS one_week_prior_value,
LAG(value, 2, 0) OVER (PARTITION BY movie, question, answer, type ORDER BY movie, type, week ASC) AS two_week_prior_value
from sample_table ;
With this query - I am trying to derive the value for "One week prior value" and "Two week prior value" for the same type, movie, question and answer using the lag function. It works perfectly for when Type = A because the Answer for every Question is in the data for each week.
The issue is when Type = B when all the "Answers" options are not available for each week. The lag function finds the previous most non-null value or it doesn't give a value for the current week for the "Answer" value that doesn't exist. Examples below
Two Issues:
The row for 10/15/2020 for "Definitely not" for "One Week Prior Value" should be 0 not 4 because the previous week does not have any values for "Definitely not"
There should be a row for 10/15/2020 for "Probably" with 0 for "Value" since it doesn't have a value for that week but does have a value for the previous 2 weeks.
+--------------------------+------+---------+----------+----------------+-------+----------------------+----------------------+
| Month, Day, Year of Week | Type | Movie | Question | Answer | Value | One Week Prior Value | Two Week Prior Value |
+--------------------------+------+---------+----------+----------------+-------+----------------------+----------------------+
| 1-Oct-20 | A | Contaco | 1 | Definitely | 2 | 0 | 0 |
| 1-Oct-20 | A | Contaco | 1 | Definitely not | 4 | 0 | 0 |
| 1-Oct-20 | A | Contaco | 1 | Maybe | 1 | 0 | 0 |
| 1-Oct-20 | A | Contaco | 1 | N/A | 1 | 0 | 0 |
| 1-Oct-20 | A | Contaco | 1 | Probably | 2 | 0 | 0 |
| 8-Oct-20 | A | Contaco | 1 | Definitely | 2 | 2 | 0 |
| 8-Oct-20 | A | Contaco | 1 | Definitely not | 1 | 4 | 0 |
| 8-Oct-20 | A | Contaco | 1 | Maybe | 1 | 1 | 0 |
| 8-Oct-20 | A | Contaco | 1 | N/A | 3 | 1 | 0 |
| 8-Oct-20 | A | Contaco | 1 | Probably | 4 | 2 | 0 |
| 15-Oct-20 | A | Contaco | 1 | Definitely | 2 | 2 | 2 |
| 15-Oct-20 | A | Contaco | 1 | Definitely not | 1 | 1 | 4 |
| 15-Oct-20 | A | Contaco | 1 | Maybe | 2 | 1 | 1 |
| 15-Oct-20 | A | Contaco | 1 | N/A | 2 | 3 | 1 |
| 15-Oct-20 | A | Contaco | 1 | Probably | 3 | 4 | 2 |
| 1-Oct-20 | B | Contaco | 1 | Definitely | 2 | 0 | 0 |
| 1-Oct-20 | B | Contaco | 1 | Definitely not | 4 | 0 | 0 |
| 1-Oct-20 | B | Contaco | 1 | Maybe | 1 | 0 | 0 |
| 1-Oct-20 | B | Contaco | 1 | N/A | 1 | 0 | 0 |
| 8-Oct-20 | B | Contaco | 1 | Definitely | 1 | 2 | 0 |
| 8-Oct-20 | B | Contaco | 1 | Maybe | 1 | 1 | 0 |
| 8-Oct-20 | B | Contaco | 1 | N/A | 3 | 1 | 0 |
| 8-Oct-20 | B | Contaco | 1 | Probably | 2 | 0 | 0 |
| 15-Oct-20 | B | Contaco | 1 | Definitely | 2 | 1 | 2 |
| 15-Oct-20 | B | Contaco | 1 | Definitely not | 1 | 4 | 0 |
| 15-Oct-20 | B | Contaco | 1 | Maybe | 2 | 1 | 1 |
| 15-Oct-20 | B | Contaco | 1 | N/A | 2 | 3 | 1 |
+--------------------------+------+---------+----------+----------------+-------+----------------------+----------------------+
This is what my ideal output should look like. Any suggestions on how to make this happen with the given available data? It took me a while to put this post so hopefully it will be worth it! Thank you in advance
- You can see that for 10/15/2020 - The One Week Prior Value for "Definitely Not' is 0 not 4
- You can see that for 10/15/2020 - The Value for "Probably" is 0
+------------------+------+-----------+----------+----------------+-------+------------------+--------------------+
| Week | Type | Movie | Question | Answer | value | prior week value | 2 week prior value |
+------------------+------+-----------+----------+----------------+-------+------------------+--------------------+
| October 1, 2020 | A | Contaco| 1 | N/A | 1.000 | 0.000 | 0.000 |
| October 1, 2020 | A | Contaco| 1 | Definitely not | 4.000 | 0.000 | 0.000 |
| October 1, 2020 | A | Contaco| 1 | Definitely | 2.000 | 0.000 | 0.000 |
| October 1, 2020 | A | Contaco| 1 | Probably | 2.000 | 0.000 | 0.000 |
| October 1, 2020 | A | Contaco| 1 | Maybe | 1.000 | 0.000 | 0.000 |
| October 8, 2020 | A | Contaco| 1 | N/A | 3.000 | 1.000 | 0.000 |
| October 8, 2020 | A | Contaco| 1 | Definitely not | 1.000 | 4.000 | 0.000 |
| October 8, 2020 | A | Contaco| 1 | Definitely | 2.000 | 2.000 | 0.000 |
| October 8, 2020 | A | Contaco| 1 | Probably | 4.000 | 2.000 | 0.000 |
| October 8, 2020 | A | Contaco| 1 | Maybe | 1.000 | 1.000 | 0.000 |
| October 15, 2020 | A | Contaco| 1 | N/A | 2.000 | 3.000 | 1.000 |
| October 15, 2020 | A | Contaco| 1 | Definitely not | 1.000 | 4.000 | 4.000 |
| October 15, 2020 | A | Contaco| 1 | Definitely | 2.000 | 2.000 | 2.000 |
| October 15, 2020 | A | Contaco| 1 | Probably | 3.000 | 4.000 | 2.000 |
| October 15, 2020 | A | Contaco| 1 | Maybe | 2.000 | 1.000 | 1.000 |
| October 1, 2020 | B | Contaco| 1 | N/A | 1.000 | 0.000 | 0.000 |
| October 1, 2020 | B | Contaco| 1 | Definitely not | 4.000 | 0.000 | 0.000 |
| October 1, 2020 | B | Contaco| 1 | Definitely | 2.000 | 0.000 | 0.000 |
| October 1, 2020 | B | Contaco| 1 | Probably | 0.000 | 0.000 | 0.000 |
| October 1, 2020 | B | Contaco| 1 | Maybe | 1.000 | 0.000 | 0.000 |
| October 8, 2020 | B | Contaco| 1 | N/A | 3.000 | 1.000 | 0.000 |
| October 8, 2020 | B | Contaco| 1 | Definitely not | 0.000 | 4.000 | 0.000 |
| October 8, 2020 | B | Contaco| 1 | Definitely | 2.000 | 2.000 | 0.000 |
| October 8, 2020 | B | Contaco| 1 | Probably | 4.000 | 0.000 | 0.000 |
| October 8, 2020 | B | Contaco| 1 | Maybe | 1.000 | 1.000 | 0.000 |
| October 15, 2020 | B | Contaco| 1 | N/A | 2.000 | 3.000 | 1.000 |
| October 15, 2020 | B | Contaco| 1 | Definitely not | 1.000 | 0.000 | 4.000 |
| October 15, 2020 | B | Contaco| 1 | Definitely | 2.000 | 2.000 | 2.000 |
| October 15, 2020 | B | Contaco| 1 | Probably | 0.000 | 4.000 | 0.000 |
| October 15, 2020 | B | Contaco| 1 | Maybe | 2.000 | 1.000 | 1.000 |
+------------------+------+-----------+----------+----------------+-------+------------------+--------------------+