I have two tables.
One contains information on a given reporting date and another has information on an incident date.
I’m trying to generate a list of the latest information before a given incident (i.e. if an incident occurred on 12/15/2015 and I have reports for 12/15/2014, 12/1/2015, and 1/12/2016, I want to pull the information for 12/1/2015 from that Data set. If on the second row there is another incident that occurred on 1/13/2016 and I have the same data as above I only want to return the information on 1/12/2016).
Dates are in a different column than ID.
Tables are in the following format
ID - DateV- ValueX
JAS 2017-12-15 00:00:00 3.45
I’ve tried running a query similar to below, but it only returns the first item ID-DateCombination not a list of all items. I would even be fine with just the latest reporting date by incident ID. That would give me enough to get the values I need.
Reporting Table - TableRep
Incident Table - TableInc
SELECT TableRep.ID, TableRep.DateV, TableRep.ValueX
FROM TableRep
INNER JOIN Table
ON TableInc.DateP > (
SELECT Max(TableRep.DateV) FROM TableRep;);
I’ve narrowed it down to only records before a given date in the table, but I’m struggling to lose the dates other than the latest ones.
Essentially, I would like to pull the id and Date from TableInc and the first date before the date indicated in TableInc from TableRep for each record in TableInc. Some IDs have multiple entries, so I can’t just pull them all and choose the max for each ID, that would only represent about 50% of the population.
Related
I have checked around but can't seem to find a solution to this, which is why I'm asking for help here.
I am working on a project with Laravel 5.8. I have a mysql table storing customer's orders. The orders table have among other columns ID, order No and a created_at column storing the timestamp of the order.
Now I want to return a collection that contains all the dates there are orders and possibly the number of orders for those dates, returned just once like so;
7/10/2019 - 5
6/10/2019 - 6
5/10/2019 - 12
4/10/2019 - 9
I want only the date part of the timestamp of the days when there are orders. The days without orders should be skipped.
I have done this;
Order::selectRaw('Date(created_at) as date')->groupBy('date')->get();
But keep getting only today's date.
I have a table of travel expenses for analysis.
I would like to create a calculated column with a value for the maximum count of records with a certain category for each employee on any given day.
For example, if the category being reviewed is "dinner", we would like to know what is the maximum number of dinner transactions charged on any given day.
The following custom expression was able to count how many dinner expenses per employee:
count(If([Expense Type]="Dinner",[Expense Type],null)) over ([Employee])
But when trying to get the max count over days, I cant seem to get it to work. Here is the expression used:
Max(count(If([Expense Type]="Dinner",[Expense Type],null)) over ([Employee])) over (Intersect([Employee],[Transaction Date]))
This seems to provide the same answer as the first expression. Any idea on how to get this code to identify the value on the date with the most expenses for each employee?
If i understand your question and comments correctly, you should be able to use intersect.
count(If([Expense Type]="Dinner",[Expense Type],null)) over (Intersect([Transaction Date],[Employee]))
You may need to cast [Transaction Date] as a date if it is an actual DateTime. Otherwise you'd get one for each unique DT.
So I'm working on a schedule system for my job a basically i wanted to know if there is a way where mysql can do something like:
|Monday |tuesday|wendsday|total
|Dan |5am-7am |7am-6pm|6am-11am|
11am-2pm| |2pm-7pm |
5pm-12am|
where i can enter multiple shifts on 1 day for each person in the same cell if needed instead of the name repeating several times like:
Dan|5-4|
Dan|6-8|
and if there is a function to calculate total time in one cell with multiple shifts
There is a way (representing the data as string), but you wouldn't want to do this - you will loose all calculations, searches etc.
You should not try to represent the data in the database exactly as how it looks on paper.
I would make a table like this:
ShiftID|Person|StartTime|EndTime
Making StartTime & EndTime columns of type DATETIME, you will store not only the HH:mm of a shift's start, but also the day. This is helpful when you have a shift which starts on one day and ends in the next, like starting on Monday 2017-05-15 23:00 and ending on Tuesday 2017-05-16 02:00.
You can extract the date only from this filed using MySQL DATE() function and select only those entires which start OR end on this day.
To calculate the shift's duration you can use MySQL function TIMESTAMPDIFF()
You can even use DAYOFWEEK() to get if it is Monday, Tuesday, etc.
About duplicating the person's name - I would make another table, which will match users with their data to IDs an use ID in the column Person, but for a starter and if your data is not big and if speed is not an issue and if typo errors (like Den instead of Dan) are not a problem ... you could use the name directly in this table.
After storing the data in a table like this you could represent it as you wish in HTML (or print).
You can create a third table with the following columns:
person_id int,
start_time datetime,
end_time datetime
Where person_id would be foreign key to Person table and start_time and end_time would be datetime columns. You can then store multiple records for a person in this table and use MySQL's date functions with GROUP BY to generate the report similar to the one in question.
I have a table with the following structure:
Entry ID | Date | Approved
Whenever a new entry is made, Entry ID auto increments and date is set to whenever the entry was made through the web application. These entries are not necessarily made every day, so there are gaps between dates.
I need to find all "missing" entries, meaning that there is no entry for that date. For instance, if there was an entry for 2015-06-01 and the next one didn't come until 2015-06-07, I need a query that returns the list of dates from 2015-06-02 to 2015-06-06 and an indication of their approved status from that field. I've been looking for a while but can't seem to find a method to get a list of entries that don't exist. Is there a method for this, or should I restructure?
Create a temp table with all possible dates and do
SELECT Date FROM temp_table WHERE Date NOT IN (SELECT Date FROM your_table);
I am having trouble wrapping my head around a query I'm supposed to write.
I've got two tables: stock and articles
Columns in articles:
id
name
price
Columns in stock:
id
articleId
tstamp_in (yyyy-mm-dd hh:mm:ss)
For every article that is delivered to me by my suppliers, I create a new row in the stock table. That means that every article can have multiple rows in the stock table. One of my colleges asked if I could supply a list that shows him all new articles, so in other words all records from stock that have a tstamp_in value higher than last midnight.
I came to the following query but I really cant figure out how to get the result I am looking for.
SELECT *
FROM stock
LEFT JOIN articles ON stock.articleId=articles.id
WHERE tstamp_in>'2015-01-12 00:00:00'
This gives me all records in the stock table created after last midnight. However this does not mean that there aren't any previous records. What I'm trying to figure out is how I can get this in one query.
In short:
Select all from stock grouped by articleId where tstamp_in > 2015-01-12 00:00:00 and not having records with that articleId before 2015-01-12 00:00:00
Is this possible?
Think of the the problem a bit differently. For what articles is the earliest tstamp_in today? That leads you to an aggregation and having:
select s.articleId
from stock s
group by s.articleId
having min(tstamp_in) > '2015-01-12'