I'm trying to find the cumulative sum of sessions of a link for its first 3 days. I tried this but it doesn't seem to take the date clause into account:
select
date,
link,
sum(sessions) as sessions
from ga
where date <= date+interval 3 day
group by link
But if I manually enter a date, it seems to work. Why is it not seeing date+interval 3 day as a proper date...?
Any help would be greatly appreciated! :)
Date is a column, not a value, you need to provide a specific date entry. Also "between" is a better keyword to use in this situation.
You need to also add date column in GROUP BY clause. Also, avoid using column name as date. It will create confusion.
Try below query :
select date_column,
link,
sum(sessions) as sessions
from ga
where date_column BETWEEN CURDATE()-3 AND CURDATE()
group by link, date_column
Related
My problem is that i want to count of id's between two dates range. But my written query is not give me required answer. sample is below .
select COUNT(hall_id) as countids
from comm_hall_form
where hall_id ='13' and ((date(booked_from)
between '2019-11-08' and '2019-11-09') or (date(booked_to)
between '2019-11-08' and '2019-11-09'))
You need to check if the booked time fully or partially covers the period you are asking for. Right now you are only checking if it starts or ends with that period.
SELECT COUNT(id) as countids
FROM comm_hall_form
WHERE hall_id ='13'
(DATE(booked_from) <= '2019-11-09' AND DATE(booked_to) >= '2019-11-09')
OR
(DATE(booked_from) <= '2019-11-08' AND DATE(booked_to) >= '2019-11-08')
I have attendance data for employees stored in the table attendance with the following column names:
emp_id (employee ID)
date
type (leave, absent, etc.)
(there are others but I'm omitting them for the sake of simplicity)
My objective is to retrieve all dates of the given month on which the employee was on leave (type = 'Leave') and the last leave taken in the last month, if any.
It's easy to do it using two queries (I'm using PHP to get process the data), but is there any way this can be done in a single query?
I'm answering my own question so as to close it. As #bpgergo pointed out in the comments, UNION will do the trick here.
SELECT * FROM table_name
WHERE type="Leave" AND
date <= (CURRENT_DATE() - 30)
Select the fields, etc you want then se a combined where clause using mysql's CURRENT_DATE() function. I subtracted 30 for 30 days in a month.
If date is a date column, this will return everyone who left 1 month or longer ago.
Edit:
If you want a specific date, change the 2nd month like this:
date <= (date_number - 30)
I have this problem if anyone can help.
There is a field (date) in my table (table1) that is a date in the format 3/31/1988 (M/D/y), and my necessity is to define how many days have passed since that date.
I have tried to give this instruction
SELECT DATEDIFF(CURDATE(), date) AS days
FROM table1
But it gives back 'null' and I think this happens because the two date formats are different (CURDATE() is YMD.....
Is it correct? can anyone help me?
Thank you in advance
You can use STR_TO_DATE():
SELECT DATEDIFF(CURDATE(),STR_TO_DATE(date, '%m/%d/%Y')) AS days
FROM table1
SQLFiddle Demo
Your DATE field should have DATE or DATETIME format to be used as DATEDIFF argument correctly.
Also DATE is MySQL keyword and I am not sure that you can use it as valid field name.
You can use this for accurate result
SELECT DATEDIFF(CURDATE(), DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(`date`)), '%Y-%m-%d')) AS days FROM `table1`
If you want to consider results without - signs that you have to follow parameters position as below :
SELECT DATEDIFF(Big_Date,Small_Date) AS days FROM table1.
positive results e.g 5 (with no sign), if you place a Small date as the first parameter then it will results minus sign e.g -5.
In order to calculate projected sales for a given day, I need to query the last six weeks of data for a given day. For example, if I want projected sales for Friday, I need to query data from the last six Fridays only.
I'm assuming there is a way to do this within a query, just not sure exactly how. Any help or insight would be greatly appreciated, as always.
Thanks in advance.
The easiest way is to use a limit.
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=6
ORDER BY date DESC LIMIT 6;
EDIT: To get this relative to today, just add CURDATE()
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=DAYOFWEEK(CURDATE())
ORDER BY date DESC LIMIT 6;
You can use a combination of different MySQL date and time functions to achieve this. Your query could look something like this:
SELECT fields FROM table WHERE DAYOFWEEK(table.date) = DAYOFWEEK(CURDATE()) ORDER BY table.date DESC LIMIT 6
Of course you can replace CURDATE() with the date that you are trying to predict.
Select * From table Where date_field > DATE_ADD(now(),INTERVAL -42 DAY)
That's about what you will need to do.
I just seen you wanted to query only a particular day of each week. Give me a moment and I'll update this.
Nevermind, I'm not going to edit this. Bobby has your answer for you. You just need to place variables in there through your script as needed. +1 Bobby.
I have a set of Access d/b's grouped already by year. within a given year, I have a field caleld REPORTDATE which is a standard mm/dd/yyyy field. However, I need to produce queries that return data by the month. For example, I just want to see records for Jan, recs for Feb, Recs for March, etc., so that I can sum them and work wwith thm.
Do I use an expression in the query design view Criteria field?
Thanks in advance.
I just want to see records for Jan, recs for Feb, Recs for March, etc., so that I can sum them and work wwith thm.
You can do all of that in one sql statement:
select month(reportdate), sum( the column you wish to sum )
from tablename
group by month(reportdate);
BUT WAIT THERE'S MORE!
Further say that there are several salepersons selling stuff, and you wish to show each salesperson's sales by month
select month(reportdate), salesperson, sum( the column you wish to sum )
from tablename
group by month(reportdate), salesperson;
That shows the sum per month per salesperson.
You know the Germans always make good stuff!
What it you wanted to see the same sums, but rtaher than comparing salespeople against each other in each month, you wanted to compare, for each salesperson, how they did from one month to another?
Just reverse the order of the group by:
select month(reportdate), saleperson, sum( the column you wish to sum )
from tablename
group by salesperson, month(reportdate);
Tacos, Fettuccini, Linguini, Martini, Bikini, you're gonna love my nuts!
The power of SQL! As seen on TV! Order now!
"select month(reportdate), sum( the column you wish to sum )from tablenamegroup by month(reportdate);" THIS IS VERY HELPFUL, THANK YOU. AND YOU ARE HILARIOUS. HOWEVER, can you clarify for me where the heck this code goes?! In the expresison Builder or what? Thank you SO much. – rick (19 mins ago)
In Access, I think from the graphical Query Builder thing's menu, select edit|SQL, and just type. And never go back to graphical!
You're a hard-charging forward-thinking entrepreneurially-minded man on the move! This is not your father's Oldsmobile! You wouldn't use an on-screen keyboard to type a document, dragging and dropping letters on the page, would you?! So why do that to build a SQL Query? Get into SQL! AS SEEN ON TV! All the cool kids and hep cats are doin' it! Order NOW!
You can use format, for example:
Format([REPORTDATE],"mmm yy")
Or Month:
SELECT * FROM Table WHERE Month([REPORTDATE]) = 10
An outline of query that may suit, paste this into the SQL view of
the query design window, changing table to the name of your table:
SELECT Format([REPORTDATE],"yyyy mm"), Count([ReportDate])
FROM Table
GROUP BY Format([REPORTDATE],"yyyy mm")
I wouldn't do this in the report's recordsource. I'd make the recordsource a regular SELECT statement and use the report's sorting/grouping. If you group on a date field (one that is really date type), you get the choice to GROUP ON:
Each Value (default)
Year
Qtr
Month
Week
Day
Hour
Minute
I think this is faster than a GROUP BY on a function, but someone who was interested should actually try it.
Certainly if your SELECT with GROUP BY has no WHERE clause, it's going to be a lot more efficient if you run the report with filtered values.