I have an employee database that shows me peer week the couple of employees who work together,
for the next week i need to know how many rest days that employee had...
Every employee had an unique ID
Depending of the week you work as Employee1 or Employee2
The calculations of the rest days starts in week 2 so 9999 is the default number if there is no restDay
so, let's take for example the employee with id 2, in the week 1 he works as emp2, but the next week he works as emp1. I need to obtain the rest days, which in this case it will be for the emp1...
the columns in my db are
1. id
2. employee1
3. employee2
4. week
5. emp1_restDay
6. emp2_restDay
7. date_time
db example
Bad data structure, but I guess you have to deal with it. You need to get one column per employee, which you can do using union all:
select employee, sum(restDay)
from ((select id, week, employee1 as employee, emp1_restDay as restDay, datetime
from table t
) union all
(select id, week, employee2 as employee, emp2_restDay as restDay, datetime
from table t
)
) t
group by employee;
Related
My Table:
datetime. employment. name
2019-11-25 12:32:12. office. Michael Jackson
2020-01-31 12:32:22. production. Jenny Darling
2019-12-25 12:32:12. office. Michael Jackson
etc.
This is a "time registering" table, so names must be DISTINCT for each mounth. (How many unique names is there every month grouped by month and employment)
Now i'd like to create a table that will show how many employees there was in every month by year.
So the table will look like:
Year & Month. Employment. Number (Unique names)
-------------------------------------
2019-01. Office. 50
2019-01 Production. 35
2019-02. Office. 45
2019-02. Production. 36
And so on for this and prev year (2019 & 2020)
Something like:
SELECT * FROM table COUNT(DISTINCT(name)) AS number GROUP BY datetime AND employment
You seem to want aggregation... but your query is invalid in several regards. I think you want:
select
date_format(datetime, '%Y-%m') yr_month,
employment,
count(distinct name) no_unique_names
from mytable
group by yr_month, employment
This gives you on row per year/month and employment, with the corresponding count of distinct names.
Hello I have a table that Has a movie name, the date it was released, the cost and the revenue. I am trying to write a query to display how many movies were released per month
CREATE TABLE movies_200249154(
movieName VARCHAR(30),
releaseDate DATE,
costInMillions INT (20),
revenueInMillions INT (20)
);
Above is my table
Here is my current query
SELECT DISTINCTROW (monthname(releaseDate)) AS 'Month released', COUNT(*)movieName
FROM movies_200249154
GROUP BY movieName;
Right now my query will display the month, and the count for the movie (always being 1) and shows duplicates. If I use SELECT DISTINCT it only displays 1 movie per month. Seemingly forgetting about the other data.
What I need is for my query to display the month AND the amount of movies released that month. For example it currently repeats June 4 times with a count of 1. The query I need would display June and a count of 4.
Any help is greatly appreciated
You can use MONTH function of MySQL and group by with count to get the required value, e.g.:
SELECT MONTH(releaseDate), count(*)
FROM movies
GROUP BY MONTH(releaseDate);
I am sure this is pretty simple but I cannot figure out what I am doing incorrectly here. I am trying to get totals for certain fields over the last 7 days and group them by date and field.
Example:
The following table:
TABLE API_LOG
COLUMNS
customer
ip_address
date_logged
endpoint
So I would like to get the total endpoint calls for the last 7 days per customer and ip_address. I already have the daily totals organized but for some reason cannot get this to group correctly.
SELECT date_logged AS DATE_LOG, Customer,
COUNT(customer) AS CustomerCount, COUNT(ip_address) as IPCOUNT
FROM API_log
GROUP BY date_logged, Customer
The idea is then to bind this to a chart.
Sample data:
Expected output;
Current Output:
Appreciate assistance with this.
edit:
I group by date because I want to see the total for each day for the last 7 days. In other words, give me the total counts for each customer by day.
I think your date_logged column is datetime so it makes sense that you are getting separate dates when grouping. Try this
SELECT CAST(date_logged AS DATE) AS DATE_LOG, Customer,
COUNT(customer) AS CustomerCount, COUNT(ip_address) as IPCOUNT
FROM API_log
GROUP BY CAST(date_logged AS DATE), Customer
I have a table
PEOPLE, DATE, DELETED
Amanda, 2015-03-01, Null
Ray, 2015-03-01, Null
Moe, 2015-04-01, Null
Yan, 2015-05-01, Null
Bee, 2015-05-05, 2015-06-12
now I need to group it and sum it with months like this:
March: 2 people
April: 3
May: 5
June: 5
July: 4
so new people should not be counted in previous month but they should be in next months for my range (January - June). And if man is DELETED, he should be counted together with another people last time in month when he has been deleted.
How to write query for this?
This can be at least solved using running totals. This just the outline how to do it, you'll need to do some work for the actual solution:
select people, date, 1 as persons from yourtable
union all
select people, deleted, -1 as persons from yourtable where deleted is not null
Then do a running total of this data, so that you sum the +-1 persons -field, and that should give you the amount of people that are there so far.
For the events happening in the middle of the month, you'll have to adjust the date to be the start of that or the next month whichever way you want them to be calculated.
If you need also those months when no changes happened, you'll probably need a table that contains the first day of each month for the biggest range of dates you'll ever need, for example 1.1.2000 - 1.12.2100.
I have two tables that are linked by an ID, and one table has a start date, and the child (linked) table has weekly entries of data. I need to be able to query and determine the ID's, that are missing a week's data, without knowing the actual dates.
Table1
ID INT
START_DATE DATE
Table2
ID INT (foreign Key to Table 1)
TRAN_DATE DATE
VALUE INT
Each INT might have a different start date, and the values are saved weekly (every Monday, Tuesday, etc... based on Start Date)
Some IDs will have missed posting their value one week, and I need to look back historically for when a record is missing.
Assuming a Start_Date of Sept 9, 2013, the dates would be (9/9/2013. 9/16/2013, 9/23/2013,...) I need to see if TRAN_DATE for ID 1 is 9/9/2013, then add 7 days (9/16/2013), and check for that record, then add 7 days (9/23/2013) and check for that record to exist. Then repeat for the different IDs. This would end with the current date, or any date into the future (if this is easier).
I can do this with a program simply enough, but I need to do this at a customer site and I can not distribute code into the site, so I need to try to do it with a query).
The following query returns any gaps in table2:
select distinct id
from table2 t2
where t2.tran_date < now() - interval 7 day and
not exists (select 1
from table2 t2a
where t2a.id = t2.id and
datediff(t2a.tran_date, t2.tran_date) = 7
);
This assumes that the first transaction is not missing. Is that possible?