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)
Related
Can someone tell me how to select the most recent date (with the least different) from now?
Currently I'm using something like this
SELECT MAX(DatumAangemaakt) AS RecenteSchema FROM Personeelsschema
I use the Max() function, but it's no good because if I have a date in the future (e.g. 2015-06-23) this date will be selected.
And what I need is to select today's date (if there any) or the most recent date from today, e.g:
2015-06-23
2015-06-01
2015-05-17 (this one will be selected)
Is there a way to do that?
Get all dates past the current date, order them and get the top record:
SELECT DatumAangemaakt AS RecenteSchema
FROM Personeelsschema
WHERE DATE(DatumAangemaakt) >= DATE(NOW())
ORDER BY DatumAangemaakt LIMIT 1
SQL Fiddle Demo
If you also want to include dates from the past in your proximity check, then you can use the following query:
SELECT DatumAangemaakt
FROM Personeelsschema
ORDER BY ABS(TIMESTAMPDIFF(DAY, DatumAangemaakt, DATE(NOW()))) LIMIT 1
SQL Fiddle Demo
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'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
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;
Let's assume I manage medical patient stays information system.
I want to get the patient count per day with the following minimal structure :
stay table has begin and end datetime columns
PHP gives me $first_day and $last_day limits
The following snippet is NOT what I want, since it only counts entries per day, and not present stays per day:
SELECT
DATE_FORMAT(`stay`.`begin`, '%Y-%m-%d') AS `date`,
COUNT(`stay`.`stay_id`) AS `total`
FROM `stay`
WHERE `stay`.`begin` <= '$first_day'
AND `stay`.`end` >= '$last_day'
GROUP BY `date`
ORDER BY `date`
Last but not least, I'm looking for a full SQL query.
It goes without saying that making one SQL query for each day would be totally trivial.
Use of temporary (dates ?) table is clearly an option.
As you mentioned using a temporary table of all dates in the range you want is one way to handle this. If you created a table of date called foo with all dates between $first_day and $last_day inclusive (see here).
Then you can write your query like:
SELECT f.date, count(s.stay_id)
FROM foo f
JOIN stay s ON s.begin <= f.date AND s.end >= date
GROUP BY f.date
ORDER BY f.date
A quick Google around leads me to this page: What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?
What I would suggest is that you either follow the advice in that question, or construct your own loop in PHP.