I'm trying to create a system that can calculate money spend on monthly or a year. Now, roughly I have created 3 tables (actually there are 5 but 2 of them are items list and their prices so there is nothing wrong with it) on my database that is:-
(i) Daily - Field (Num, DTotal, Date) >> save all item's value by day
(ii) Monthly - Field (Num, MTotal, StartDate) >> save all values from Daily table from a starting date until the end of month of that date.
(iii) Year - Field (num, YTotal, StartDate) >> save all values from Monthly table from a staring month until the end of the year.
The Daily part I have succeeded but the problem now is with the Monthly part. I don't know how to calculate it. How am I supposed to read the Daily table from a date until another date? Let's say I want to save from 15/8/2011 until 31/8/2011. Help please.
insert into monthly (ytotal, StartDate)
select sum(Dtotal), MONTH(Date) date
group by MONTH(Date)
ON DUPLICATE KEY UPDATE
Also, make sure to put a unique index on Monthly.StartDate. That and the ON DUPLICATE KEY UPDATE clause will protect you in case the insert is run several times.
see http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
Related
Data Tables
I have two data tables similar to the ones above (see link to Data Tables), where the "Raw Data Table" is an ongoing counter per week of sales calls per person. In the table below, I'd like to create an automated formula that can recognize the date ranges in Week Start (Col1) and Week End (Col2) and give the appropriate this week/last week values as this table is updated throughout the year. I had tried the following formula for Carly (Col3), but only got the column header as the output:
=QUERY(A1:D54,"select C where A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' and B <= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' ", 1).
Any help here would be great! Thank you so much in advance!
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 information to be collected monthly. same data columns but different content of course. I'm asking about which are the best way to make the user insert this data, should I make a database table for each month with the same columns, or should I make one table with one column to determine the month.
For example:
table: July
id|program_name|program_date|program_result
table: June
id|program_name|program_date|program_result
Or:
table: monthly_info
id|program_name|program_date|program_result|month
I'm asking which way is more efficient than the other.
Thank you
Create one table to save all your data with date.
table: monthly_info
id|program_name|program_date|program_result|date
Then you can query monthly data as below.
If your condition month parameter is integer. Use this query. (this will return all data matches to month August)
SELECT * FROM monthly_info WHERE MONTH(date) = 8
If your condition month parameter is string. Use this query.
SELECT * FROM monthly_info WHERE DATENAME(mm, date) = 'August'
I have a table lets say:
tblHotel
id
start_date
end_date
rate
Now I want to write procedure for update records for date range, say for example I have data:
id start_date end_date rate
1 2016/01/01 2016/01/10 10
2 2016/01/11 2016/01/20 50
Now if a new date range and rate comes from supplier I want to update tables record like new range is.
start_date end_date rate
2016/01/05 2016/01/12 100
Now updated records should be like this:
id start_date end_date rate
1 2016/01/01 2016/01/04 10
2 2016/01/05 2016/01/12 100
3 2016/01/13 2016/01/20 50
I'm not going to write the code for you, but handling overlapping time frame is tricky. You need to handle this as different cases:
If nothing overlaps, then this is simple:
insert into tbl_Hotel(start_date, end_date, rate)
select $start_date, $end_date, $rate
from dual
where not exists (select 1
from tbl_Hotel h
where h.start_date <= $end_date and h.end_date >= $start_date
);
Easy . . . And in the stored procedure the where can be handled using if logic.
Then the hard part. There are four types of overlaps:
-------hhhhhhhhhhh--------
a) ---------xxxxx------------
b) -----xxxxxx---------------
c) ----------xxxxxx----------
d) --xxxxxxxxxxxxxxxxxxxxxx--
And, then it gets a bit more complicated because a new rate period could overlap with more than one existing period.
Arrrg! How do you approach this? Carefully and with test cases. You might even want to use a cursor (although there are non-cursor-based methods as well).
The idea is to pull out one overlapping existing period. Then, for that period handle the logic:
a) The existing period needs to be split into two parts (with appropriate end dates. Then the new reservation can just be added.
b) The start date of the existing period has to change to one more than the end date of the new one. Then the new one inserted.
c) The end date of the existing period has to change to one less than the start date of the new one. Then the new one inserted.
d) The old record is removed and the new one inserted.
As I say, good tests for your stored procedure are important, so you can actually check that it works.
I've created a website showing the prices of the fruits and vegetables of my province. These prices are updated every day (aprox. +250 rows each day).
For that reason I created a MySql table to speed up the access to this information.
On the table showed on my Web site I want to limit the date showed up to 1 week and not all the dates available on the database. Taking in consideration that the database is increased daily I need a query for this dynamic date request.
They query I'm using right now is:
SELECT base_beta.Tipo,
base_beta.fecha_numero,
base_beta.Variedad,
base_beta.Fecha,
base_beta.alhondiga,
base_beta.corte_uno,
base_beta.corte_dos,
base_beta.corte_tres,
base_beta.corte_cuatro,
base_beta.corte_cinco,
base_beta.corte_seis,
base_beta.corte_siete,
base_beta.corte_ocho,
base_beta.corte_nueve,
base_beta.corte_diez,
base_beta.corte_once,
base_beta.corte_doce,
base_beta.corte_trece,
base_beta.corte_catorce,
base_beta.corte_quince
FROM base_beta
WHERE 1=1
AND base_beta.Tipo = 'pi'
ORDER BY base_beta.fecha_numero DESC
There are serveral columns with corte (aka prices) because every item has a random number of prices, since the fruits and vegetables has a bid system where prices decrease on every round.
For example, a tomato may start at 0,80€ and then its prices decrease to 0.76, 0.74, 0.69 and so on.
What code do I have to add to show the data of the current day and the next 6 days to complete a week?
Here are a photo of the: database content and format
Thanks, Jose.
If this defines your table schema
CREATE TABLE prices (item INT UNSIGNED, price DECIMAL(5,2), date DATETIME);
the following query will retrieve prices up to 7 days back in time
SELECT * FROM prices WHERE date > SUBDATE(CURDATE(),7) AND date <= CURDATE();
See also https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Hope this helps.