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 |
|
dimoss
Yak Posting Veteran
52 Posts |
Posted - 2006-05-28 : 07:39:54
|
| Hi,I have a table with 4 fields (id, nomarxia, adt, moria) I want to make a query which will look for a specific "adt" (ex WHERE adt="something") and will return the "nomraxia" and "moria".Till now it's apiece of cake.However I want with these results ("nomarxia") to receive the MAX(moria), MIN(moria) and COUNT(id). I know that it has to be a subquery...but how?Please help..DenisEXAMPLE with fieldsTable: DEid nomarxia adt moria1...Ath......M1234....7002...Ath.......F5323....8003...Ait.......M1234....7004...Kav.......M1234....7005...Xan.......F5323....8006...Kav.......F5323....8007...Spa.......G5654....10008...Xan.......L9876....11009...Ait.......P2134....950 10..Kav.......M1234....600 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-28 : 07:48:07
|
is this what you want ?select nomarxia, max(moria), min(moria), count(id)from tablegroup by nomarxia KH |
 |
|
|
dimoss
Yak Posting Veteran
52 Posts |
Posted - 2006-05-28 : 07:51:06
|
quote: Originally posted by khtan is this what you want ?select nomarxia, max(moria), min(moria), count(id)from tablegroup by nomarxia KH
Nope...I have already did thatI want in these results to find MAX, MIN and COUNT...I mean the results according to WHERE Clause..See the table above in the 1st postexample:I want the max(moria), min(moria), count(id) of all where adt = "M1234"www.tabletennis.gr |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-28 : 08:11:56
|
I see you have posted some sample data. Can you post the expected result ? KH |
 |
|
|
dimoss
Yak Posting Veteran
52 Posts |
Posted - 2006-05-28 : 08:16:54
|
quote: Originally posted by khtan I see you have posted some sample data. Can you post the expected result ? KH
For the selected condition and I mean where adt = "M1234"I expect to get thisnomarxia.moria.MAX_moria..MIN_moria..Count_idAth......700...800........700........2Ait......700...950........700........2Kav......700...800........600........3www.tabletennis.gr |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-28 : 08:23:45
|
Based on the sample data, i got slighly different result. Maybe i miss interpret your requirement on min() & max().Anyway have a look at this.declare @DE table( id int, nomarxia varchar(10), adt varchar(10), moria int)insert into @DEselect 1, 'Ath', 'M1234', 700 union allselect 2, 'Ath', 'F5323', 800 union allselect 3, 'Ait', 'M1234', 700 union allselect 4, 'Kav', 'M1234', 700 union allselect 5, 'Xan', 'F5323', 800 union allselect 6, 'Kav', 'F5323', 800 union allselect 7, 'Spa', 'G5654', 1000 union allselect 8, 'Xan', 'L9876', 1100 union allselect 9, 'Ait', 'P2134', 950 union allselect 10, 'Spa', 'H9735', 980select a.nomarxia, a.moria, b.max_moria, b.min_moriafrom( select nomarxia, moria from @DE where adt = 'M1234') ainner join( select nomarxia, max(moria) as max_moria, min(moria) as min_moria from @DE group by nomarxia) bon a.nomarxia = b.nomarxia KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-28 : 08:29:58
|
Based on you modifed samples data & result, i changed accordingly to below.declare @DE table( id int, nomarxia varchar(10), adt varchar(10), moria int)insert into @DEselect 1, 'Ath', 'M1234', 700 union allselect 2, 'Ath', 'F5323', 800 union allselect 3, 'Ait', 'M1234', 700 union allselect 4, 'Kav', 'M1234', 700 union allselect 5, 'Xan', 'F5323', 800 union allselect 6, 'Kav', 'F5323', 800 union allselect 7, 'Spa', 'G5654', 1000 union allselect 8, 'Xan', 'L9876', 1100 union allselect 9, 'Ait', 'P2134', 950 union allselect 10, 'Kav', 'M1234', 600select a.nomarxia, a.moria, b.max_moria, b.min_moria, b.cntfrom( select nomarxia, max(moria) as moria from @DE where adt = 'M1234' group by nomarxia) ainner join( select nomarxia, max(moria) as max_moria, min(moria) as min_moria, count(*) as cnt from @DE group by nomarxia) bon a.nomarxia = b.nomarxia/* Result : nomarxia moria max_moria min_moria cnt ---------- ----------- ----------- ----------- ----------- Ait 700 950 700 2Ath 700 800 700 2Kav 700 800 600 3(3 row(s) affected)*/ KH |
 |
|
|
dimoss
Yak Posting Veteran
52 Posts |
Posted - 2006-05-28 : 08:31:42
|
| It doesn't work...:-(I forgot to mention that I work in Access..the table is in Access..Sorry..:-(www.tabletennis.gr |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-28 : 08:33:50
|
quote: Originally posted by dimoss It doesn't work...:-(I forgot to mention that I work in Access..the table is in Accesswww.tabletennis.gr
Oh Access . My query works on MS SQL Server. Anyway when you run the query in access, any error encounter ? KH |
 |
|
|
dimoss
Yak Posting Veteran
52 Posts |
Posted - 2006-05-28 : 08:44:47
|
quote: Originally posted by khtan
quote: Originally posted by dimoss It doesn't work...:-(I forgot to mention that I work in Access..the table is in Accesswww.tabletennis.gr
Oh Access . My query works on MS SQL Server. Anyway when you run the query in access, any error encounter ? KHSyntax error in query. Incomplete query clause.
www.tabletennis.gr |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-28 : 09:02:31
|
Try this.select nomarxia, max(moria) as moria, (select max(moria) from DE x where x.nomarxia = d.normaxia) as max_moria, (select min(moria) from DE x where x.nomarxia = d.normaxia) as min_moria, (select count(*) from DE x where x.nomarxia = d.normaxia) as cntfrom DE dwhere adt = 'M1234'group by nomarxia If it does not work, try posting at http://www.sqlteam.com/forums/forum.asp?FORUM_ID=3.Sorry i can't help you much as not familiar with MS Access. KH |
 |
|
|
dimoss
Yak Posting Veteran
52 Posts |
Posted - 2006-05-28 : 09:08:04
|
| Didn't work...Ask for parameter value "moria"Anyway...I appreciate your help...:-)I will try to post it in the forum you mentionedwww.tabletennis.gr |
 |
|
|
cmdr_skywalker
Posting Yak Master
159 Posts |
Posted - 2006-05-29 : 01:14:52
|
| you need to have a table column named "moria". Try to use the query using the actual table and column. if you can't figure that out, post the table definition here. Access is similar to SQL except you don't have the table declaration and only one statement per query object. So, if you have multiple statement above, run them on separate query object. You must first create the table and changed the table name of the queries. Hope this helps.May the Almighty God bless us all! |
 |
|
|
|
|
|
|
|