filter multiple rows in sql - mysql

I have table like
Company Year
--------------
AxisBank 2018
AxisBank 2019
AxisBank 2020
ICICIBank 2018
ICICIBank 2019
ICICIBank 2020
Now I want to filter only Axis Bank and year less than 2020. So that output should be like below
Company Year
--------------
AxisBank 2020
ICICIBank 2018
ICICIBank 2019
ICICIBank 2020
Can someone please help me with Query

You can use:
where company <> 'AxisBank' or year = 2020
You can also express this as:
where not (company = 'AxisBank' and year < 2020)

You can use full Query like below:
SELECT * FROM tableName
WHERE Company != 'AxisBank' OR (Company = 'AxisBank' AND Year >= 2020)
So it will filter out, Company AxisBank and Year is less than 2020.

Related

Implement where condition varchar field in MySQL

I have usages table which contains field values like below
id | page_open_time
1, 28 May 2019 08:39:11
2, 12 July 2019 18:39:11
3, 22 December 2019 11:39:11
4, 23 June 2019 12:39:11
Now I would like to get all records where page_open_time is greater than 1 july, 2019. For this, I wrote this query below , but it didnt help. Is there any way, I can perform this ?
SELECT * FROM `usages` where page_open_time > '2019-07-01'
Your dates are not in a valid MySQL DATETIME format, so you need to convert them before comparison, using STR_TO_DATE:
SELECT *
FROM usages
WHERE STR_TO_DATE(page_open_time, '%d %M %Y %H:%i:%s') > '2019-07-01'
Output
id page_open_time
2 12 July 2019 18:39:11
3 22 December 2019 11:39:11
Demo on dbfiddle

MySQL select multiple date range

I have a table with 2 dates: startDate and EndDate
I pass to my query 2 dates and I want to check if in this date range the offer are available.
I have an offer that is valid From 1 may 2019 to 30 may 2019.
When I search in offers, I want to show this only if the date range that I pass is included in the date range of the offer.
OFFER From 1 may 2019 to 30 may 2019
Value pass: 01 jan 2019 to 4 jan 2019 NO SHOW
Value pass: 28 april 2019 to 2 may 2019 NO SHOW
Value pass: 10 may 2019 to 12 may 2019 ONLY SHOW IN THIS CASE
Value pass: 29 may 2019 to 2 june 2019 NO SHOW
Value pass: 10 nov 2019 to 12 now 2019 NO SHOW
How can I do this?
This will work for MySQL.
date_from < '2019-10-05' AND date_to > '2019-10-12'

Order By before a Group By [duplicate]

This question already has answers here:
GROUP_CONCAT ORDER BY
(5 answers)
Closed 8 years ago.
I need to pull some data on a couple thousand records, but I'm having trouble sorting the results.
SELECT IdCode, GROUP_CONCAT(' ',CONCAT(MONTHNAME(ei.StartDatetime),' ',YEAR(ei.StartDatetime)))
FROM exclusion_import_data eid JOIN exclusion_import ei ON ei.ImportId = eid.ImportId
WHERE ( IdCode IN ( '84277', '253533' ))
GROUP BY IdCode
ORDER BY RecordId ASC;
Basically I'm trying to see which months specific records appeared in our system.
253533 May 2013, November 2013, December 2013, January 2014, February
2014, March 2014, April 2014, May 2014, June 2014, August 2014,
October 2013, September 2013, June 2013, July 2013, May 2013, August
2013 84277 September 2013, April 2014, May 2013, May 2014, June 2014,
May 2013, March 2014, June 2013, February 2014, January 2014, July
2013, December 2013, November 2013, August 2013, October 2013, August
2014
The above query returns the correct months, separated by the idCodes, but the months are all out of order.
How can I sort the records before grouping them?
Use the order by option in the group_concat():
SELECT IdCode,
GROUP_CONCAT(' ', CONCAT(MONTHNAME(ei.StartDatetime),' ',YEAR(ei.StartDatetime))
ORDER BY ei.StartDatetime)
FROM exclusion_import_data eid JOIN
exclusion_import ei
ON ei.ImportId = eid.ImportId
WHERE ( IdCode IN ( '84277', '253533' ))
GROUP BY IdCode
ORDER BY RecordId ASC;
You can specify an ordering within the GROUP_CONCAT function, for example:
GROUP_CONCAT(DATE_FORMAT(ei.StartDatetime,'%M %Y') ORDER BY ei.StartDatetime)
^^^^^^^^^^^^^^^^^^^^^^^^^
(My preference would be to get the month and year with a single function call, and one reference to the column, rather than three function calls and two references to the same column. I've demonstrated that above as well.)
Ref: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

Sql Server Query not getting desire Result

I want show record month wise and date wise. My query is this but I am not getting how I can achieve this.
Query is
select
iWorkItemId
, convert(varchar(20),Progress.dtProgressDate,106) as dtProgressDate
, vsDescription
, WPD.rWeightage
, Progress.ProgressPerc as ProgressPerc
, Convert(decimal(6,2)
, ((WPD.rWeightage * Progress.ProgressPerc)/100)) as TotalProgress
from tblPkgWorkItems WPD
join tblPkgWorkItemsProgress Progress on WPD.iWorkItemId = Progress.iWorkItemsId
where iPackage='7'
group by iWorkItemId, dtProgressDate,vsDescription,rWeightage,ProgressPerc,progressmonth
order by progressmonth ASC,dtProgressDate ASC , iWorkItemId ASC
Column name : (1) progressmonth (Store Month Name)
(2) dtProgressDate (Store complete date)
Example :
In our table we are storing the records like in this manner:
30 Apr 2013
31 Aug 2013
28 Feb 2013
30 Jan 2013
Then I want to show records like this
30 Jan 2014
if some record is inserted in 15 jan 2014 then After 30 jan 2014 then 15 jan 2014 record will show.
28 Feb 2014
30 Apr 2014
30 Aug 2014
Means firstly Jan records will come then Feb record then March records.
Means all records will come month by order then sort by date wise .
My data is being returned like this:
iWorkItemId dtProgressDate progressmonth rWeightage
320 30 Apr 2014 Apr 2
321 20 Apr 2014 Apr 3
320 10 Feb 2014 Feb 4
321 9 Jan 2014 Jan 7
I want record should come first 9 jan 2014 then 10 Feb 2014 (Means Order by Month) then after it 30 April 2014, then 20 april 2014 (Then Sot by Date).
iWorkItemId will repeat win every month.
For my problem is I want to show
Firstly Jan records then Feb records , then March records
And whenever Jan month is coming then All Date which is belong to jan month that will come under JAN month.
Firstly Higher date of JAN month (Means ) 31 Jan 2014, 30 jan 2014, 29 jan 2014, 28 feb 2014, then 27 feb 2014.
Means first records fetch month wise then on particular month that records sort by datewise

MySql Query- Date Range within a Date Range

I use mySql 5 and IIS.
I have products, that have a start date field and an end date field.
I need to run a query that will take user entered Start and End dates, and output the number of days that the product ran within the date range.
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru Sept 15 2011
Results = 31
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru July 15 2011
Results = 15
If your products have a start_date and an end_date and your query has a qstart_date and a qend_date, then we want the number of days between:
GREATEST(start_date, qstart_date)
and
LEAST(end_date,qend_date)
. In MySQL I think this looks like
1 + DATEDIFF ( 'd' , GREATEST(start_date, qstart_date) , LEAST(end_date,qend_date) )
And you'll want to ignore negative numbers, replacing them with "0".