Group By Date, Count Of Multiple Fields Within Date
take table a:
userID INT,
userLogin VARCHAR
and table b:
customerID INT,
userID INT,
customerStarted DATE,
customerFinished DATE
what I'm looking to produce is a report by date, showing number of starts and finishes for all customers marked with a specific userID, like
User 4:
Date | starts | finishes
2008-10-01 | 0 | 5
2008-10-02 | 3 | 3
2008-10-03 | 4 | 2
2008-10-04 | 0 | 0
2008-10-05 | 3 | 3
...
etc.
View Complete Forum Thread with Replies
Related Forum Messages:
Using Group By Date And Count
Is there a way to select count(*) by grouping by date, and having multiple date ranges? combining... select field,count(*) from table where datefield > 2004/1/1 and datefield < 2004/1/31 and select field,count(*) from table where datefield > 2004/2/1 and datefield < 2004/2/29 so that the output is field 34 field 40
View Replies !
Count And Group By Month In 1 Query For A Date Range?
I have a table that has employees work history on it. Basically the only thing I am worried about is the start and end date. My boss wants a report that charts out the quantity of workers for the past 12 months group by the month. For example Employee--Start Date--End Date Charles-----2008-6-3---2009-2-1 Vicky-------2009-1-1---Present So in this case there would be a quantity of 1 from June 08 to December 08. Then their would be a quantity of 2 from Jan 09 to Feb 09. And Vicky would also be counted for March since she is still working. I can do this by creating PHP functions and putting it all together, but I was just wondering if there was a way to do this all in 1 query.
View Replies !
Extract Date Into Multiple Form Fields
I'm storing standard datetime values in a MySQL table (i.e. 1999-12-31 23:59:59). A query such as: "SELECT thedate FROM tablename" returns the expected 1999-12-31 23:59:59 How can I extract the indivdual elements into separate PHP variables like: theyear = 1999 themonth = 12 theday = 31 thehour = 23 theminute = 59 thesecond = 59 I've searched through the forums and thought DATE_FORMAT would be the ticket, but so far haven't been able to find anything that addresses this particular issue.
View Replies !
Output Multiple Records From Start Date To End Date?
Firstly I'm using MySQL 5 on Win2003. I have a DB table that has multple columns, but the 2 columns I am interested in are DATE_START and DATE_END. So I'll have many records in the DB and the values in these fields will be anything like: DATE_START=2006-10-23 DATE_END=2006-10-26 Or if its a 1 day thing it will be sumin like: DATE_START=2006-10-23 DATE_END=2006-10-23 Is there anyway I can output multiple records from the DB for that one record between those dates? For example, the first record above would output: TITLE | SUMMARY | 2006-10-23 TITLE | SUMMARY | 2006-10-24 TITLE | SUMMARY | 2006-10-25 TITLE | SUMMARY | 2006-10-26 So basically I get a loop of all dates between the start and end date? Before anyone asks, no I can't loop through this in the code! I have a calendar, and all the dates are created and I just need to populate these dates with the records... and I won't go into detail, but without querying every day, this is the best way by far to do it... just need to know if it can be done?
View Replies !
Pagination By Date With Multiple Rows For Each Date
I'm not exactly sure what is the best method to use when trying to paginate my results. Currently I have 2 tables: entries ------- entryDate entryText images ------- entryDate imageName This is the query that I have been using to display all entries and their images on one page: Code: SELECT e.entryDate , e.entryText , i.imageName FROM entries AS e INNER JOIN images AS i ON e.entryDate = i.entryDate ORDER BY e.entryDate.........................
View Replies !
GROUP BY Multiple Fields
Can somebody show me how to properly use GROUP BY if I'm using multiple fields. For instance: I have a table that has a month field and a year field. I want to group all entries by month and year. How do I prevent grouping a record from January 2007 with a record from January 2008?
View Replies !
Multiple COUNT() And GROUP BY
I think I'm on the right track, but I could use some help. I have a simple database with one table that contains observations of animal behavior. Columns include date, time, and animal_type. I want an output that shows the number of times each animal type was observed, grouped by date. Something like this (sorry about the formatting) date cat dog mouse 2004-1-1 1 2 3 2004-1-2 2 3 5 2004-1-3 1 2 3 I've gotten as far as getting one column, which would be this query: "select date, count(date) as cat from observations group by date" Can any one tell me how to get the rest?
View Replies !
How To Group By Date
I have following entries in Mysql database. /*******/ 2007-12-06 08:38:35 2007-12-06 09:02:40 2007-12-06 09:03:08 2007-12-06 09:04:41 2007-12-06 09:05:25 2007-12-07 08:04:28 2007-12-07 08:14:14 2007-12-07 09:01:01 2007-12-07 10:01:02 2007-12-07 11:01:01 2007-12-08 00:01:01 2007-12-08 01:03:02 2007-12-08 01:04:02 2007-12-08 01:05:02 2007-12-08 01:06:02 /******/ What I want is to group by dates without considering time. From above example, I want to get one record for ��-12-06', one record for ��-12-07', and one record for ��-12-08'.
View Replies !
Group By ID Having MAX(date)
I have a table such as the following: Code: ID | Forename | Surname | Created --------------------------------- 1 | Tom | Smith | 2008-01-01 1 | Tom | Windsor | 2008-02-01 2 | Anne | Thorn | 2008-01-05 2 | Anne | Baker | 2008-03-01 3 | Bill | Sykes | 2008-01-20 Basically, I want this to return the most recent name for each ID, so it would return: Code: ID | Forename | Surname | Created --------------------------------- 1 | Tom | Windsor | 2008-02-01 2 | Anne | Baker | 2008-03-01 3 | Bill | Sykes | 2008-01-20 The query (and variations of) I've been trying, and failing with is: Code: SELECT DISTINCT ID, Forename, Surname FROM Name GROUP BY ID HAVING MAX(Created)
View Replies !
Group Date Bimonthly
sentence--> "select * from dateTest group by month(fecha),year(fecha) order by fecha desc;" +---------------------+ | fecha | +---------------------+ | 2008-12-01 00:00:00 | | 2008-11-01 00:00:00 | | 2008-10-01 00:00:00 | | 2008-09-01 00:00:00 | | 2008-08-01 00:00:00 | | 2008-07-01 00:00:00 | | 2008-06-01 00:00:00 | | 2008-05-01 00:00:00 | | 2008-04-01 00:00:00 | | 2008-03-01 00:00:00 | | 2008-02-01 00:00:00 | | 2008-01-01 00:00:00 | | 2007-12-01 00:00:00 | | 2007-11-01 00:00:00 | | 2007-10-01 00:00:00 | | 2007-09-01 00:00:00 | | 2007-08-01 00:00:00 | | 2007-07-01 00:00:00 | | 2007-06-01 00:00:00 | | 2007-05-01 00:00:00 | | 2007-04-01 00:00:00 | | 2007-03-01 00:00:00 | | 2007-02-01 00:00:00 | | 2007-01-01 00:00:00 | +---------------------+ and I've obtained all first values per month and year. Now, I want to obtain all first values per BImonth and year. I want to receive, the following result: sentence--> "???????????????????????????" +---------------------+ | fecha | +---------------------+ | 2008-11-01 00:00:00 | | 2008-09-01 00:00:00 | | 2008-07-01 00:00:00 | | 2008-05-01 00:00:00 | | 2008-03-01 00:00:00 | | 2008-01-01 00:00:00 | | 2007-11-01 00:00:00 | | 2007-09-01 00:00:00 | | 2007-07-01 00:00:00 | | 2007-05-01 00:00:00 | | 2007-03-01 00:00:00 | | 2007-01-01 00:00:00 | +---------------------+ What is the sentence which I receive this result?
View Replies !
Date Functions In Group By
I've got a table with visitor statistics where one of the fields is a timestamp generated with UNIX_TIMESTAMP(). Each hit on the website is recorded as a row. Now i'd like to get the busiest day, the day with the most hits. Is this anyway possible with the Date and Time functions from Mysql? GROUP BY DAYOFYEAR(ts) or something similar?
View Replies !
Group With Two Date Checks
please accept apologies - post edited 23:19. Query below successfully groups for records having greater than 1 results while joining to a State table: State.StateAbbr | State.StateTerm posting table: JPosts2.JID | JPosts2.JState | JPosts2.Class runtime table: JRun.JRID | JRun.FK_JobID | JRun.StartRun | JRun.EndRun SELECT JPosts2.JID, JRun.StartRun, JRun.EndRun, JRun.JRID,StateTerm, COUNT(JState) FROM JPosts2 INNER JOIN State ON State.StateAbbr=JPosts2.JState INNER JOIN JRun ON JRun.FK_JobID =JPosts2.JobID AND `JClass` = 'Mechanics' GROUP StateTerm HAVING count(JState) > 1 ORDER BY JState ------------------------------------------ QUESTION: Can the query be coaxed to return results for the same JState where there are both: at least >0 expired and at least > 0 current date checks grouped by StateTerm for the given JClass? ...JRun.JRID =(SELECT max(JRun.JRID) from JRun WHERE JRun.FK_JobID = JID) AND (CURRENT_DATE () > DATE_ADD(JRun.EndRun,INTERVAL 18 DAY)) // expired ...JRun.JRID =(SELECT max(JRun.JRID) from JobRun WHERE JRun.FK_JobID = JID) AND JRun.StartRun BETWEEN DATE_SUB(JRun.EndRun,INTERVAL 18 DAY) AND CURRENT_DATE //current
View Replies !
Group By Specific Part Of Date
I have a field in a table with the date format. I was wondering if there is a way to group by a specific part of the date instead of the whole date? Meaning I would like to grab all records and group by year in the date column. How can I do that?
View Replies !
Count Fields Over Multiple Rows
I have a database like this id, field1,field2,field3,field4,field5 Database contains 100 rows, some rows have no fields filled, some 1field , some 2 fields etc. How would i count the number of fields filled in total? So the outcome is (number of fields filled in row1)+(number of fields filled in row2)+(number of fields filled in row3)....................+(number of fields filled in row100)
View Replies !
Group By Date Or Hour: Fill The Gaps
Example: SELECT DATE_FORMAT(date, '%e %M') AS day, date, SUM(amount) AS amount FROM log GROUP BY day ORDER BY date DESC This will output: 17 June 2008-06-17 00:32:47 4631.17 16 June 2008-06-16 08:46:44 17292.81 13 June 2008-06-13 09:13:29 3419 12 June 2008-06-12 09:24:17 11861.75 As you can see, 14th and 15th of June are missing since there is no data in the table for those days. However, I would like to see the following: 17 June 2008-06-17 00:32:47 4631.17 16 June 2008-06-16 08:46:44 17292.81 15 June 2008-06-15 07:11:22 0 14 June 2008-06-14 08:55:01 0 13 June 2008-06-13 09:13:29 3419 12 June 2008-06-12 09:24:17 11861.75
View Replies !
GROUP BY Date With Variable Is Unix Time
I am saving the Unix time every time a record is created in a field called "Time" and would now like to write a report to group on Date ... is there a in-built function to do this .. SELECT * FROM tblTemp GROUP BY [Date] ?
View Replies !
COUNT WHERE (COUNT > X OR ABOVE) And WHERE DATE -3 Days Before
Example of Date of request 1. SELECT Count(tbl_eventbooked.idEvent), Sum(tbl_eventmain.StartDateEvent) FROM tbl_eventmain Left Join tbl_eventbooked ON tbl_eventbooked.idEvent = tbl_eventmain.id_Event WHERE tbl_eventmain.StartDateEvent <= '-2' GROUP BY tbl_eventbooked.UserEmail, tbl_eventbooked.FirstName Example Count of request 2. SELECT Count(tbl_eventbooked.idEvent) FROM tbl_eventmain Left Join tbl_eventbooked ON tbl_eventbooked.idEvent = tbl_eventmain.id_Event WHERE Count(tbl_eventbooked.idEvent) >= '15' (but how do you tell it 50% of row X)
View Replies !
Date Of Birth Fields
I have a MySQL database field titled user_date_dob. This is of type INT(11). The entries that occur in this field are entries such as, 634194000, 791442000, 599634000, etc). Looks like they were entered into the database based off of this PHP code Code: $signup_date_dob = mktime(0, 0, 0, $signup_date_dob_month, $signup_date_dob_day, $signup_date_dob_year); all date fields in my database are of TYPE INT(11). I am not sure why they were not done using DATE or DATETIME TYPE. I am wanting to convert the data in the INT(11) fields to more appropriate (for my application) DATE nad DATETIME fields. I have already adjusted the PHP code to enter in DATETIME (using NOW() in my INSERT statement) and would like to know if there is any way to convert the data already in the database fields to the appropriate type. So, in short, can I convert an INT(11) field holding timestamp information to an acceptable DATETIME or DATE format? I am not beyond extracting the data via php code and converting it via php and inserting it back into the db,
View Replies !
Time And Date Fields
I have a form which insert data into a MYSQL table. In this MYSQL table I have a field called "DateEntered". Is it possible to have this field automatically updated with the current date when the form is submitted. What do I need to do in Mysql or on the html form?
View Replies !
Queries And Date Fields
My tables have a date field (in the DATE format), and I would like to select rows that are more recent than a certain date. I tried a select query using (for example): select * from table where date > 2005-05-16; But this returns all the rows, including dates prior to 2005-05-16. Is there any way to select using a date field?
View Replies !
Date Split In Three Fields
I have split the date field in three. Basically I have set the date as three int fields so that I will make it easier for the user to insert dates in the database. Because of this I have created another problem. I want to make a query in the database that is date related. I have used the following query SELECT * FROM main WHERE DATE (CONCAT(Year,Month,Day)) < '2007-10-10' However this is not returning any information back.
View Replies !
Altering Date Fields
I'm brand new to MySQL and have just migrated my Access database to MySQL. In Access I have two date fields, one for Date (DateLogged) and another for Time (TimeLogged). Bad idea, but I didn't know better at the time. Nw, in MySQL, the DateLogged field has the date information from Access and also the time as 00:00:00. The TimeLogged field has the time information with the date set as 1899-12-30. Is there a way in which I can join these fields, and just have one datetime field? Or is it impossible?
View Replies !
Modified Date Fields
I can't seem to find any documentation on the correct way to do this... did a number of google searches. I know I could just use a timestamp and then insert it but I'd rather base it on when the actual sql statement is used.
View Replies !
Indexing Date / Time Fields.
If we have a date/time field and are doing frequent queries WHERE {date/time field} BETWEEN TimeA AND TimeB Does it make sense, query speed wise, to create an index on the date/time field? The reason I ask is that TimeA and TimeB are significant down to seconds. My *assumption* is that with a large amount of data scatter very few records will contain duplicate date/time points. And that under these circumstances, indexing will be of little value since every date/time point will need to be examined in detail. Date/time fields are actually floating point numbers after all. In other words, the assumption I am making is that if most of the data in a field is unique, then indexing will yield little or no query performance benefit. Is my "assumption" valid? Would an index on the date/time field speed up this type of query?
View Replies !
Creating Date And Time Fields
What's the correct way of creating date and time fields. Do I set a default for these fields of NULL so I may query NULL values instead of '0000-00-00' values? What's the preferred way?
View Replies !
Grouping Fields And Getting Most Recent Date
I have a table like: table_id table_foreign table_datetime I'd like to select the highest datetime for each foreign. So far I've tried something like this... SELECT * FROM table GROUP BY table_foreign ORDER BY table_datetime DESC Unfortunately, the ordering only works on the grouped resultset, not that the values of the dates.
View Replies !
Load Data Date Fields
syntax to load date format data as below. table +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | | NULL | | | dte | date | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ data file aaa, 5-Jan-2009 aaa, 5-Feb-2009 what should i do to load this data into mysql table. i am using below command/syntax but its loading all in this format. LOAD DATA INFILE '/tmp/ld.txt' INTO TABLE test1 FIELDS TERMINATED BY ',' LINES TERMINATED BY ' '; (name,@dte) SET dte = (str_to_date(@dte, '%d-%m-%y'); MySql> select * from test1; +------+------------+ | name | dte | +------+------------+ | aaa | 0000-00-00 | | aaa | 0000-00-00 | I want date to be in 2008-01-05 format,
View Replies !
Drupal Profile Values - Count & Pivot Multiple Fields
availabe data in the form---> TABLE field_id, user_id,value 1,1,Manoj 2,1,Computer science and engineering 3,1,2008 1,2,Ajay 2,2,Information Technology 3,2,2007 QUERY ? have to convert this data into the form:---> RESULT b/d CSE ECE IT 2006 0 0 0 2007 2 0 10 2008 52 0 30
View Replies !
Query - Count With Date Intervals
I have this: select count(*) from visitors where visitordate >= '2008-01-01' AND visitordate >= '2008-01-08' Now this gives me a count of how many visitors that week visited my site but I want something more automatic. I do not know which functions to use to make this logic below happen: Start with year base (ie:2008) increment by week number (ie:upto 52) and determine how many visitors each week visited the site. I wish this worked: select count(*) from visitors where visitordate = week(1), then week(2), etc.
View Replies !
Count Back From Todays Date
I am producing an online booking system and the customer wishes to look back through booked jobs so yesterday, last 7 days, last month, last 6 month. I have managed to create the query for tomorrow, next seven days etc and thought it would be as simple as putting a minus sign in front of the required number of days but it doesnt work. Example of the query used SELECT booking_id, DATE_FORMAT(date, '%W %D %M, %Y') AS date, name, address, postcode, tel, alt_tel, email, make, model, colour, registration, job_details, parts, comments, installer FROM jobs WHERE DATEDIFF(date,CURDATE()) BETWEEN 0 AND -1
View Replies !
Is There A 'good Practice' Method For Date Fields?
What I mean is, with using php to insert and pull records from a date type field (called tsdate), should I insert as a unix timestamp or is there some advantage starting with 2006-06-06 and converting later? Just curious. I am dealing with millions of records, indexes are used, load time is good, but I want to (for the future) continually use one method for data records, that's all
View Replies !
Adding A Date Field, Need To Populate The Fields
I have a table and until now there is was date entry for the fields. I have added a timestamp (column "time") and from now on, every time I insert a new entry, the date/time will be recorded. The thing is, I want a date/time to exist for the old fields. I have about 250 fields and I want each field to be assigned a timestamp based on the fields alphabetical order (ordered by "title"). So, field with title "aaaaa title" will have a timestamp of today. A field with a title "zzzz title" will have a timestamp of 250 days ago. It doesn't have to be exactly like this. My main goal is to assign dates to my old fields. Anyone know how to do this?
View Replies !
MySQL Date Range Max Count Query
Code: ----------------------------------------------------- Idstartend ----------------------------------------------------- a2008-09-01 15:012008-09-01 15:04 b2008-09-01 15:022008-09-01 15:09 c2008-09-01 15:122008-09-01 15:15 d2008-09-01 16:112008-09-01 16:23 e2008-09-01 16:192008-09-01 16:25 f2008-09-01 17:522008-09-01 17:59 g2008-09-01 18:182008-09-01 18:22 It shows each time some one logs on, and logs off. The ID is not a userId, just a unique id for the record. I want to be able to query the data so i can create a graph showing concurrent visitors. Either at 10 min, Hourly, Daily or monthly summarys. For example: Code: 2008-09-01 16:10 - 16:202 2008-09-01 17:50 - 18:001 2008-09-01 18:10 - 18:201 The above shows me concurrent visitors within ten minute intervals. The query doesnt have to be that specific, it can just show changes. For example: Code: dateCount ------------------------------------------ 2008-09-01 15:011 2008-09-01 15:022 2008-09-01 15:041 2008-09-01 15:090 2008-09-01 15:121 2008-09-01 15:150 2008-09-01 16:111 2008-09-01 16:192 2008-09-01 16:231 2008-09-01 16:250 etc etc etc...
View Replies !
Multiple Date Ranges
I have a table in my database that contains information about shows that my client's company promotes/produces (tblEngagements). There is a second table called tblEngagementShows that contains the details of the actual shows - date/time, location, etc. These tables are able to be joined on engagementID. They would like to be able to run a report (PHP page) that will return the number of shows and the money that was made on the shows for the current year, last year and two years ago based on a date entered by the user. The user specifies the date range for the report (10/1/2008 - 10/31/2008 for example) and I programatically calculate the previous years (10/1/2007 - 10/31/2007 and 2006) using UNIX timestamps. If, for example, 3 engagements were added in October of 2008 and none in 2007 and 2006, when the user enters a date range of 10/1/2008-10/31/2008, they should see a table showing 3 shows in 2008, 0 shows in 2007 and 0 shows in 2006 (along with their estimated net values). The query should return a list of attractionIDs from the tblEngagements table. The data for -1 and -2 years should return the count and sum for any attractions that match the IDs in the main query that were added during the date range specified -1/-2 years (it should also return 0 if no data was entered). Ultimately, I believe that my results should be comprised of 7 columns: Attraction, current year count, current year sum, -1 count, -1 sum, -2 count and -2 sum. I'd like to avoid running three separate queries and put this all into one, but I'm running in to some trouble. My first question should probably be, is this the best (most efficient) way to be performing this operation? If so, what's the best way to do this?
View Replies !
Date Range :: Month, Year And Date In Separate Columns
I have a database where the date is stored in 3 different collums month, day, year. A am trying to retrieve data by a date range something like "1-1-2005" - "1-1-2007" or something but had quite some difficulty in doing it but I found a way and I am not sure that it is the best way but it appears to work well. Let me know what you think. "SELECT * FROM database " + "WHERE datey*365+DAYOFYEAR(CONCAT(datey, '-', datem, '-', dated)) " + "BETWEEN " + StartDate + " AND " + "EndDate + " " + "ORDER BY datey, datem, dated;" NOTE: StartDate and EndDate use the same formula of (year*365)+dayofyear
View Replies !
Date :: Select X Number Of Months Ahead Of Current Date
I am trying to query dates stored in my mysql database... eg: 2005-12-13 (13th Dec 2005) 2006-03-14 (14th Mar 2005) My SQL query needs to fetch all the records which are X number of months ahead of the current date. eg: I query it saying "get me all records where the date is 1 month from now.... I have used this (MONTH(date_review)-MONTH(CURDATE())) which returns 1 - so this is ok - as it's November, and december is one month away.... but when I ask for 3 months - it wont get the records... I know I somehow need to add the months on - but how do I do this with the year attached too?
View Replies !
Passing Date Via Form For Mysql Date Type
I've created a database for tracking our paper inventory. Basically when an individual takes paper, or envelopes the quantity is entered into the database, along with some other items. This all works great. I also have two fields that use the "Date" and "Time" types for holding the date and time of the initial transaction. I've created a seperate php script that we will use for our "end of month" reports. The script goes through and adds up the cost of each "purchase" between a specified time frame (1 may thru 31 may for example). This script works for me as long as I perform my query with my condition formated as such:
View Replies !
DATE COMPARISON :: Get Older Date Between Two Dates
I was wondering if there is a date function to get the older date between two dates?. Or smething that indicates me that one date is older than the other one.I know that I have the function YEAR,MONTH,DAY and I can use them, but I don't know if exists a function that can do the same.
View Replies !
|