Skip to content

Commit de352d5

Browse files
committed
solve: questions faizanxmulla#13, #14
1 parent 25205fe commit de352d5

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
### Problem Statement
2+
3+
Write a query to get `new value` from the value and the formula provided for each of the table.
4+
5+
**Explanation**: For the first row, the formula is `1+4`, which indicates that we need to sum the values from the 1st and 4th rows. This results in `10 + 20`, giving a new value of `30`.
6+
7+
### Schema Setup
8+
9+
```sql
10+
CREATE TABLE input (
11+
id INT,
12+
formula VARCHAR(10),
13+
value INT
14+
);
15+
16+
INSERT INTO input VALUES
17+
(1, '1+4', 10),
18+
(2, '2+1', 5),
19+
(3, '3-2', 40),
20+
(4, '4-1', 20);
21+
```
22+
23+
24+
### Expected Output
25+
26+
id | formula | value | new_value |
27+
--|--|--|--|
28+
1 | 1+4 | 10 | 30 |
29+
2 | 2+1 | 5 | 15 |
30+
3 | 3-2 | 40 | 35 |
31+
4 | 4-1 | 20 | 10 |
32+
33+
34+
35+
### Solution Query
36+
37+
```sql
38+
WITH CTE as (
39+
SELECT *,
40+
SUBSTRING(formula, 1, 1)::int as first_digit,
41+
SUBSTRING(formula, 3, 1)::int as second_digit,
42+
SUBSTRING(formula, 2, 1) as operator
43+
FROM input
44+
)
45+
SELECT c.id,
46+
c.formula,
47+
c.value,
48+
CASE WHEN operator='+' THEN i1.value+i2.value ELSE i1.value-i2.value END as new_value
49+
FROM CTE c JOIN input i1 ON c.first_digit=i1.id
50+
JOIN input i2 ON c.second_digit=i2.id
51+
ORDER BY 1
52+
53+
54+
-- NOTE:
55+
56+
-- syntax --> SUBSTRING(string, start_position, length)
57+
58+
-- also for the digits, instead of SUBSTRING, we could have just used LEFT and RIGHT.
59+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
### Problem Statement
2+
3+
Write a query to return either `Y` or `N`.
4+
5+
**Condition**: If both criteria 1 and criteria 2 are `Y` and at least 2 members have a value of `Y`, the output should be `Y`; otherwise, it should be `N`.
6+
7+
### Schema Setup
8+
9+
```sql
10+
CREATE TABLE data (
11+
team_id VARCHAR(2),
12+
member_id VARCHAR(10),
13+
criteria_1 VARCHAR(1),
14+
criteria_2 VARCHAR(1)
15+
);
16+
17+
INSERT INTO data VALUES
18+
('T1', 'T1_mbr1', 'Y', 'Y'),
19+
('T1', 'T1_mbr2', 'Y', 'Y'),
20+
('T1', 'T1_mbr3', 'Y', 'Y'),
21+
('T1', 'T1_mbr4', 'Y', 'Y'),
22+
('T1', 'T1_mbr5', 'Y', 'N'),
23+
('T2', 'T2_mbr1', 'Y', 'Y'),
24+
('T2', 'T2_mbr2', 'Y', 'N'),
25+
('T2', 'T2_mbr3', 'N', 'Y'),
26+
('T2', 'T2_mbr4', 'N', 'N'),
27+
('T2', 'T2_mbr5', 'N', 'N'),
28+
('T3', 'T3_mbr1', 'Y', 'Y'),
29+
('T3', 'T3_mbr2', 'Y', 'Y'),
30+
('T3', 'T3_mbr3', 'N', 'Y'),
31+
('T3', 'T3_mbr4', 'N', 'Y'),
32+
('T3', 'T3_mbr5', 'Y', 'N');
33+
```
34+
35+
36+
### Expected Output
37+
38+
| team_id | member_id | criteria_1 | criteria_2 | output |
39+
|---------|-----------|------------|------------|--------|
40+
| T1 | T1_mbr1 | Y | Y | Y |
41+
| T1 | T1_mbr2 | Y | Y | Y |
42+
| T1 | T1_mbr3 | Y | Y | Y |
43+
| T1 | T1_mbr4 | Y | Y | Y |
44+
| T1 | T1_mbr5 | Y | N | N |
45+
| T2 | T2_mbr1 | Y | Y | N |
46+
| T2 | T2_mbr2 | Y | N | N |
47+
| T2 | T2_mbr3 | N | Y | N |
48+
| T2 | T2_mbr4 | N | N | N |
49+
| T2 | T2_mbr5 | N | N | N |
50+
| T3 | T3_mbr1 | Y | Y | Y |
51+
| T3 | T3_mbr2 | Y | Y | Y |
52+
| T3 | T3_mbr3 | N | Y | N |
53+
| T3 | T3_mbr4 | N | Y | N |
54+
| T3 | T3_mbr5 | Y | N | N |
55+
56+
57+
58+
59+
### Solution Query
60+
61+
```sql
62+
WITH criteria_check as (
63+
SELECT *, CASE WHEN criteria_1='Y' and criteria_2='Y' THEN 1 ELSE 0 END as criteria_flag
64+
FROM data
65+
),
66+
criteria_summary AS (
67+
SELECT *, SUM(criteria_flag) OVER(PARTITION BY team_id) as flag_sum
68+
FROM criteria_check
69+
)
70+
SELECT team_id,
71+
member_id,
72+
criteria_1,
73+
criteria_2,
74+
CASE WHEN criteria_flag=1 and flag_sum>=2 THEN 'Y' ELSE 'N' END as output
75+
FROM criteria_summary
76+
77+
78+
-- NOTE: solve in first attempt
79+
```

0 commit comments

Comments
 (0)