I have a table named table1. It has a column NT_T_SENTDATE. This column keeps dates. It has data since 2005. Therefore this table is so heavy.
I want to see the rows which date is from dec 2013 to dec 2014 in NT_T_SENTDATE column. I know there is datediff method, I have used this query for this:
select *
from notificationtransactions
where datediff(day, NT_D_SentDate, '2015-03-25') > 360
BUT it is also showing the records older than 2013.
Can someone please correct me? What should I add to the query so that I can see only records older than 1 year, but not older than 3 years?
Looking forward your response
POOJA
This Query will return only this year records
SELECT *
FROM notificationtransactions
WHERE NT_T_SENTDATE> DATEADD(YY, -1, GETDATE())
This Query will return only Previous year records
SELECT *
FROM notificationtransactions
WHERE NT_T_SENTDATE BETWEEN
DATEADD(YY, -2, GETDATE()) AND DATEADD(YY, -1, GETDATE())
How about this:
SELECT *
FROM dbo.NotificationTransactions
WHERE
NT_D_SentDate <= '20140325'
AND NT_D_SentDate >= '20130101'
It shows all records with a "sent date" between Jan 1, 2013 and one year ago.
Related
This year, August contains week #'s 31, 32, 33 & 34.
How can I make a query that gets all returns from each week in the month(8)?
Example:
select sum(a) as MyTot
from MyTable
where week numbers are included in month
Clear as mud?
I can get the return for each week because there is a field with the week number in it. I need to sum all the weeks that are in any given month.
Thanks,
You could create a calendar table, containing all kinds of data about dates - weekday, week number, day of year, day of month, month name, month number etc'. Then you can join your current table with the calendar table, filtering by the relevant month.
In fact, this might be a very useful thing to have, as pointed out by Aaron Bertrand in his Creating a date dimension or calendar table in SQL Server article.
Another option is to compute the first and last date of the month, get the week number of these dates, and use that to select the data from your table:
DECLARE #StartDate date = DATEADD(Month, DATEDIFF(Month, 0, GetDate()), 0)
DECLARE #EndDate date = DATEADD(Day, -1, DATEADD(Month, 1, #StartDate))
SELECT SUM(a) as MyTot
FROM MyTable
WHERE weekNumber >= DATEPART(WEEK, #StartDate)
AND weekNumber <= DATEPART(WEEK, #EndDate)
Please note, however, that the DATEPART(WEEK, #Date) return value depends on the value set by SET DATEFIRST. You might also want to look at ISO_WEEK.
I have this module where i am supposed to check the winners of bidding in the current month. be that October, November, December or whatever.
I run this query,
SELECT *
FROM auction_winners
WHERE MONTH('2015-10-16 00:00:00')
but it shows me everything, from all the months.
i believe even if this query works it will work for October only, i am looking for something that checks system current date month.
Just add your column with date:
SELECT * FROM auction_winners WHERE MONTH(column_name) = MONTH('2015-10-16 00:00:00')
or:
SELECT * FROM auction_winners WHERE MONTH(column_name) = MONTH(NOW())
I have a MySQL database with one table that contains a data field and a "period" field, in months - int.
The idea is that the date indicates a due date to begin a project inside my company. And the "period" the period of time it is suppose to take to finish it, in months.
I need to select rows that will impact a given year. So if I am generating a report for 2014, I need to select the rows such: date+period is inside 2014.
It will be easy to do it inside the program, but I am looking for a way to do it in the query - if possible.
So basically I just need a way to sum dates and ints in a query, where the int is the number of months.
Any thoughts?
It's easy to do date arithmetic in MySQL and other RDMS systems. You need all the records in which the start date is not after the year in question OR the end date is not before the year in question. That is this expression:
NOT(YEAR(start_date) > 2014 OR YEAR(start_date + INTERVAL period MONTH) < 2014)
This logically reduces to
YEAR(start_date) <= 2014 AND YEAR(start_date + INTERVAL period MONTH) >= 2014
So this query will do it.
SELECT whatever, whatever
FROM project
WHERE YEAR(start_date) <= 2014
AND YEAR(start_date + INTERVAL period MONTH) >= 2014
AND (whatever other selection criteria you have)
This will give all projects that were active during 2014, including those that started before 2014 and those that will still be in progress at the end of that year.
I'm not sure this is even possible without using PHP, but I'd love to try.
I have a database that looks like this (a bunch of other stuff, but this is all that is relevant:
Date_Day (is a range from 1 to 31 with no trailing 0)
Date_Month (is a range from January to December, not numerical)
Date_Year (is the year in 4 digit format, ex: 2005)
Total (number with 2 decimal places)
I know the way the dates are stored is awful, but this is the database I was given. If there is a query that I could use these columns to create an actual DATETIME column, I would happily do it, I just don't know what that query looks like.
I have this query that returns the Total sales amount for this day for all previous years:
SELECT
Date_Year, Date_Month, SUM(Total)
FROM
tablename
WHERE
Date_Year < YEAR(CURDATE())
AND
Date_Month = MONTHNAME(CURDATE())
AND
Date_Day = DAY(CURDATE())
GROUP BY
Date_Year, Date_Month
So if I run this today, I get the daily totals for October 4th for all previous years. The issue is that in sales, this isn't very helpful for comparing growth. What I really need is the daily totals for the 1st Friday in October for all previous years.
Is this possible without having to rely on PHP? If so, I would be very grateful for your help.
Thank you.
You might be looking for DAYOFWEEK()
Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday). These index values correspond to the ODBC standard.
mysql> SELECT DAYOFWEEK('2007-02-03');
> 7
SELECT
Date_Year, Date_Month, SUM(Total)
FROM
tablename
WHERE
Date_Year < YEAR(CURDATE())
AND
Date_Month = MONTHNAME(CURDATE())
AND
Date_Day = DAY(LAST_DAY(CURDATE()) - ((28 + WEEKDAY(LAST_DAY(CURDATE())) - 4)))
GROUP BY
Date_Year, Date_Month
maybe this will help
I have the following Query:
SELECT Artist_no, SUM(purchasedate BETWEEN '2012-07-01' AND '2012-07-31') /
SUM(purchasedate BETWEEN '2011-07-01' AND '2011-07-31')
FROM Sales
This successfully returns me the percentage of increases/decreases in sales within my table 'Sales' of CD's. However I wish to alter to code to instead list specific dates of July 2011 against July 2012, to instead do July of this year, against July of last year. I understand that running this new query now would obtain the same result, however i wish if you were to do this query in 100 years time, the query would test for July 2111 against July 2112.
I have attempted to do this myself however when I i run the code My answer is NULL. Which shouldn't be the case, the following new code is here (even though it is incorrect):
SELECT SUM(purchasedate = YEAR(CURDATE() AND MONTH(purchasedate) = 7))
/ SUM(purchasedate = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR) AND MONTH(purchasedate) = 7))
FROM Sales
Secondly, in the case if there is a new artist with a new CD, it would be dividing against 0, and wouldn't include it in the result, however July 2011 being 0 and July 2012 398 for example, obviously this should be included in my result.
Try comparing the formatted date string as below:
SELECT SUM(date_format(purchasedate, '%m-%Y') = CONCAT('07-',YEAR(CURDATE()))
/ SUM(date_format(purchasedate, '%m-%Y') =
CONCAT('07-',YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR))
FROM Sales