Click here to Skip to main content
15,909,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,I am working on a project where i have to generate a latest status report of an application which is saved in database as AppNo according to the date.

The hint given to me is that i should first sort my database according to date for a particular AppNo and when the AppNo changes the data just data just above that denotes the latest status of that Application.
But I donot know how to sort it...so kindly help me...

Thank you..
Posted
Comments
[no name] 14-Jul-13 14:33pm    
Your hint is telling you to query your data using "ORDER BY".
Reza Alipour Fard 14-Jul-13 14:42pm    
Do you must save data in database as sorted data?
For Example if you have bellow records in DB
ID Name Date
-----------------------------
1 Test1 2012-01-01
2 Test2 2013-01-01
And now you want add a recod in 2012-06-30, you table value must be:

ID Name Date
-----------------------------
1 Test1 2012-01-01
2 NewTest 2012-06-30
3 Test2 2013-01-01

Your hint is it?

1 solution

Generally, to sort data you need to ues ORDER BY[^] clause.

Have a look at example:
SQL
--define temporary table
DECLARE @apps TABLE(AppNo INT, AppName VARCHAR(30), ModDate DATETIME)

--insert data
INSERT INTO @apps (AppNo, AppName, ModDate)
SELECT 1 AS AppNo, 'ApplicationNo1' AS ApName, '2010-05-11' AS ModDate
UNION ALL SELECT 2, 'ApplicationNo2', '2010-06-11'
UNION ALL SELECT 1, 'ApplicationNo1', '2010-09-01'
UNION ALL SELECT 3, 'ApplicationNo3', '2010-09-21'
UNION ALL SELECT 1, 'ApplicationNo1', '2011-03-15'
UNION ALL SELECT 2, 'ApplicationNo2', '2011-04-23'
UNION ALL SELECT 3, 'ApplicationNo3', '2012-06-13'
UNION ALL SELECT 2, 'ApplicationNo2', '2012-07-01'
UNION ALL SELECT 1, 'ApplicationNo1', '2012-10-18'
UNION ALL SELECT 1, 'ApplicationNo1', '2013-01-01'
UNION ALL SELECT 2, 'ApplicationNo2', '2013-03-01'

--get data sorted by AppNo and last ModDate
SELECT *
FROM @apps
ORDER BY AppNo, ModDate DESC

--get data sorted by AppNo and ModDate; the highest ModNo is for last ModDate
SELECT *, ROW_NUMBER() OVER(PARTITION BY AppNo ORDER BY ModDate DESC) AS ModNo 
FROM @apps


Results:
1. query:
AppNo AppName       ModDate                    ModNo
1     ApplicationNo1  2013-01-01 00:00:00.000
1     ApplicationNo1  2012-10-18 00:00:00.000
1     ApplicationNo1  2011-03-15 00:00:00.000
1     ApplicationNo1  2010-09-01 00:00:00.000
1     ApplicationNo1  2010-05-11 00:00:00.000
2     ApplicationNo2  2013-03-01 00:00:00.000
2     ApplicationNo2  2012-07-01 00:00:00.000
2     ApplicationNo2  2011-04-23 00:00:00.000
2     ApplicationNo2  2010-06-11 00:00:00.000
3     ApplicationNo3  2012-06-13 00:00:00.000
3     ApplicationNo3  2010-09-21 00:00:00.000

2. query:
AppNo   AppName         ModDate                    ModNo
1	ApplicationNo1	2013-01-01 00:00:00.000	   1
1	ApplicationNo1	2012-10-18 00:00:00.000	   2
1	ApplicationNo1	2011-03-15 00:00:00.000	   3
1	ApplicationNo1	2010-09-01 00:00:00.000	   4
1	ApplicationNo1	2010-05-11 00:00:00.000	   5
2	ApplicationNo2	2013-03-01 00:00:00.000	   1
2	ApplicationNo2	2012-07-01 00:00:00.000	   2
2	ApplicationNo2	2011-04-23 00:00:00.000	   3
2	ApplicationNo2	2010-06-11 00:00:00.000	   4
3	ApplicationNo3	2012-06-13 00:00:00.000	   1
3	ApplicationNo3	2010-09-21 00:00:00.000	   2
 
Share this answer
 
v3
Comments
Shivangi Rawat 16-Jul-13 14:08pm    
Sorry....and Thank You
Maciej Los

I tried it as follows but its giving an error that code near AppNo is incorrect.Kindly Help me..

SqlConnection s = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\grdproject.mdf;
Integrated Security=True;Connect Timeout=30;User Instance=True");


s.Open();

int f = 0;
string x = "select * from dataentry orderby AppNo,Edate desc";
SqlCommand s1 = new SqlCommand(x, s);
SqlDataReader s2 = s1.ExecuteReader();
while (s2.Read())
{
f = 1;
s.Close();
break;
}
if (f == 1)
{
s.Open();
SqlDataAdapter s11 = new SqlDataAdapter(x, s);
DataSet d = new DataSet();
s11.Fill(d);
yy.DataSource = d.Tables[0];
yy.DataBind();
s.Close();
}

}
Maciej Los 16-Jul-13 14:13pm    
Replace: select * from dataentry orderby AppNo,Edate desc with select * from dataentry order by AppNo, Edate desc
Shivangi Rawat 16-Jul-13 15:25pm    
Thank you very much it worked..
but If i want to select only the topmost data of every particular AppNo when sorted in descending order then what query i should execute?

Maciej Los 16-Jul-13 15:50pm    
I'm not sure what you mean "the topmost data of particular AppNo". Could you, please, elaborate it?
Shivangi Rawat 16-Jul-13 16:00pm    
eg:-
AppNo Name Workcenter Sent_Recvd Action Remarks Edate NatureOfClaim Modno
100 Jyoti Ahmedabad From WorkCenter Return after Correction corrections done 7/16/2013 11:51:44 AM 10-D 1
100 Jyoti Ahmedabad To WorkCenter RIO for Correction photo missing 7/16/2013 11:42:03 AM 10-D 2
100 Jyoti Ahmedabad 7/16/2013 11:39:05 AM 10-D 3
101 Divya Chennai From Finance Form8 done passed through finance 7/16/2013 12:00:00 AM 10-C(Withdrawl) 1
101 Divya Chennai 7/16/2013 12:00:00 AM 10-C(Withdrawl) 2
101 Divya Chennai To Finance For Form8 make form8 7/16/2013 12:00:00 AM 10-C(Withdrawl) 3
102 Shewta Dehradun 7/16/2013 12:31:00 PM 10-D


now,"when sorted for each AppNo the data that comes at the top will be the latest status of that application.So i want to retrieve only that latest data"....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900