Displays value of those part of the selected parameter - mysql

Sorry to bother you again, i have difficulties in formulating my query . i have two columns with a date values. (see sample below)
Posting Date col B
Feb 02,2013 feb 01, 2013
Feb 02, 2013
feb 15, 2013
mar 03,2013 mar 01, 2013
april 12, 2013 april 12, 2013
if my parameter is a range of date based on col B (ex. where colB between 02/01/2013 and 02/28/2013).
I want to show all value in Posting date which is part of the date range i had filtered. say, having month of Feb and 2013 as year
results:
Posting Date col B
Feb 02,2013 feb 01, 2013
Feb 02, 2013
feb 15, 2013

Is this what you're looking for? Is your field type of your Posting_Date column a Date? If so, then this should work:
SELECT Posting_Date, ColB
FROM YourTable
WHERE Posting_Date >= '2013-02-01'
AND Posting_Date < '2013-03-01'
I prefer using >= and < rather than BETWEEN.
If your Posting_Date field is stored as a varchar, then use STR_TO_DATE:
SELECT Posting_Date, ColB
FROM YourTable
WHERE STR_TO_DATE(Posting_Date , '%M %d,%Y') >= '2013-02-01'
AND STR_TO_DATE(Posting_Date , '%M %d,%Y') < '2013-03-01'
SQL Fiddle Demo

Related

SELECT last 13 months name inducing current Month

Experts ,
What is best way to get last 13 months name inducing current month. In the table I have records for every month from 2014.
Note : date format is - 2016-01-15 , each month has multiple records in table.
something like below
SELECT MONTHNAME(start_date) as month_name
FROM purchase_order_entry_reports
WHERE start_date (needs to select last 13 months )
Result i'm looking for
November,
December,
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November
SELECT distinct YEAR(start_date), MONTHNAME(start_date) as month_name
FROM purchase_order_entry_reports
WHERE start_date BETWEEN now() - interval 13 month
AND now()

Incremental counts in mysql

I have a table "UserLogins" and when the user login into the system I will drop a record in the "UserLogins" table.
Jan 10th (Users : 1,2,3,4) // four records with Ids 1,2,3,4
Jan 20th (1,2,3,4,5,6) // six records with Ids 1,2,3,4,5,6
Jan 30th (1,2)
Feb 10th (1,7)
Feb 20th (2,6,8)
Feb 25th (1,2,3,5)
Mar 10th (3,4)
Mar 20th (4,5,9)
Mar 30th (8,10,11)
Apr 10th (10,12)
Apr 20th (1,2,3,6,13, 14, 15)
Apr 30th (11,12,16)
When I write the group by my results as follows
Jan - 6
Feb - 7
Mar - 7
Apr - 11
But I need an out put like as follows
Upto Jan - 6 //count of distinct users upto Jan
Upto Feb - 8 //count of distinct users upto Feb
Upto Mar - 11 //count of distinct users upto Mar
Upto Apr - 16 //count of distinct users upto Apr
Your first count could simply be like this:
SELECT
DATE_FORMAT(login_date, '%Y-%b') AS year_month,
COUNT(DISTINCT user_id)
FROM
UserLogins
GROUP BY
DATE_FORMAT(login_date, '%Y-%b')
while to count all users up to a given mount, I would use a Join:
SELECT
DATE_FORMAT(last_day, '%Y-%b') AS year_month,
COUNT(DISTINCT user_id)
FROM
(SELECT DISTINCT LAST_DAY(login_date) as last_day
FROM UserLogins) d
INNER JOIN
UserLogins ON UserLogins.login_date<=last_day
GROUP BY
DATE_FORMAT(last_day, '%Y-%b')
on the subquery d I will return all last days of every month that has a record on the UserLogins table, then I will join all userlogin that logged up to the end of the month, and then I will do the count.

Get Mondays of the last two months from SQL Server 2008

If today is 10 JULY 2015 then what I need is a record set in SQL Server which has the following records
6 JULY 2015
29 JUNE 2015
22 JUNE 2015
15 JUNE 2015
8 JUNE 2015
1 JUNE 2015
25 MAY 2015
With the following expression you get the last Monday:
CAST(GETDATE()-(##DATEFIRST-1+DATEPART(weekday,GETDATE())) % 7+1 AS DATE)
Here we use ##DATEFIRST and DATEPART(weekday,...) to find the last MONDAY.
Then you should generate 8 (two months) rows sequence of days each of them -7 days prior the next. Here I've used CTE but you can use UNION or TOP 8 rows from existing (system or user) in your DB table to generate 8 rows.
;WITH CT(n) AS
(
SELECT 0
UNION ALL
SELECT n+1 FROM CT WHERE n < 7
)
SELECT DATEADD(DAY,-n*7,
CAST(GETDATE()-
(##DATEFIRST-1+DATEPART(weekday,GETDATE())) % 7+1 AS DATE)) as DT
FROM CT ORDER BY n
Sqlfiddle demo
Try this
;WITH dates AS
(
SELECT CONVERT(datetime,cast(month(getdate())-2 as varchar(2))+'/'+cast(day(getdate()) as varchar(2))+'/'+ cast(year(getdate()) as varchar(4))) as Date,' ' as eid
UNION ALL
SELECT DATEADD(d,1,[Date]),' ' as eid
FROM dates
WHERE DATE < GETDATE()
)
select datename(DD,dates.date)+' '+datename(MM,dates.date)+' '+ datename(YYYY,dates.date) from dates
where datename(dw,dates.date) = 'Monday'

MySQL order by IF and sorting dates

In sorting the ORDER BY of dates in MySQL, I'm trying to sort ASC for dates after today, and DESC for dates before today in one go.
I'm doing something similar to:
SELECT * FROM test ORDER BY IF(created >= NOW(), created, 1) ASC, created DESC
This gives me a somewhat desired result:
ID CREATED
8 May, 10 2014 11:15:00+0000
7 May, 03 2014 20:47:00+0000
5 May, 02 2014 14:00:00+0000
4 April, 30 2014 17:41:00+0000
3 April, 30 2014 17:00:00+0000
1 April, 21 2014 03:30:00+0000
6 March, 23 2014 12:00:00+0000
9 May, 20 2014 20:45:00+0000
10 July, 02 2014 20:30:00+0000
2 June, 30 2015 11:16:00+0000
Before today is DESC, after today is ASC. However, I want to see the block of ID's 9,10,2 at the top of the results.
Any thoughts on how to do this is appreciated.
Test Link: http://www.sqlfiddle.com/#!2/4667b/2/0
Add an additional condition to the order by, which is a binary indicator of whether the date is in the past or future:
SELECT *
FROM test
ORDER BY (created >= NOW()) desc,
IF(created >= NOW(), created, NULL) ASC,
created DESC;
how about splitting it up into 2 sql queries and then unioning the results?
(SELECT * FROM test WHERE created >= NOW() ORDER BY created ASC)
UNION
(SELECT * FROM test WHERE created < NOW() ORDER BY created DESC)

Table Date column comparison with a generated dates list - MYSQL

There seem to be a weird behaviour when comparing a date in table column with a date generated in a list within MYSQL.
Please take a look at the * SQLFIDDLE reference.
Payroll Table:
ID DATESTAMP
1 August, 30 2012 00:00:00+0000
2 September, 02 2012 00:00:00+0000
3 September, 15 2012 00:00:00+0000
4 September, 24 2012 00:00:00+0000
5 October, 05 2012 00:00:00+0000
6 October, 16 2012 00:00:00+0000
7 October, 19 2012 00:00:00+0000
8 November, 02 2012 00:00:00+0000
9 November, 10 2012 00:00:00+0000
10 November, 16 2012 00:00:00+0000
11 November, 24 2012 00:00:00+0000
12 November, 30 2012 00:00:00+0000
13 December, 01 2012 00:00:00+0000
14 December, 07 2012 00:00:00+0000
Dates list is generated between two particular dates with a constant day interval
Query:
set #i:= 0;
SELECT date_format(DATE(ADDDATE('2012-10-05',
INTERVAL #i:=#i+14 DAY)),'%Y-%m-%d')
AS dateP, #i
FROM payroll
HAVING #i < datediff(now(), date '2012-10-05')
;
DATEP #IntervalDays
2012-10-19 14
2012-11-02 28
2012-11-16 42
2012-11-30 56
2012-12-14 70
As you can see the generated dates list has matches to the Payroll table above. However when the comparison is done, it reutns zero records.
Comparison Query:
set #i:= 0;
SELECT distinct datestamp FROM payroll
WHERE date(datestamp) in (
SELECT DATE(ADDDATE('2012-10-05',
INTERVAL #i:=#i+14 DAY) ) AS dateP
FROM payroll
where #i < DATEDIFF(now(), date '2012-10-05')
)
;
So Questions I have:
Is the inner query stop generating dates when used as a nested query?
Is there anything wrong with the dates comparison method I am using here?
What could be the reason for this entire failure?
How to fix it within Select itself without any procedure/functions? :)
PS:
I am also trying to test this in SQL server as well as Oracle.
There are many good questions and answers to support 'Date Comparison' issues occurred at various scenarios with the site. That includes posts such as mysql date comparison with date_format. etc.. May be there's one hidden somewhere asking for exact issue I am facing with different wording. Couldn't find and hence posted the question.
Second UPDATE:
Now I got it working in every version:
select
*
from
Payroll
inner join
(
SELECT DATE(DATE_ADD('2012-10-05',
INTERVAL #i:=#i+14 DAY) ) AS dateP
FROM Payroll, (SELECT #i:=0) r
where #i < DATEDIFF(now(), date '2012-10-05')
) sq on Payroll.datestamp = sq.dateP
You just have to initialize the variable inside the query.
UPDATE:
Strange thing is, this one works on my local machine without problems (version 5.1.41-3ubuntu12.7-log), but not in your SQLfiddle.
set #i:= 0;
select
*
from
Payroll
inner join
(
SELECT DATE(DATE_ADD('2012-10-05',
INTERVAL #i:=#i+14 DAY) ) AS dateP
FROM Payroll
where #i < DATEDIFF(now(), date '2012-10-05')
) sq on Payroll.datestamp = sq.dateP
END OF UPDATE
Have you tried it like this?
set #i:= 0;
SELECT distinct datestamp FROM payroll
WHERE STR_TO_DATE(datestamp, '%M, %d %Y %H:%i:%f') in (
SELECT DATE(ADDDATE('2012-10-05',
INTERVAL #i:=#i+14 DAY) ) AS dateP
FROM payroll
where #i < DATEDIFF(now(), date '2012-10-05')
)
;
My guess is, that the DATE() function fails, cause you're varchar(is it?) date is not in ISO format. Therefore you have to use STR_TO_DATE() function.
For exact usage of STR_TO_DATE() read here and here. I'm not sure about the microsecond part.