I'm trying to avoid involving php after this query. I have a table that holds a list of employees and how much time they worked in seconds,date, etc... i want to:
select SUM(`seconds`) between date A and date B group by WEEK(`date`)
that will give me results for each week but now i want to get an average seconds worked per week by using AVG() on the whole result set. How could you accomplish this in one query?
You can use something like this
select sum(total) from (select SUM(`seconds`) as total between date A and date B group by WEEK(`date`)) as tbl1
Hope it help
This will do the trick:
Select AVG(sum_seconds) from (select SUM('seconds') as sum_seconds between date A and date B group by WEEK('date')) as a
Related
hello everyone for my erp system i want some specific type of data fetch from database
for example if i want number of row added in current month purchase then i use
select count(receive_no) FROM tbl where contact_id=".$contactid." AND MONTH(created_on) = MONTH(CURDATE());
but it gets false results
and if i want sum of result in current month then i use
select sum(price) FROM tbl where contact_id=".$contactid." AND MONTH(created_on) = MONTH(CURDATE())
and if i want today date number of row or purchase then i use
select count(receive_no) FROM tbl where contact_id=".$contactid." AND created_on >= CURDATE()
so as if i want today sum of purchase then i use
select sum(price) FROM tbl where contact_id=".$contactid." AND created_on >= CURDATE()
my table created on field datetime like 2016-09-01 11:56:45
so please suggest me better query to fetch exact data
thanks
It's not really clear what - if any - the problem is with the last 3 queries, but the first one will get you the data from this month this year and this month last year, the year before that, etc. etc.
So what you need is something like (there are several ways to write that...):
select count(receive_no) FROM tbl where
contact_id=".$contactid."
AND MONTH(created_on) = MONTH(CURDATE())
AND YEAR(created_on) = YEAR(CURDATE())
For example I have two columns in my DB:
Date1 and Date2. I put into my input fields (field1 = 12/11/14 and field2 = 22/11/14). So I want to take all rows into my table who have lower date than 12/11/14 and higher than 22/11/14. How should my query look?
I will make my question more clear.
Here is my DB:
So I put into rent_date a client who will rent a car from 11/12/14 and will return it at 11/22/14. There are 5 clients and only one will rent a car between exactly 11/12/14 and 11/22/14 so I want to select the other 4 clients who doesn't rent car between this two dates. I hope I was clear. :)
I believe you can just use the BETWEEN operator to check that the rent_date is not between your intervals, like this:
SELECT *
FROM myTable
WHERE rent_date NOT BETWEEN '2014-11-12' AND '2014-11-22'
If you are looking to find out the records whose rent and return dates are not in the given range, you can use a query by comparing the dateS as follows:
SELECT * FROM DB WHERE rent_date < '2014-11-12 00:00:00' OR return_date > '2014-11-22 23:59:59'
You need to use a range query.
SELECT foo FROM bar WHERE timestamp BETWEEN timestamp1 AND timestamp2
I'm using mysql and I'm having trouble thinking of a query to count the number of users/visitors for a certain date range. The way that I'm currently doing it is using php, I select the date range and process the data in a for loop and then just count them there. It's actually pretty easy, but the problem is that this method does not work for bigger data of a few million rows. The alternative is to count the distinct values using mysql only and just return a count and not actual data by utilizing the index on the timestamp column. Also, converting the column to a datetime is not an option. Any ideas how I can achieve this?
Here's a sample result set of what I need:
date | count
5-01-13 14
5-02-13 44
5-03-13 23
5-04-13 13
My problem is that I don't know how to group the timestamp column by day.
That should do the trick:
SELECT DATE('datetimecolumn'), COUNT(*)
FROM Table
GROUP BY DATE('datetimecolumn')
You just have to do the same, but instead add a group by clause:
SELECT myDate, count(distinct myField) as cnt
FROM myTable
WHERE myDate BETWEEN ? and ?
GROUP by myDate;
Where the "?" are the dates you use in your original query.
I have this little script that shows one wisdom each day.
so I have three columns.
Id wisdom timestamp
1 wisdon 1 4/1/2012
2 wisdon 2 4/1/2012
3 wisdon 3 4/2/2012
and I want to fetch array of one wisdom for each day
I looked around your website, but unfortunately I didn't find something similar to what I want.
also I got this code
$sql = mysql_query("SELECT DISTINCT id FROM day_table group by timestamp");
but this also not working.
any ideas?
is it possible to make a counter of 24 hours update wisdom date?
please give me some help.
You can make another table that is called wisdom_of_day
The table would have the following columns, id, wisdom_id, date
Basically each day you can randomly select a wisdom from your wisdom table and insert it into the wisdom day table. You can also add a constraint to your date column so it is distinct. It is important that it is a date column and not a timestamp since you don't care about time.
Then you can retrieve the wisdom of the day by querying based on the date.
It's possible I read your question wrong and you just want to select one wisdom for each day, but you want to show multiple days and you want to get the data from your table.
If so, the reason your query is not working is because you are grouping by a timestamp which includes the date and time. You need to group it by date for it to group like you want.
Here is a query that will group by the day correctly. This will only work if you have a timestamp field and are not storing a unix timstamp on an int column.
select id, wisdom, date(timestamp) date_only from day_table group by date_only order by date_only asc;
Hmm, I noticed that your timestamp values are in some kind of date format, maybe as a string? If so the above query probably won't work.
First compute number of days since 1970
SELECT DATEDIFF(CURDATE(), '1970-01-01')
Then insert this number inside RAND, for example:
SELECT * FROM table ORDER BY RAND(15767) LIMIT 1;
Rand with number as argument is deterministic.
Full query:
SELECT * FROM table ORDER BY RAND((SELECT DATEDIFF(CURDATE(), '1970-01-01'))) LIMIT 1;
I am a complete novice at SQL Server and naively thought that there would be a QUARTER() function, alas there is not and some googling didn't come up with anything useful!
Basically what I want to achieve is that for all rows in my database I want a count of those rows grouped by Quarter.
If possible I would like to keep all calculation with the query but if not it is PHP that is kicking everything off.
This should do the trick, provided that you have a datetime column in the row of course :)
SELECT datepart(qq, my_date_column) as quarter, count(*) as rows
from my_table
group by datepart(qq, my_date_column)
Of course, if you have more than one years data, you might want to add datepart(yyyy, my_date_column) as well
I use
select Datename(quarter,MYDATECOLUMN_NAME)
MYDATECOLUMN_NAME is date format.
I simply used the Month Function on the date and then used a Case function to organize them:
Case When Month(date) in (1,2,3) then '1stQtr'
When Month(date) in (4,5,6) then '2ndQtr'
When Month(Date) in (7,8,9) then "3rdQtr'
When Month(Date) in (10,11,12) then '4thQty' END AS Quarter
select
CONVERT(DATETIME, CONVERT(CHAR(8),
DATEPART(YEAR, yourDateColumn) * 10000 +
( DATEPART(QUARTER, #date) * 3 - 2) * 100 +
1),
112) from...
taken from here:
http://geertverhoeven.blogspot.com/2007/01/get-quarter-of-given-date-in-datetime.html
Or you could the SQL mentioned in the comment in that link, to get the first day of the quarter:
DATEADD(qq,DATEDIFF(qq,0,#date),0)