I'm able to get the set of rows by ascending time order. The columns I have are: artist name, action date and city name. The query looks as follows:
SELECT T_Action.Artist,
T_ActionDates.ActionDate,
T_City.Name
FROM T_Action
INNER JOIN T_ActionDates
ON T_Action.ID = T_ActionDates.ActionID
INNER JOIN T_ActionPlace
ON T_Action.ActionPlaceID = T_ActionPlace.ID
INNER JOIN T_City
ON T_ActionPlace.CityID = T_City.ID
WHERE T_Action.ETicketEnabled = 1
AND T_ActionDates.ActionDate >= GETDATE()
ORDER BY
T_ActionDates.ActionDate
I should display this set of rows in ASP.NET repeater control, but they should be sorted by time and grouped by particular city separately. For example:
City1:
2013-01-01, Action1
2013-01-02, Action2
City2:
2013-02-01, Action1
2013-02-02, Action2
2013-02-03, Action3
And so on.
Can I do that in SQL directly or I should use the code behind to arrange the data? The preferred way is through SQL.
You will want to order your results by city then iterate over them yourself. So your ORDER BY
is likely ORDER BY T_City.Name, T_ActionDates.ActionDate
. Then you will need to intelligently iterate over them yourself to show your groupings.
SQL returns a single resultset per query with a set structure (i.e. it knows the columns ahead of time and calculates the row data). It won't do master-detail rows or anything like that. That's up to you.