Access - Finding duplicates between 2 tables between 2 dates - ms-access

I need your help in access how to count the duplicated during certain period
Table One : Unique Values
Number
Close Time
Max Date
1110
01-11-2022 8:47:00 AM
01-12-2022 8:47:00 AM
1111
02-11-2022 8:47:00 AM
02-12-2022 8:47:00 AM
1112
03-11-2022 8:47:00 AM
03-12-2022 8:47:00 AM
Table Two : Contain the unique value and duplicated Value
Number Close Time
1110 01-11-22 8:47
1110 02-11-22 8:47
1110 03-11-22 8:47
1111 02-11-22 8:47
1111 05-11-22 8:47
1111 06-12-22 8:47
1112 03-11-22 8:47
1112 08-12-22 8:47
1112 09-12-22 8:47
i need 2 types of result
first one include the value im looking for like this
Result Count
1110 3
1111 2
1112 1
and the second type exclude the value im looking for
Result Count
1110 2
1111 1
1112 0
i can do it in excel using =countifs but now im working on huge data ( millions of records ) and most of the data sent on access files
enter image description here
i dont know why tables became like this so i added a photo

Perhaps this query will suit you
SELECT t3.Num1 AS Num, sum(inResult1) AS CountInclude, sum(inResult2) AS CountExclude
FROM
(SELECT t1.Number as Num1,t1.CloseTime as CloseTime1,t1.MaxDate
,t2.Number as Num2,t2.CloseTime as CloseTime2
, iif(t2.CloseTime >= t1.CloseTime and t2.CloseTime<=t1.MaxDate,1,0) AS inResult1
, iif(t2.CloseTime > t1.CloseTime and t2.CloseTime<=t1.MaxDate,1,0) AS inResult2
FROM tableOne AS t1 INNER JOIN tableTwo AS t2 ON t1.Number = t2.Number
) AS T3
GROUP BY T3.Num1;
You may write "LEFT JOIN", this will be better.

Should be doable with a GROUP BY and COUNT(*):
SELECT Number, COUNT(*)
FROM tableTwo
GROUP BY Number, CloseTime
For the second result, just put COUNT(*)-1.

Related

MySQl v5.6 How to exclude unwanted / incorrect rows produced after Joining two tables

I have two tables , I want to fetch correct data rows, but when I make a join in between incorrect extra rows are getting create. I want to exclude them.
Table T1
Employee_ID
Work_START
1111
10 Nov
1111
14 Nov
1111
18 Nov
Table T2
Employee_ID
Work_END
1111
12 Nov
1111
15 Nov
1111
20 Nov
I can not use Rank Function since MySQL version is 5.6 and I have Read access to DB so can not create INDEX or use SET function
I tried to make a join with Below SQL Query:
Select T1.Employee_ID, T1.Work_START, T2.Work_END from T1
Left Join T2 On T1.Employee_ID = T2.Employee_ID
where T2.Work_END > T1.Work_START
(used this condition to reduce the incorrect joined rows)
I tried using Left , Right join, using Distinct function as well
I am getting the result as below
Order_ID
Order_Date
Ship_Date
1111
10 Nov
12 Nov
1111
10 Nov
15 Nov
1111
10 Nov
20 Nov
1111
14 Nov
15 Nov
1111
14 Nov
20 Nov
1111
18 Nov
20 Nov
Expected Result is as below
Logic of Output : an employee has worked on a task on 3 different times, so to get those correct 3 rows I want the expected table to follow the below condition
row 1 work_END should be higher than row 1 Work_START and
2nd row Work_START should be higher than 1st row Work_END and so on
next row Work_start should be higher than previos row Work_END
Expected Table
Order_ID
Order_Date
Ship_Date
1111
10 Nov
12 Nov
1111
14 Nov
15 Nov
1111
18 Nov
20 Nov
Please Note: I Have read access to DB and Can not use Rank function since MySQL version is 5.6
Disclaimer : you should consider fixing your data model. Spreading that data over two different tables does not look like appropriate design.
With the current data model, we could approach the question like so: starting from each beginning date in the first table, bring the closest end date with a subquery:
select t1.employee_id, t1.work_start,
(
select min(t2.work_end)
from t2
where t2.employee_id = t1.employee_id and t2.work_end > t1.work_start
) work_end
from t1
This guarantees as many rows in the resultset as there are in the first table (not more, not less). If the dates of the two tables do not properly interleave, you might see results that look inconsistent somehow (using row_number() would not avoid this).

MYSQL select the date from one table that not exist in the second table

i have 2 tables
presence table:
matricule date_effet
248 2017-01-30
248 2017-01-31
248 2017-02-01
248 2017-02-02
Activities table :
Matricule date
248 2017-01-31
248 2017-02-01
248 2017-02-02
what i want is to extract the dates that exist on the first one and don't exist in the second one in this case 2017-01-30 knowing that the user will select a range date for exemple in this case maybe date between 2017-01-28 and 2017-02-02
try with
SELECT * FROM presence
MINUS
SELECT * FROM Activities
from SQL EXCEPT / MINUS
Try this:
SELECT date_effet FROM presence WHERE date_effet NOT IN(SELECT date FROM activities)
The inner SELECT statement will select all the dates from second table, and then the first select will select all the dates from first table which do not exist in the second table.
Hope it helps!

last entered value row in group by Mysql

My Query-
SELECT id, MAX(sl), details_sp FROM shareholderreceive GROUP BY id
and result
id MAX(sl) details_sp
1 76
2 74
3 64
4 67
5 69
6 70
10 72
But i need Those MAX(sl) Where details_sp column has last entered value.
My expected table is -
id MAX(sl) details_sp
1 72 Jul'16
2 74
3 64
4 62 Aug'16
5 69
6 70
10 71 Aug'16
here, data type details_sp is Varchar.
What query do I need to get this in MySQL?
What I think you mean is that when there is a details_sp filled in you need the sl value of that row and when there is no details_sp you need the max sl value?
If so, don't use MAX() but use the ORDER BY with the GROUP BY.
SELECT id, sl, details_sp
FROM shareholderreceive
ORDER BY details_sp DESC, sl DESC
GROUP BY id
The only problem with this is that you can never order by a date when it is a varchar value. This means you can never get the latest entry with just a varchar as a date.
So if there is a way, make the details_sp a date value (when you don't have a day just use the first day of the month, so 2017-06-01 for juli 2017) and use DATE_FORMAT() in MySQL or date_format in PHP (or JAVA or etc).

Missing values on count in mysql

I'm just stuck with this issue atm and I'm not 100% sure how to deal with it.
I have a table where I'm aggregating data on week
select week(create_date),count(*)
from user
where create_date > '2015-02-01'
and id_customer between 9 and 17
group by week(create_date);
the results that I'm getting have missing values in the count, as shown below
5 334
6 376
7 394
8 405
9 504
10 569
11 709
12 679
13 802
14 936
15 1081
16 559
21 1
24 9
25 22
26 1
32 3
34 1
35 1
For example here from 16 to 21 there a obviously 4 values missing I would like these values to be included and count to be 0. I want this because I want the weeks to be matching with other metrics as we are outputting them in an excel file for internal analysis.
Any help would be greatly appreciated.
The problem is that an sql query cannot really produce data that is not there at all.
You have 3 options:
If you have data for each week in your entire table for the period you are querying, then you can use a self join to get the missing weeks:
select week(t1.create_date), count(t2.id_customer)
from customer t1
left join customer t2 on t1.id_customer=t2.id_customer and t1.create_date=t2.create_date and t2.id_customer between 9 and 17
where t1.create_date > '2015-02-01'
group by week(t1.create_date)
If you have missing weeks from the customer table as whole, then create a helper table that contain week numbers from 1 or 0 (depending on mysql config) to 53 and do a left join to this helper table.
Use a stored procedure that loops through the results of your original query and inserts the missing data in the resultset using a temporary table and then returns the extended dataset as result.
The problem is that there is no data matching your criteria for the missing weeks. A solution will be to join from a table that has all week numbers. For example if you create a table weeknumbers with one field weeknumber containing all the numbers from 0 to 53 you can use something like this
select weeknumber,count(user.*)
from weeknumbers left join user on (weeknumbers.weeknumber=week(user.create_date)
and user.create_date > '2015-02-01'
and user.id_customer between 9 and 17)
group by weeknumber;
Additionaly you might want to limit the week numbers you do not want to see.
The other way is to do it in the application.

mysql query group function as a column

I need to write a query to calculate the sum of each account but the special thing that I want is to group the some of the debit of each month as a column.
So what I need is like this example:
SELECT accid "Account ID", Left(tr_date,7) "Date", SUM(debit) "Debit Sum"
FROM transactions
WHERE tr_date Between "2014-07-01" and "2014-09-30"
GROUP BY acc_id, LEFT(tr_date,7);
The result will be something like that:
Account ID Date Debit Sum
1111 2014-07 300
1111 2014-08 351
1111 2014-09 352
1123 2014-07 500
1123 2014-08 100
1123 2014-09 230
But what I was asked to have is the following result:
Account ID 2014-07 2014-08 2014-09
1111 300 351 352
1123 500 100 230
where the user chooses the dates so the number of the columns is not limited
is there any way to do this or no? thank you for any comment or answer.
The only way to do that is to know the dates beforehand.
SELECT accid "Account ID"
SUM(if(LEFT(tr_date,7)="2014-07",debit,0)) "2014-07"
SUM(if(LEFT(tr_date,7)="2014-08",debit,0)) "2014-09"
SUM(if(LEFT(tr_date,7)="2014-09",debit,0)) "2014-08"
FROM transactions
WHERE tr_date Between "2014-07-01" AND "2014-09-30"
GROUP BY acc_id
if you need the user to be able to choose or add columns, you'll need to build the query dinamically either with a stored procedure or another scripting language.
PD: you're treating your dates as strings and I kept that logic. It's not a good practice though