multiple sub-conditions in WHERE clause SQL - sql

I have the following example of a table from which I want all rows where Year is '2016' and '2017' but want to exclude CustID 'AB17' and 'AB18' from Year '2017'. In total I should get 12 rows. See this fiddle
Example:
SQL:
SELECT * FROM testing
WHERE Year In ('2016','2017')
AND (CustID NOT In ('AB17','AB18') AND Year = '2017');
Table:
Year CustID Revenue
2016 AB12 10
2016 AB13 11
2016 AB14 12
2016 AB15 13
2016 AB16 14
2016 AB17 15
2016 AB18 16
2017 AB12 10
2017 AB13 11
2017 AB14 12
2017 AB15 13
2017 AB16 14
2017 AB17 15
2017 AB18 16
2018 AB12 17
2018 AB13 18
2018 AB14 19
2018 AB15 20
2018 AB16 21
2018 AB17 22
2018 AB18 23
Any suggestions?

This should work:
WHERE
Year = '2016'
OR (Year = '2017' AND CustID NOT In ('AB17','AB18'))

A pretty direct translation of:
Year is '2016' and '2017' but want to exclude CustID 'AB17' and 'AB18' from Year '2017'.
is:
where year in (2016, 2017) and
not (year = 2017 and custID in ('AB17', 'AB18'))

Related

Count records and group by different field while showing max value

id
year
1
2014
10
2015
10
2019
102
2015
102
2019
104
2015
104
2017
104
2019
104
2021
The output I want in postgres is below. The max year is populated based on the id and the count should also count based on id. If id = 10 then it should show the max date within id 10 and also count how many records have the id as 10.
id
year
max year
count
1
2014
2014
1
10
2015
2017
2
10
2017
2017
2
102
2015
2019
2
102
2019
2019
2
104
2015
2021
4
104
2017
2021
4
104
2019
2021
4
104
2021
2021
4
SELECT aa.id,
aa.year,
aa.max_year,
count(aa.id)
from (SELECT id,MAX(year) AS year FROM table
GROUP BY id) aa
FULL JOIN table2 b ON aa.id = b.id
You can use window functions:
select id,
year,
max(year) over (partition by id) as max_year,
count(*) over (partition by id)
from the_table

Add column value to next column in SQL

My sql table is
Week Year Applications
1 2017 0
2 2017 10
3 2017 20
4 2017 50
5 2017 0
1 2018 10
2 2018 0
3 2018 40
4 2018 50
5 2018 10
And I want SQL query which give below output
Week Year Applications
1 2017 0
2 2017 10
3 2017 30
4 2017 80
5 2017 80
1 2018 10
2 2018 10
3 2018 50
4 2018 100
5 2018 110
Can anyone help me to write below query?
You could use SUM() OVER to get cumulative sum:
SELECT *, SUM(Applications) OVER(PARTITION BY Year ORDER BY Week)
FROM tab
It looks like you want a cumulative sum:
select week, year,
sum(applications) over (partition by year order by week) as cumulative_applications
from t;

Add values in one column based on value of another SQL

I have a data set here:
YOA Count
2014 43
2014 2
2014 17
2016 47
2015 191
2017 185
2016 26
2014 119
2016 33
What I'd like to do is write a SQL query that displays:
2017 - all counts for 2017
2016 - all counts for 2017
2015 - all counts for 2017
2014 - all counts for 2017
Try this:
SELECT YOA, SUM(count) total
FROM tableName
GROUP BY YOA
Use SUM with GROUP BY:
SELECT YOA,SUM([Count]) FROM myTable GROUP BY YOA

Merging old table with new table with different structure

I am using SQL Server 2012. I have two tables which I need to 'merge'. The two tables are called tblOld and tblNew.
tblOld has data from say 2012 to 2013
tblNew has data from 2013 onwards and has a different structure
The dates do not overlap between the tables.
Simple example of the tables:
Old table
t_date region sub_region sales
------------------------------------------
1 Jan 2012 US QR 2
1 Jan 2012 US NT 3
1 Jan 2012 EU QR 5
2 Jan 2012 US QR 4
2 Jan 2012 US NT 6
2 Jan 2012 EU QR 10
...
31 Dec 2013 US QR 8
31 Dec 2013 US NT 9
31 Dec 2013 EU QR 15
New table
t_date region sales
-----------------------------
1 Jan 2014 US 20
1 Jan 2014 EU 50
2 Jan 2014 US 40
2 Jan 2014 EU 100
...
31 Dec 2014 US 80
31 Dec 2014 EU 150
Result I'm looking for:
t_date US QR US NT EU
-------------------------------------
1 Jan 2012 2 3 5
2 Jan 2012 4 6 10
...
31 Dec 2013 8 9 15
1 Jan 2014 20 50
2 Jan 2014 40 100
...
31 Dec 2014 80 150
So I'm trying to create a query which will give me the results above although I'm not sure how to do this or if it can be done?
SELECT t_date,
SUM(CASE WHEN region='US' AND (sub_region='QR' OR sub_region IS NULL) THEN sales ELSE 0 END) 'US QR',
SUM(CASE WHEN region='US' AND sub_region='NT' THEN sales ELSE 0 END) 'US NT',
SUM(CASE WHEN region='EU' THEN sales ELSE 0 END) 'EU'
FROM (
SELECT t_date
,region
,sub_region
,sales
FROM tblOLD
UNION ALL
SELECT t_date
,region
,NULL
,sales
FROM tblNEW
) t
GROUP BY t_date
You are looking for a UNION of the two tables:
SELECT t_date
,region
,sales
,sub_region
FROM tblOLD
UNION ALL
SELECT t_date
,region
,NULL
,sales
FROM tblNEW

How to get begin date if I know year and week number via Oracle SQL?

How can I get the begin date via Oracle SQL if I provide year and week number?
For example, giving year 2014 and week number 1, get date 2014-01-05.
Does not go that simply, but the next_day() function is of help here:
select
next_day(to_date(&year||'01-01','yyyy-mm-dd'), 'SUN')
+ 7 * (&week-1)
from dual;
(Thanks to Alex Poole for pointing out ...)
The second parameter to the next_day() function is NLS-specific, as is the concept of the "start of week". So you'll have to play with NLS settings for your session and think about potential portability of your solution to other countries.
You can modify the year as per your need, the query will list all the dates which are first Sunday of the week for that Year
SQL> SELECT YEAR,
2 week,
3 next_day(to_date(YEAR
4 ||'01-01','yyyy-mm-dd'), 'SUN') + (week-1)* 7
5 FROM
6 (SELECT '2014' YEAR, ROWNUM week FROM all_objects WHERE ROWNUM <= 53
7 )
8 /
YEAR WEEK NEXT_DAY(
---- ---------- ---------
2014 1 05-JAN-14
2014 2 12-JAN-14
2014 3 19-JAN-14
2014 4 26-JAN-14
2014 5 02-FEB-14
2014 6 09-FEB-14
2014 7 16-FEB-14
2014 8 23-FEB-14
2014 9 02-MAR-14
2014 10 09-MAR-14
2014 11 16-MAR-14
2014 12 23-MAR-14
2014 13 30-MAR-14
2014 14 06-APR-14
2014 15 13-APR-14
2014 16 20-APR-14
2014 17 27-APR-14
2014 18 04-MAY-14
2014 19 11-MAY-14
2014 20 18-MAY-14
2014 21 25-MAY-14
2014 22 01-JUN-14
2014 23 08-JUN-14
2014 24 15-JUN-14
2014 25 22-JUN-14
2014 26 29-JUN-14
2014 27 06-JUL-14
2014 28 13-JUL-14
2014 29 20-JUL-14
2014 30 27-JUL-14
2014 31 03-AUG-14
2014 32 10-AUG-14
2014 33 17-AUG-14
2014 34 24-AUG-14
2014 35 31-AUG-14
2014 36 07-SEP-14
2014 37 14-SEP-14
2014 38 21-SEP-14
2014 39 28-SEP-14
2014 40 05-OCT-14
2014 41 12-OCT-14
2014 42 19-OCT-14
2014 43 26-OCT-14
2014 44 02-NOV-14
2014 45 09-NOV-14
2014 46 16-NOV-14
2014 47 23-NOV-14
2014 48 30-NOV-14
2014 49 07-DEC-14
2014 50 14-DEC-14
2014 51 21-DEC-14
2014 52 28-DEC-14
2014 53 04-JAN-15
53 rows selected.
SQL>