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

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!

Related

Create new column storing average of rows from another column in MySQL

I am trying to create a new column that has stores the 'Average weight of the field'. For example, the answer for RaceID = 123 would be 54.5. The RaceID's are not organised from smaller to largest and are displayed randomly like the example below.
RaceID
Weight
No. Starters
123
56
2
124
58
2
123
53
2
125
60
2
125
51
2
124
62
2
Try below query, It will display current table data along with average column :
select t.*,
avg(Weight) over(partition by raceID order by raceID ) avg_raceID
from table t;
SELECT RaceID, AVG(Weight) AS val_1
FROM dataset_name
GROUP BY RaceID;
By using above code we can get the average value of weights for every unique RaceID. Check the below image for better understanding.
https://i.stack.imgur.com/kMA68.png
Let me know if there are any modifications or error.

If a particular record is not available then I need to display as zero in MYSQL query

SELECT DATE(login) AS loginDate, COALESCE(COUNT(login),0) AS loginCount
FROM LMS_LoginTracking
WHERE prism_mem_id = 55308
AND DATE(login) BETWEEN '2017-01-04T18:30:00.000Z' AND '2017-01-09T18:30:00.000Z'
GROUP BY DATE(login)
Output is :
LoginDate LoginCount
2017-01-05 406
2017-01-06 558
2017-01-08 11
2017-01-09 406
Here, what I am expecting is 2017-01-07 should be 0 as this date is not available in the data, even though I need the result for that particular date as zero result.
Required output:
LoginDate LoginCount
2017-01-05 406
2017-01-06 558
2017-01-07 0
2017-01-08 11
2017-01-09 406
Please help me on this.
There are lot's of different ways in which this can be achieved. The easiest I know is to create a table that stores the dates of interest. Then fill it up with the relevent data. Let's call it work_days
CREATE TABLE work_days(
work_day date NOT NULL PRIMARY KEY
);
And then you can use a left join on it as follows
SELECT date(login) as loginDate, COALESCE(count(login),0) as loginCount
FROM work_dates LEFT JOIN LMS_LoginTracking
ON work_dates.work_day = date(LMS_LoginTracking.login) /* edit as appropriate */
WHERE prism_mem_id=55308 AND DATE(login) BETWEEN '2017-01-04T18:30:00.000Z' AND '2017-01-09T18:30:00.000Z' group by date(login)
You haven't posted your tables so cannot be tested here. Therefor this query may not work in it's exact form. Please edit as appropriate

SELECT all_data WHERE value_in_column LIKE specific_string

I have a MySQL database with a table that contains a lot of data for a certain vehicle across many different days. In this table there is a column called time_stamp that has the date the data was taken like so:
ID UnitID time_stamp data more_data
0 56 2016-08-10 12:01 asdf 12
1 57 2016-08-09 11:19 ghjk 34
2 57 2016-08-09 9:35 qwer 56
3 58 2016-08-09 1:16 tyui 78
I want to select everything in the table where UnitID LIKE 57 and time_stamp LIKE 2016-08-09% so that it would return:
ID UnitID time_stamp data more_data
1 57 2016-08-09 11:19 ghjk 34
2 57 2016-08-09 9:35 qwer 56
I have tried SELECT * FROM table_name WHERE time_stamp LIKE 2016-08-09% AND UnitID LIKE 57; but I keep getting a Error Code: 1054 telling me that there is an unknown column in 'where clause'. I am still very new to MySQL but I can usually figure out what I need to do with the help of the MySQL Docs, however this one I cannot figure out. I have also looked for similar questions like this one here, but nothing I have found is able to accomplish exactly what I want.
LIKE is for string matching and as such is comparatively slow to compared to more appropriate operators. Your query would be better with a WHERE such as this:
WHERE UnitID = 57 AND time_stamp BETWEEN '2016-08-09 00:00:00' AND '2016-08-09 23:59:29'
That said, MySQL's main objection to the query you had was probably that you did not enclose 2016-08-09% in quotes.
You don't need LIKE clauses here. Try this:
SELECT * FROM table_name
WHERE
time_stamp >= Convert(datetime,'2016-08-09')
AND
time_stamp < Convert(datetime,'2016-08-10')
AND
UnitID = 57
I don't think you're going to need LIKE for this query because the data that you're looking for isn't TEXT, VARCHAR, or CHAR data. Try doing this in your predicate:
WHERE UnitID = 57 AND DATE(time_stamp) = '2016-08-09'

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