How do I make a query run for current month and current day of that month?
For example :
Select
installed_date,
cust_no,
sum(Price) as daily_price
From
table1
Group By
installed_date,
cust_no
I want this query to always return current month's data so in this case from 1st Dec 2013 till 3rd Dec 2013.
Thanks
select installed_date,cust_no,sum(Price) as daily_price
from table1
where installed_date >= dateadd(month, datediff(month,0, current_timestamp), 0)
group by installed_date,cust_no
Click on this link
Check above link.
I have created a table with three column. Schema of same is added to this link.
Also wrote a query which will resolve your problem. Have a look. I am not mysql developer but tried to convert mssql query to mysql. If you want MSSQL query then revert.
Related
I am working in SQL Server 2012.
I am trying to collect all data for the current month (2015-07) and group them. When I run this query it selects only the current day.
SELECT
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST,
SUM(PALLETDAYS) AS PALLETDAYS,
COUNT(*) AS LOCATIONDAYS
FROM
[METRICS].[dbo].[DailyData]
WHERE
DATE = CONVERT(date, DATEADD(MM, 0, GETDATE()))
GROUP BY
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST, PALLETDAYS
Thanks in advance
Gerry
Try this:
SELECT
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST,
SUM(PALLETDAYS) AS PALLETDAYS,
COUNT(*) AS LOCATIONDAYS
FROM
[METRICS].[dbo].[DailyData]
WHERE
MONTH(DATE) = MONTH(SYSDATETIME())
GROUP BY
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST, PALLETDAYS
By using the MONTH() function, you get the month number - both of your column Date (which is a really horribly bad name for a column, since DATE is also a reserved keyword for a datatype in SQL Server 2012 - try to use something more meaningful than just Date!) and the current date (I prefer the SYSDATETIME() function over GETDATE())
I am using Hive (which is similar to SQL, but the syntax can be little different for the SQL users). I have looked at the other stackoverflow, but they seems to be in the SQL with different syntax.
I am trying to the get the first day of the month through this query. This one gives me today's day. For example, if today is 2015-04-30, then result would be 2015-04-01. Thanks!
select
cust_id,
FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd') as first_day_of_month_transaction
--DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) as first_day_of_month_transaction --SQL format. Not compatible in Hive.
from
customers;
Try this
date_format(current_date,'yyyy-MM-01')
To get the first day of the month, you can use:
date_add(<date>,
1 - day(<date>) )
Applied to your expression:
date_add(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'),
1 - day(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'))
)
But this will work for any column in the right format.
SELECT TRUNC(rpt.statement_date,'MM') will give you the first day of month.
You can use this query to calculate the First day of the current month
Select date_add(last_day(add_months(current_date, -1)),1);
Instead of current_date, you can use the date field name.
last_day => Gives the last day of current month
add_months with -1 => Gives previous month
date_add with 1 => Add one day
Another way - select concat(substring(current_date,1,7),'-01');
I have a table of data that goes quite some time back, but I only want to results in my query for the last 13 weeks. There is a date column in that table.
I can use
SELECT DATEADD(Week, -13, GETDATE())
to get the date of 13 weeks back as a separate query - but I am having trouble linking that into the initial select to only return me the last 13 weeks of data. I have used that query as the data should refresh every day to go back 13 weeks to that date.
Is there any way I can do that?
Thanks in advance
This should be what you are looking for:
SELECT *
FROM TABLE_NAME
WHERE date_field >
(SELECT DATEADD(Week, - 13, GETDATE()))
I'm a bit confused on what your issue is. You should be able to use the dateadd() in your where clause:
SELECT *
FROM TABLE
WHERE DATECOLUMNTOCOMPARE > DATEADD(WEEK,-13,GETDATE())
I need some advise and ideas on writing a query that returns a count of policies that are in-force, or active, per month over a 12 month period. I can easily build it for a given month (see simplified example) but was hoping to find a way to avoid using JOIN statements, something more efficient. There WHERE condition is the part I'm struggling with.
select 'January', count(*)
from policies
where effective_date < '1/1/2012'
and expiration_date > '2/1/2012'
Suggestions?
Use GROUP BY to aggregate policies based on month
SELECT month, count(*) FROM policies WHERE effective_date < '1/1/2012' AND expiration_date > '1/1/2013' GROUP BY month;
select
datename(m,p.effective_date) as mymonth,
count(*)
from policies p
where effective_date BETWEEN '1 jan 2012' AND '31 dec 2012'
and expiration_date BETWEEN '1 jan 2012' AND '31 dec 2012'
group by datename(m,p.effective_date)
Thanks to those who took the time and interest in my problem.
I came up with a solution that is probably as efficient as it can get. I wrote a sub-select statement that sets the date values that I need to report on (pseudo query:
select add_months(trunc(sysdate),"month",-12) from dual
union
select add_months(trunc(sysdate),"month",-11) ..
and so on), then added that as a stand-alone table to my primary query, which used those dates in the WHERE condition and the GROUP BY. Worked like a champ.
Again, my thanks to all who responded and helped me get some different thought processes going.
Hi how do I get the data by current week?
Select * from Transaction where transactionDate ....
In SQL Server based on week of year. Please see DATEPART for ##DATEFIRST etc. for example, this is all trades since Sunday in US/UK settigs:
WHERE DATEPART(week, transactionDate) = DATEPART(week, GETDATE())
Edit:
For Access, use this DatePart and use "ww" for the part of date you want.
In answer to the comment, "week" is not a variable; it's the bit of the date you want
So:
WHERE DatePart("ww", transactionDate) = DatePart("ww", GETDATE())
In Microsoft Access
Last n days:
SELECT *
FROM Transaction
WHERE transactionDate >=Date()-7
If you have indexes and this type of difference suits, it will be faster because it is sargable
This week by week difference:
SELECT *
FROM Transaction
WHERE DateDiff("w",[transactionDate],Date())=0
BTW It is considered bad practice to use *
DateDiff: http://office.microsoft.com/en-us/access/ha012288111033.aspx
Simple but portable:
SELECT *
FROM Transaction
WHERE transactionDate >= ?
AND transactionDate <= ?
Calculate the two parameters in your server-side code to whatever definition of 'week' you need.
In IBM DB2
SELECT *
FROM Transaction
WHERE transactionDate BETWEEN CURRENT TIMESTAMP - 7 days AND CURRENT TIMESTAMP;
In Access, if you want to run a query to find records that fall in the current week, use
SELECT *
FROM table
WHERE table.DateField Between (Date()-Weekday(Date())+1) And (Date()-Weekday(Date())+7);
That runs Sunday through Saturday. Use +2 and +6 instead if you want the workweek.
mySQL (standard date stamp)
SELECT *
FROM Transaction
WHERE WEEK(NOW())=WEEK(transactionDate);
mySQL (unix timestamp stamp)
SELECT *
FROM Transaction
WHERE WEEK(NOW())=WEEK(FROM_UNIXTIME(transactionDate));
Bit of a unoptimized query. Could be a more efficient way.
Note: This isn't a rolling 7 days. Just the current week of the year.
EDIT: Sorry I didn't see the ms-access tag. ignore all of this :|