Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2005-03-23 : 10:58:56
|
I have a table looks likeItem Pricea 5a 6a 6a 4b 7b 7b 8b 8c 1c 2c 3d 2d 2d 2d 1 Here is the result I would like to get:Result Pricea 6b 7c 1d 2 I would like to get the price that appears most times per each iteam.For example, price 6 is selected for item A because price 6 is shown twice whereas 5 and 4 are shown only one time.However, if the price are shown same times, I would like to select the cheapest price. For example,because both price 7 and 8 for item B are shown twice, I select the price 7 for item B.and If the prices are shown equally, I would like to select ,also, the cheapest price. For example, price 1 , 2, and 3 for item C are shown one time so that I select 1 as the price for item C.Resulta 6b 7c 1d 2I would like to get the result using one select statement. How can I do this? |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2005-03-23 : 11:16:51
|
| SQL does not have a built in mode function, so you would have to figure out the modal average for each one with a GROUPed COUNT and then use that as probably a joined query to figure the mode for each value.-------Moo. :) |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2005-03-23 : 11:53:34
|
| Can you show me some example? |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-03-23 : 12:04:30
|
I just did something like this...cut and paste the code into QA to see it runUSE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99([ID] int, VisitType varchar(15), [Date] datetime)GOINSERT INTO myTable99([ID], VisitType, [Date])SELECT 1, 'Home', '2/5/2005' UNION ALLSELECT 2, 'Office', '2/5/2005' UNION ALLSELECT 1, 'Office', '2/5/2005' UNION ALLSELECT 3, 'Phone', '2/5/2005' UNION ALLSELECT 1, 'Home', '2/5/2005' UNION ALLSELECT 1, 'Home', '2/5/2005' UNION ALLSELECT 2, 'Office', '2/5/2005' UNION ALLSELECT 1, 'Home', '2/5/2005' UNION ALLSELECT 1, 'Home', '2/5/2005' UNION ALLSELECT 1, 'Home', '2/5/2005' UNION ALLSELECT 2, 'Home', '2/5/2005'GOSELECT VisitType, MAX(COUNT_VisitTypes) AS MAX_COUNT_VisitTypes FROM ( SELECT [ID], VisitType, COUNT(*) AS COUNT_VisitTypes FROM myTable99 GROUP BY [ID], VisitType) AS XXXGROUP BY VisitTypeGOSET NOCOUNT OFFDROP TABLE myTable99GO Brett8-) |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-03-23 : 12:43:30
|
| your table will need a primary key on your table in order to do the calculations you need. As you may have learned by now, without a primary key on your tables there's not much you can do usefully with SQL.- Jeff |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-03-23 : 13:10:59
|
What do you plan to do with ties?USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99(Item char(1), Price int)GOINSERT INTO myTable99(Item, Price)SELECT 'a', 5 UNION ALLSELECT 'a', 6 UNION ALLSELECT 'a', 6 UNION ALLSELECT 'a', 4 UNION ALLSELECT 'b', 7 UNION ALLSELECT 'b', 7 UNION ALLSELECT 'b', 8 UNION ALLSELECT 'b', 8 UNION ALLSELECT 'c', 1 UNION ALLSELECT 'c', 2 UNION ALLSELECT 'c', 3 UNION ALLSELECT 'd', 2 UNION ALLSELECT 'd', 2 UNION ALLSELECT 'd', 2 UNION ALLSELECT 'd', 1GO SELECT * FROM ( SELECT xxx.Item, MAX(xxx.Count_Price) AS MAX_COUNT_Price FROM ( SELECT Item, Price, COUNT(*) AS COUNT_Price FROM myTable99 GROUP BY Item, Price) AS XXX GROUP BY Item) AS ALEFT JOIN ( SELECT Item, Price, COUNT(*) AS COUNT_Price FROM myTable99 GROUP BY Item, Price) AS B ON A.Item = B.Item AND A.MAX_COUNT_Price = B.COUNT_PriceGOSET NOCOUNT OFFDROP TABLE myTable99GO Brett8-) |
 |
|
|
|
|
|
|
|