Access query count by months - sql

I have 2 tables -
"club members" table with 3 fields For example:
"Member_ID", "Date_Of_Birth"
1 13/4/1980
2 20/4/1990
3 30/12/1970
4 20/11/1960
"months list" table with the 1 field for example:
"Month"
4-2017
5-2017
...
11-2017
12-2017
...
4-2018
...
11-2018
12-2018
I wish to generate a query that displays
Month , Number_Of_Birthdays
for example:
4-2017 2
5-2017 0
...
11-2017 1
12-2017 1
...
4-2018 2
...
11-2018 1
12-2018 1
How can I do it in access?
Thank you

use inner join and aggregate function count
As you edit your Question so try below query,remember date formating is a major issue here so keep it same format
select ML.Month,Number_Of_Birthdays_Of_Club_Memebers from
(
select format(Member_Date_Of_Birth,"mm-yyyy") as month_number,count(Member_ID) as Number_Of_Birthdays_Of_Club_Memebers
from club_members
group by format(Member_Date_Of_Birth,"mm-yyyy")
)as T1
inner join months_list as ML
T1.month_number=ML.Month

Use this if you want count the members as per join date.
Select b.Month_Date, Count(a.*) as Total_Members
From Club_Members as a INNER Join Month_List as b
ON a.Member_Join_Date=b.Month_Date
Group BY B.month_Date

Try this :
SELECT Month_Date, sum(MemberCount)
FROM (
SELECT c.Member_ID, c.Member_Join_Date, m.Month_Date,CASE WHEN c.Member_Join_Date < m.Month_Date THEN 1 ELSE 0 END MemberCount
FROM club_members c, months_list m
) s
GROUP BY Month_Date

Related

How can I use a Join from another table when im doing a Max to a column in ORACLE?

Getting This issue in which I'm using a Max to a Column, it returns me the number.
(My tables have already the Constraints).
Actual Return
CVEANO
CVENUMERO
CVEACCION
2021
7
4
2021
1
3
What I Want to Return from TblACCION
CVEANO
CVENUMERO
CVEACCION
CVEACCION NAME Brought from tblACCION
2021
7
4
NAME FOR NUMBER 4
2021
1
3
NAME FOR NUMBER 3
My actual Query is
SELECT
*
FROM
(
SELECT
cveano,
cvenumero,
max(cveaccion) as ultima
FROM
tblbitacoragf
WHERE
cveusuario = 1
GROUP BY
cvenumero,
cveano
order by max(fechaaccion) desc
)
WHERE ROWNUM <= 4
I've tried doing
INNER JOIN tblACCION ta USING (CVEACCION)
and in SELECT
ta.descripcion AS accion
but I guess there's something wrong, because it always asks me for
00000 - "missing right parenthesis"
and I did check for parenthesis, but there's no lack of parenthesis.
UPDATE
I've tried this 2.0 , nothing
As I understood, you are looking for something like this.
SELECT
*
FROM
(
SELECT
t1.cveano,
t1.cvenumero,
max(t1.cveaccion) as ultima,
max(t2.cveaccionName) as cveaccionName
FROM
tblbitacoragf t1
INNER JOIN tblACCION t2 ON t1.cveaccion = t2.cveaccion
WHERE
t1.cveusuario = 1
GROUP BY
t1.cvenumero,
t1.cveano
order by
max(t1.fechaaccion) desc
)
WHERE
ROWNUM <= 4
Also you can try this.
SELECT
abc.*,
xyz.cveaccionName
FROM
(
SELECT
cveano,
cvenumero,
max(cveaccion) as ultima
FROM
tblbitacoragf
WHERE
cveusuario = 1
GROUP BY
cvenumero,
cveano
order by
max(fechaaccion) desc
) abc
INNER JOIN tblACCION xyz ON abc.ultima = xyz.cveaccion
WHERE
ROWNUM <= 4

Postgresql How to Calculate between 2 date depends on another table

let's say i have two table like this :
workday_emp
emp_id work_start work_end
1 "2021-04-06" "2021-04-14"
2 "2021-04-27" "2021-05-04"
3 "2021-04-30" "2021-05-07"
holiday_tbl
id name date
1 "holiday 1" "2021-04-07"
2 "holiday 2" "2021-04-28"
3 "holiday 3" "2021-04-29"
i want to show table like this with a query:
emp_id work_start work_end day_holiday
1 "2021-04-06" "2021-04-14" 1
2 "2021-04-27" "2021-05-04" 2
3 "2021-04-30" "2021-05-07" 1
the question is, how to calculate how many "day_holiday" between "work_start" and "work_end" depends to "holiday_tbl" table?
Please try this. For Employee 3 holiday count will 0 not 1 because his work_day starts at april30 but last holiday was apr29.
-- PostgreSQL(v11)
SELECT w.emp_id, w.work_start, w.work_end
, (SELECT COUNT(id)
FROM holiday_tbl
WHERE holiday_date BETWEEN w.work_start AND w.work_end) day_holiday
FROM workday_emp w
Please check from url https://dbfiddle.uk/?rdbms=postgres_11&fiddle=1948691b58ba841b2765d7de383f8df8
This should do the job:
SELECT emp_id, work_start, work_end, COUNT(ht.holiday) holiday_cnt
FROM workday_emp we LEFT JOIN
(
SELECT date holiday
FROM holiday_tbl
) ht ON ht.holiday BETWEEN we.work_start AND we.work_end
GROUP BY 1, 2, 3
ORDER BY 1, 2;
db<>fiddle

How to get a correlated subquery as column

I dont know how I can do this sql query, probably its simple but I don't know how i can do it.
I have 2 tables:
Table_Articles:
COD NAME
1 Bottle
2 Car
3 Phone
Table_Articles_Registered
COD_ARTICLE DATE
1 05/11/2014
1 06/11/2014
1 07/11/2014
2 08/11/2014
2 09/11/2014
3 05/11/2014
I want take in the table Table_Articles_Registered the row with the MAX date , finally I want get this result:
COD NAME DATE
1 Bottle 07/11/2014
2 Car 09/11/2014
3 Phone 05/11/2014
I need use the sencente like this. The problem its in the subquery. Later I use other inner join in the sentence, this is only a fragment.
select
_Article.Code,
_Article.Description ,
from Tbl_Articles as _Article left join
(
select top 1 *
from ArticlesRegisterds where DATE_REGISTERED <= '18/11/2014'
order by DATE_REGISTERED
)
as regAux
on regAux.CODE_ARTICLE= _Article.CODE
I dont know how can I connect the field CODE_ARTICLE in the table ArticlesRegisterds with the first query.
I think this is a basic aggregation query with a join:
select a.cod, a.name, max(ar.date) as date
from Artiles a join
ArticlesRegisterds ar
on ar.cod_article = a.cod
group by a.cod, a.name
Try this:-
SELECT TAR.COD_ARTICLE, TA.NAME, MAX(TAR.DATE)
FROM Table_Articles_Registered TAR JOIN
Table_Articles.TA ON TAR.COD_ARTICLE = TA.COD
GROUP BY TAR.COD_ARTICLE, TA.NAME;
Can't you just do this?:
SELECT
Table_Articles.COD,
Table_Articles.NAME,
(
SELECT MAX(Table_Articles_Registered.DATE)
FROM Table_Articles_Registered
WHERE Table_Articles.COD_ARTICLE=Table_Articles.COD
) AS DATE
FROM
Table_Articles

Find duplicates within a specific period

I have a table with the following structure
ID Person LOG_TIME
-----------------------------------
1 1 2012-05-21 13:03:11.550
2 1 2012-05-22 13:09:37.050 <--- this is duplicate
3 1 2012-05-28 13:09:37.183
4 2 2012-05-20 15:09:37.230
5 2 2012-05-22 13:03:11.990 <--- this is duplicate
6 2 2012-05-24 04:04:13.222 <--- this is duplicate
7 2 2012-05-29 11:09:37.240
I have some application job that fills this table with data.
There is a business rule that each person should have only 1 record in every 7 days.
From the above example, records # 2,5 and 6 are considered duplicates while 1,3,4 and 7 are OK.
I want to have a SQL query that checks if there are records for the same person in less than 7 days.
;WITH cte AS
(
SELECT ID, Person, LOG_TIME,
DATEDIFF(d, MIN(LOG_TIME) OVER (PARTITION BY Person), LOG_TIME) AS diff_date
FROM dbo.Log_time
)
SELECT *
FROM cte
WHERE diff_date BETWEEN 1 AND 6
Demo on SQLFiddle
Please see my attempt on SQLFiddle here.
You can use a join based on DATEDIFF() to find records which are logged less than 7 days apart:
WITH TooClose
AS
(
SELECT
a.ID AS BeforeID,
b.ID AS AfterID
FROM
Log a
INNER JOIN Log b ON a.Person = b.Person
AND a.LOG_TIME < b.LOG_TIME
AND DATEDIFF(DAY, a.LOG_TIME, b.LOG_TIME) < 7
)
However, this will include records which you don't consider "duplicates" (for instance, ID 3, because it is too close to ID 2). From what you've said, I'm inferring that a record isn't a "duplicate" if the record it is too close to is itself a "duplicate".
So to apply this rule and get the final list of duplicates:
SELECT
AfterID AS ID
FROM
TooClose
WHERE
BeforeID NOT IN (SELECT AfterID FROM TooClose)
Please take a look at this sample.
Reference: SQLFIDDLE
Query:
select person,
datediff(max(log_time),min(log_time)) as diff,
count(log_time)
from pers
group by person
;
select y.person, y.ct
from (
select person,
datediff(max(log_time),min(log_time)) as diff,
count(log_time) as ct
from pers
group by person) as y
where y.ct > 1
and y.diff <= 7
;
PERSON DIFF COUNT(LOG_TIME)
1 1 3
2 8 3
PERSON CT
1 3
declare #Count int
set #count=(
select COUNT(*)
from timeslot
where (( (TimeFrom<#Timefrom and TimeTo >#Timefrom)
or (TimeFrom<#Timeto and TimeTo >#Timeto))
or (TimeFrom=#Timefrom or TimeTo=#Timeto)))

MySql Join with Sum

I have a table called RESULTS with this structure :
resultid,winner,type
And a table called TICKETS with this structure :
resultid,ticketid,bet,sum_won,status
And I want to show each row from table RESULTS and for each result I want to calculate the totalBet and Sum_won using the values from table TICKETS
I tried to make some joins,some sums,but I cant get what I want.
SELECT *,COALESCE(SUM(tickets.bet),0) AS totalbets,
COALESCE(SUM(tickets.sum_won),0) AS totalwins
FROM `results` NATURAL JOIN `tickets`
WHERE tickets.status<>0
GROUP BY resultid
Please give me some advice.
I want to display something like this
RESULT WINNER TOTALBETS TOTALWINS
1 2 431 222
2 3 0 0
3 1 23 0
4 1 324 111
Use:
SELECT r.*,
COALESCE(x.totalbet, 0) AS totalbet,
COALESCE(x.totalwins, 0) AS totalwins
FROM RESULTS r
LEFT JOIN (SELECT t.resultid,
SUM(t.bet) AS totalbet,
SUM(t.sum_won) AS totalwins
FROM TICKETS t
WHERE t.status != 0
GROUP BY t.resultid) x ON x.resultid = r.resultid
I don't care for the NATURAL JOIN syntax, preferring to be explicit about how to JOIN/link tables together.
SELECT *, COALESCE(SUM(tickets.bet),0) AS totalbets,
COALESCE(SUM(tickets.sum_won),0) AS totalwins
FROM `results` NATURAL JOIN `tickets`
WHERE tickets.status<>0
GROUP BY resultid
Try to replace the first * with resultid. If this helps, then add more columns to SELECT and add them to GROUP BY at the same time.