Monday, March 12, 2012

GROUP BY without Aggregation -- Is it possible ?

Here is what I want to do.

I have a database with HotelInfo

hotelid
hotelname
hotelstate
hotelcity
hotelphone etc etc

I want to display all hotels I have grouped by state

SELECT hotelname, hotelcity, hotelphone
FROM HotelInfo
GROUP BY hotelstate

So basically I was For eg.

TX
hotel1 Dallas xxx-xxx-xxxx
hotel4 Plano xxx-xxx-xxxx

CA
hotel2 San Fransisco xxx-xxx-xxxx

How can I group by without Aggregation ?Why wouldn't you just do an order by instead of group by? And then let your application do the formatting.

SELECT hotelname, hotelcity, hotelphone
FROM HotelInfo
ORDER BY hotelstate|||humm... sounds good.. I thought group by will be better, so I was never thinking in direction of order by.

When I display result on my page here is how I want:

AK
------
------

CA
-----
-----

TX
------
-----
------

I guess I can leave with Orderby also......|||Well you should be able to do this with a datarepeater or datalist. Check out those controls, which should have a group header band.

http://www.codeproject.com/useritems/GridGroupFormat.asp shows a way to do this in a datagrid. http://www.datawebcontrols.com/faqs/DataLists/GroupingByCategory.print.shtml shows how to do this using a datalist. HTH|||Tell me one more thing from design point of view

Is it better to have a seperate look up table "States" with stateID, stateCode and StateName

for eg.
1 AR Arkansa
2 AZ Arizona
43 TX Texas
etc ...........

and then in Table "HotelInfo" where it is "hotelState" put the stateID ? i.e. 1 OR 2 OR 43

I have 2 other table which also needs state info.

Is it good idea OR it is better to put StateName in HotelInfo (AR OR AZ) ?|||Is it possible to have set of range specified in SQL

SELECT xx, xxx , xxxxx
FROM XYZ
WHERE ID BETWEEN 1 and 35 AND
ID BETWEEN 37 AND 50

???

If I write like this I get error ...

No comments:

Post a Comment