MySQL-how to make query for selecting min date and second min date with three other fields [duplicate]

MySQL-how to make query for selecting min date and second min date with three other fields [duplicate]



This question already has an answer here:



I have a table with the following fields


+-------+------------+--------+------+
| Icode | PurDate | Qty | Rate |
+-------+------------+--------+------+
| 1 | 2018-08-19 | 10.000 | 5.35 |
| 2 | 2018-08-19 | 10.000 | 4.00 |
| 3 | 2018-08-19 | 15.000 | 4.30 |
| 1 | 2018-12-15 | 15.000 | 5.00 |
| 2 | 2018-12-15 | 12.000 | 5.00 |
| 1 | 2018-12-14 | 5.000 | 8.00 |
| 3 | 2018-12-14 | 7.000 | 6.00 |
+-------+------------+--------+------+



I want to select data from above table like


Select Min(Purdate) AS Date1, Qty, Rate From TableName Where Icode='1'



Please Help me



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





what will be your output from this data
– Zaynul Abadin Tuhin
Aug 19 at 11:56





i want to in the following format:
– Ram Niwas
Aug 19 at 11:59





1, 2018/12/14, 5.000, 8.00 1, 2018/12/15, 15.000, 5.00
– Ram Niwas
Aug 19 at 12:03





Your SQL is unvalid your can't mix non aggregate columnms with a aggregate column.. What is the expected output?
– Raymond Nijland
Aug 19 at 12:03






then i make any stored procedure for this output in MySQl
– Ram Niwas
Aug 19 at 12:07




3 Answers
3



Use a sub-select to locate the minimum date, then join to the to get the row matching that date.


SELECT
a.`Purdate` AS a.`Date1`, a.`Qty`, a.`Rate`
FROM `TableName` a
JOIN (SELECT MIN(`PurDate`) as `minDate`
FROM `TableName`
WHERE `Icode` = '1') b
ON b.`minDate` = a.`PurDate`
WHERE a.`Icode` = '1'





@RamNiwas Because this query does not take ties in account.. you can get multiple records out off this query.. When PurDate is the same within the icode "group"
– Raymond Nijland
Aug 19 at 12:19




Since you don't need grouping you can just order by the date column and get the first two rows.


SELECT PurDate, Qty, Rate FROM TableName WHERE Icode = '1' ORDER BY PurDate LIMIT 2





Your query is working but it not showing result as per requirement. I modified this as Select Top 1 Purdate, Qty, Rate From table name where icode=1 Order by Purdate DESC but it return record of 2018-12-15 instead of 2018-12-14 please help me
– Ram Niwas
2 days ago





Is it MySql or Microsoft Sql server? Also do you want a result with 6 colums or 2 rows x 3 cols?
– dereli
2 days ago



use corelated sub query and union


select A.* from
(
select * from tablename t1 #1st min date will return
where t1.purdate in
(select min(purdate) from
tablename t2 where t2.icode=t1.icode

union


select t1.* from tablename t1 inner join
(SELECT
Icode
, Purdate
FROM (
SELECT
@row_num :=IF(@prev_value=Icode,@row_num+1,1) AS rn
, mp.Icode
, mp.Purdate
, @prev_value := Icode
FROM tablename mp
CROSS JOIN (SELECT @row_num :=1, @prev_value :='') vars
ORDER BY
mp.Icode
, mp.Purdate DESC
) d
WHERE rn = 2
) t2
on t1.Icode=t2.Icode and t1.Purdate=t2.Purdate
) as A where A.Icode in (......)





@ram niwas you can check my query 1st query will return min date and 2nd query will return min date then union both
– Zaynul Abadin Tuhin
Aug 19 at 12:24





its showing LIMIT error
– Ram Niwas
Aug 19 at 12:51





not used limit that i have changed
– Zaynul Abadin Tuhin
Aug 19 at 12:58






hello down voter any reason for down vote ?
– Zaynul Abadin Tuhin
Aug 19 at 12:59





@RamNiwas does not it helps ?
– Zaynul Abadin Tuhin
Aug 19 at 19:40


Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj