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
Related
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
In MS Access 365 I have 2 tables and I want to count the occurrence of a year from the first table in part of a string of the second table, purely with SQL (so far I used VBA, but I want to simplify).
The first table (tDistinctYears) contains all the years, in which one of our members paid:
ID
PaymentYear
1
2015
2
2016
3
2017
3
2018
4
2019
5
2020
6
2021
7
2022
The second table (tPayments) has all payments from members with one column containing a membership number and the other one containing payment years. Sometimes a member pays for one year, sometime for several years. The table therefore looks like that:
MembershipNr
YearPayment
11
2016
11
2017
11
2018
26
2017
26
2018;2019
26
2020;2021;2022
38
2016
38
2017
38
2018;2019;2020;2021
I want a query which tells me how many members paid in which year:
PaymentYear
Count
2015
0
2016
2
2017
3
2018
3
2019
2
2020
2
2021
2
I used the following SQL query, which I found using various answers on stackoverflow:
SELECT tDistinctYears.PaymentYear, (COUNT(tPayments.YearPayment)) AS [Count]
FROM tDistinctYears
LEFT JOIN tPayments ON tDistinctYears.PaymentYear like "*" & tPayments.YearPayment & "*"
WHERE (tDistinctYears.PaymentYear > 0 AND tDistinctYears.PaymentYear <= YEAR(NOW()))
GROUP BY tDistinctYears.PaymentYear;
But what I get is this:
PaymentYear
Count
2015
0
2016
2
2017
3
2018
1
2019
0
2020
0
2021
0
It seems as if the above query does not use the “like” expression in the JOIN ON section.
Can someone help me, please?
I think you are close just alter column in where condition tPayments.YearPayment should be first and tDistinctYears.PaymentYear should be inside like operator.
SELECT tDistinctYears.PaymentYear, (COUNT(tPayments.YearPayment)) AS [Count]
FROM tDistinctYears
LEFT JOIN tPayments ON tPayments.YearPayment like "*" &
tDistinctYears.PaymentYear
& "*" WHERE (tDistinctYears.PaymentYear > 0 AND tDistinctYears.PaymentYear <=
YEAR(NOW()))
GROUP BY tDistinctYears.PaymentYear;
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'))
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;
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>