| Author |
Topic |
|
raxbat
Yak Posting Veteran
52 Posts |
Posted - 2007-09-27 : 06:21:58
|
Hello expert! I need some assistance from you.So, I have a table:Broker Currency Ask1 EURUSD 1.30002 USDCAD 2222 EURUSD 1.3022...........I need a query, which returns the MAX ASK values for every broker for every Currency.Help me pleas do it whith one query!Thanks a lot!  |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-27 : 06:24:27
|
[code]select Broker, Currency, max(Ask)from atablegroup by Broker, Currency[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
raxbat
Yak Posting Veteran
52 Posts |
Posted - 2007-09-27 : 07:12:09
|
| Sorry, I formulated my question incorrectly.The Idea is to get Max ASK value, currency and broker who has this valueso we need to get somth like that:2 EURUSD 1.2341 GBPUSD 2.234ok? |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-09-27 : 07:27:16
|
-- create our tabledeclare @foo table (Broker int, Currency varchar(10), Ask decimal(15,4))-- insert test datainsert into @foo select 1,'EURUSD',1.3 union all select2 ,'USDCAD',222 union all select2,'EURUSD',1.3022 union all select3, 'USDCAD',23.23 union all select3,'EURUSD',1.4-- do the workselect f.broker,f.currency, f.askfrom @foo finner join(select Currency, max(Ask) as Askfrom @foogroup by Currency) aon f.currency = a.currency and f.ask = a.ask [Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
raxbat
Yak Posting Veteran
52 Posts |
Posted - 2007-09-27 : 07:37:49
|
| rhanks a lot |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 08:48:41
|
This is a SQL Server 2005 forum, right?SELECT Broker, Currency, AskFROM ( SELECT Broker, Currency, Ask, ROW_NUMBER() OVER (PARTITION BY Currency ORDER BY Ask DESC) AS RecID FROM @Foo ) AS dWHERE RecID = 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-09-27 : 09:03:40
|
yes. yes it is. I don't know the "new" operators, so I did it the old way. [Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 09:07:45
|
I wasn't putting you down! I questioned OP. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
raxbat
Yak Posting Veteran
52 Posts |
Posted - 2007-09-27 : 10:47:06
|
| Another problem! I we have another column Date, I need to select max ASK and MAX date:SELECT TOP 100 PERCENT dbo.ClearingFarm.broker_id, dbo.ClearingFarm.symbol, dbo.ClearingFarm.ask, dbo.ClearingFarm.lastchangeFROM dbo.ClearingFarm INNER JOIN (SELECT symbol, MAX(Ask) AS Ask, MAX(lastchange) AS lastchange FROM clearingfarm GROUP BY symbol) a ON dbo.ClearingFarm.symbol = a.symbol AND dbo.ClearingFarm.ask = a.AskORDER BY dbo.ClearingFarm.symbolResults:broker_id symbol ask lastchange6 AUDUSD 0.8811 27/09/2007 17:39:075 AUDUSD 0.8811 27/09/2007 17:39:084 EURGBP 0.7001 27/09/2007 17:39:442 EURJPY 163.68 27/09/2007 17:39:482 EURUSD 1.4152 27/09/2007 17:39:486 EURUSD 1.4152 27/09/2007 17:39:115 GBPUSD 2.0227 27/09/2007 17:39:492 NZDUSD 0.7509 27/09/2007 17:39:486 USDCAD 1.0023 27/09/2007 17:39:115 USDCAD 1.0023 27/09/2007 17:39:163 USDCAD 1.0023 27/09/2007 17:39:232 USDJPY 115.67 27/09/2007 17:39:48 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 10:50:54
|
Blimey!There is absoluteley no guarantee that the two MAXes come from same record!Do you want the LATEST (max date) and all other relevant information for this record,or do you want the LARGEST (max ask) and all other relevant information for this record as before? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 10:53:38
|
I can prove my point with this simple codeDECLARE @Sample TABLE (Item INT, Date SMALLDATETIME, Val SMALLMONEY)INSERT @SampleSELECT 1, '20070101', 2000.5 UNION ALLSELECT 1, '20070925', -110.7 UNION ALLSELECT 2, '20070923', 2000 UNION ALLSELECT 2, '20070924', 1000SELECT Item, MAX(Date) AS mxDate, MAX(Val) AS mxValFROM @SampleGROUP BY ItemORDER BY Item E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 10:58:28
|
I posted a perfectly valid SQL Server 2005 query before.This is how I prove my other point, with same sample data as before.-- Prepare sample dataDECLARE @Sample TABLE (Item INT, Date SMALLDATETIME, Val SMALLMONEY)INSERT @SampleSELECT 1, '20070101', 2000.5 UNION ALLSELECT 1, '20070925', -110.7 UNION ALLSELECT 2, '20070923', 2000 UNION ALLSELECT 2, '20070924', 1000-- Show latest (by date) record per itemSELECT Item, Date, ValFROM ( SELECT Item, Date, Val, ROW_NUMBER() OVER (PARTITION BY Item ORDER BY Date DESC) AS RecID FROM @Sample ) AS dWHERE RecID = 1-- Show max (by val) record per itemSELECT Item, Date, ValFROM ( SELECT Item, Date, Val, ROW_NUMBER() OVER (PARTITION BY Item ORDER BY Val DESC) AS RecID FROM @Sample ) AS dWHERE RecID = 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-09-27 : 12:42:55
|
quote: Originally posted by Peso I wasn't putting you down! I questioned OP. E 12°55'05.25"N 56°04'39.16"
Lies! you hate me! [Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
raxbat
Yak Posting Veteran
52 Posts |
Posted - 2007-09-27 : 12:44:43
|
| OK, but what about SQL 2000?Thank for a great code for 2005! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-27 : 13:12:15
|
What about SQL Server 2000? This is a SQL Server 2005 forum.Are you using SQL Server 2000? Please repost your question in that forum. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|