| Author |
Topic |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-01-21 : 09:23:11
|
| Hi,I believe this deserves a new post.I am trying to learn Row_Number as someone has suggested it is the way to resolve my complex sql query.So, I am reading alot regarding this and trying to implement it.Several sample data is provided...Let me know if you need more please. so, please do not tell me off if I have not posted this properly :-)This is my initial query. The problem with it is:I only want the record which has the latest date but it returns the result below.At the very bottom of the post you will see my query with ROW_Number. Does it look ok to you? Thanksselect t.Security_ID, s.Source_Code, h.Bid_Price, h.Bid_Size, max(Import_Date) as Import_Datefrom tblTEST t inner join tblSources s on t.Security_ID = s.Security_ID inner join tblPricesSourcesImportHistory h on s.Source_ID = h.Source_ID where (t.Bid_Price_Best_Latest is null) and len(h.Bid_Price) > 1group by t.Security_ID, s.Source_ID, s.Source_Code, h.Bid_Price, h.Bid_Sizeorder by t.Security_ID, Import_Date descRESULT of the above query is here but just below you see what should be returned as the CORRECT result:Security_ID Source_Code Bid_Price Bid_Size Import_Date84 GFI01 102.25 1 2008-01-21 13:05:0084 GFI01 102.812 1 2008-01-21 12:02:0084 GFI01 102.75 1 2008-01-21 10:12:0084 GFI01 102.625 1 2008-01-21 10:08:0084 GFI01 103.062 1 2008-01-18 16:47:0084 TR75 103.00 0.5 2008-01-18 15:46:0084 GFI01 103.00 1 2008-01-18 09:33:0084 GFI01 103.125 1 2008-01-17 15:00:0084 EUROVCSC45 103.00 0.5 2008-01-17 15:00:0084 GFI01 103.25 1 2008-01-17 12:21:0084 GFI01 103.125 2 2008-01-17 09:53:0084 TR75 103.125 0.5 2008-01-16 17:49:0084 TR75 102.875 0.5 2008-01-16 09:10:0084 TR75 103.25 0.5 2008-01-15 17:23:0084 TTFN01 103.00 0.5 2008-01-14 08:54:00130 TR76 103.125 0.5 2008-01-18 16:58:00130 TR76 102.625 0.5 2008-01-18 12:08:00130 TR76 102.75 0.5 2008-01-17 16:45:00130 EUROVCSC45 102.50 0.5 2008-01-17 11:52:00130 TR76 102.50 0.5 2008-01-17 09:39:00130 TR76 102.375 0.5 2008-01-17 09:38:00130 TR76 102.25 0.5 2008-01-16 16:44:00173 TR75 101.625 0.5 2008-01-21 13:05:00173 TR75 101.50 0.5 2008-01-21 11:28:00173 TR75 102.062 0.5 2008-01-18 16:58:00173 TR75 102.00 0.5 2008-01-18 09:34:00173 EUROVCSC45 102.06 0.5 2008-01-17 15:00:00173 TTFN01 102.00 0.5 2008-01-17 13:28:00173 TR75 101.875 0.5 2008-01-17 08:25:00173 TTFN01 101.875 0.5 2008-01-16 16:51:00173 EUROVCSC45 102.00 0.5 2008-01-16 16:34:00173 EUROVCSC45 101.87 0.5 2008-01-16 08:38:00189 TR75 92.00 0.5 2008-01-18 17:29:00189 TR75 92.25 0.5 2008-01-17 17:06:00189 TR75 92.125 0.5 2008-01-17 14:15:00189 TR75 93.00 0.5 2008-01-17 08:01:00189 EUROVCSC8 92.25 0.5 2008-01-16 16:58:00189 EUROVCSC8 92.00 0.5 2008-01-16 10:05:00189 EUROVCSC8 91.00 0.5 2008-01-15 16:30:00189 TR75 91.50 0.5 2008-01-15 14:52:00249 GFI01 89.75 1 2008-01-18 17:34:00249 4GDC 90.25 1 2008-01-17 10:40:00249 EUROVCSC45 90.00 0.5 2008-01-17 09:29:00249 GFI01 90.25 1 2008-01-17 08:13:00249 EUROVCSC45 90.25 0.5 2008-01-16 10:56:00CORRECT RESULT SHOULD BE:Security_ID Source_Code Bid_Price Bid_Size Import_Date84 GFI01 102.25 1 2008-01-21 13:05:00130 TR76 103.125 0.5 2008-01-18 16:58:00173 TR75 101.625 0.5 2008-01-21 13:05:00189 TR75 92.00 0.5 2008-01-18 17:29:00249 GFI01 89.75 1 2008-01-18 17:34:00--------------Here is some sample data for you :create table tblTEST ( Security_ID smallint, Security_Name varchar(50), Bid_Price_Best_Latest decimal(12, 4), Bid_Size_Best_Latest decimal(12, 2), Bid_Source varchar(1000), Ask_Price_Best_Latest decimal(12, 4), Ask_Size_Best_Latest decimal(12, 2), Ask_Source varchar(1000) )insert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(22,'RUSSIA12 3/4 06/28', 183.0000,2.00,G,184.2500,2.00,'N') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(34,'ALFARU 7 7/8 10/09', 98.0000,0.50,'B',98.8800,0.50,'B') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(35,'ALFARU 8.635 02/17',93.0000,0.50,'B',94.0000,0.50,'B') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(36,'ALROSA 8 7/8 11/14',107.5000,0.50,'B',108.2500,1.00,'G') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(51,'EVRAZ 8 1/4 11/15', 98.5000 0.50,'T',99.5000,0.50,'T') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(56,'GAZPRU 7.201 02/20', 101.7500,2.00,'T',,NULL,NULL,NULL) union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(60,'GAZPRU 5 7/8 06/15', 93.5000 2.00,'I',NULL,NULL,NULL) union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(74,'LUKOIL 6.356 06/17', 94.2500 1.50,'G,N',94.8750,0.50,'N') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(75,'LUKOIL 6.656 06/22', 91.5000 0.50,'T',92.5000,0.50,'T') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(84,'MOBTEL 8 3/8 10/10', NULL,NULL,NULL,103.5000,0.50,'T') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(90,'NOMOS 8 1/4 05/09',97.2500,1.00,'B', 98.2500,1.00,'B') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(94,'PETRRU 8 3/4 12/09', 97.7500 0.50,'B',98.5000,0.50,'B') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(95,'PROMBK 8 3/4 10/10', 94.2500 0.50,'T',95.5000,0.50,'T') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(96,'RASPAD 7 1/2 05/12', 96.2500 1.00,'G',97.2500,1.00,'G') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(125,'RUSSIA 7 1/2 03/30',115.06204.00,'B,G',115.1800,6.00,'B,G,N') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(129,'UKRAIN 6.58 11/16', 99.8750 1.00,'T',NULL,NULL,NULL) union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(130,'AFKSRU 8 7/8 01/11',NULL,NULL,NULL,103.5000,0.50,'N') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(135,'ALFARU8.2 06/25/12',94.0000,0.50,'B',95.0000,0.50,'B') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(171,'MDMBK 7.765 01/10', 97.2500,1.00,'B',NULL,NULL,NULL) union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(173,'MEGAFO 8 12/10/09', NULL,NULL,NULL,102.4370,0.50,'T') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(176,'MOBTEL 8 01/28/12', 101.5000,1.00,'G',102.5000,1.00,'G') union allinsert into tblTEST(Security_ID, Security_Name, Bid_Price_Best_Latest,Bid_Size_Best_Latest,Bid_Source,Ask_Price_Best_Latest,Ask_Size_Best_Latest,Ask_Source) values(180,'NOMOS 8.1875 02/10',95.2500,1.00,'B',NULL,NULL,NULL) unin all.........RESULT of the above inserts:22 RUSSIA12 3/4 06/28 183.0000 2.00 G 184.2500 2.00 N34 ALFARU 7 7/8 10/09 98.0000 0.50 B 98.8800 0.50 B35 ALFARU 8.635 02/17 93.0000 0.50 B 94.0000 0.50 B36 ALROSA 8 7/8 11/14 107.5000 0.50 B 108.2500 1.00 G51 EVRAZ 8 1/4 11/15 98.5000 0.50 T 99.5000 0.50 T56 GAZPRU 7.201 02/20 101.7500 2.00 T NULL NULL NULL60 GAZPRU 5 7/8 06/15 93.5000 2.00 I NULL NULL NULL74 LUKOIL 6.356 06/17 94.2500 1.50 G,N 94.8750 0.50 N75 LUKOIL 6.656 06/22 91.5000 0.50 T 92.5000 0.50 T84 MOBTEL 8 3/8 10/10 NULL NULL NULL 103.5000 0.50 T90 NOMOS 8 1/4 05/09 97.2500 1.00 B 98.2500 1.00 B94 PETRRU 8 3/4 12/09 97.7500 0.50 B 98.5000 0.50 B95 PROMBK 8 3/4 10/10 94.2500 0.50 T 95.5000 0.50 T96 RASPAD 7 1/2 05/12 96.2500 1.00 G 97.2500 1.00 G125 RUSSIA 7 1/2 03/30 115.0620 4.00 B,G 115.1800 6.00 B,G,N129 UKRAIN 6.58 11/16 99.8750 1.00 T NULL NULL NULL130 AFKSRU 8 7/8 01/11 NULL NULL NULL 103.5000 0.50 N135 ALFARU8.2 06/25/12 94.0000 0.50 B 95.0000 0.50 B171 MDMBK 7.765 01/10 97.2500 1.00 B NULL NULL NULL173 MEGAFO 8 12/10/09 NULL NULL NULL 102.4370 0.50 T176 MOBTEL 8 01/28/12 101.5000 1.00 G 102.5000 1.00 G180 NOMOS 8.1875 02/10 95.2500 1.00 B NULL NULL NULL188 PROMBK 8 3/4 10/11 93.5000 0.50 T 94.5000 0.50 B189 PROMBK 9 5/8 05/12 NULL NULL NULL 93.5000 0.50 T221 VIP 8 1/4 05/23/16 99.0000 1.00 G 100.0000 1.00 G233 ZENIT 8 3/4 10/09 98.7500 0.50 N 99.2500 0.50 N249 TMENRU 6 5/8 03/17 NULL NULL NULL 90.3750 1.00 G250 TNEFT 6.103 06/12 99.1250 0.50 T 99.8750 0.50 T255 VIP 10 06/16/09 103.5000 0.50 T 104.5000 0.50 T333 GAZPRU 6.605 02/18 93.5000 1.00 T NULL NULL NULL341 TMENRU 7 7/8 03/18 97.5000 0.50 T 98.5000 0.50 T346 NKNCRU 8 1/2 12/15 96.5000 0.50 L 97.0000 0.50 L355 FIUKR 9 3/4 02/10 95.0000 0.50 L NULL NULL NULL373 URSAP 7 05/21/10 90.0000 1.00 T NULL NULL NULL381 TURKEY11 7/8 01/30 155.1200 2.00 B 156.0000 2.00 B-----------------CREATE TABLE [dbo].[tblSources]( [Source_ID] [int] IDENTITY(1,1) NOT NULL, [Security_ID] [smallint] NOT NULL, [Source_Parent] [varchar](50) NOT NULL, [Source_Code] [varchar](20) NOT NULL, [Source_Security_Name] [varchar](50) NOT NULL)insert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (121,84,'Testers','GFI01','MTS 10') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (169,84,'Testers','QLABND1','MTS 10 .') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (177,130,'Testers','QLABND1','SIST 11') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (177,130,'Testers','QLABND1','SIST 11') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (370,84,'Testers','TR75','MTS 8.375 10') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (517,84,'Testers','EUROVCSC45','MTS 10 $') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (520,130,'Testers ','EUROVCSC45','SISTEMA 11 $') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (562,130,'Testers ','NFBC','SIST11') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (623,84,'Testers','4GDC','MTS 10$') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (635,130,'Testers','4GDC','SISTEMA 11$') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (742,84,'Testers','TTFN01','MTS 10 $') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (752,130,'Testers','TTFN01','SISTEMA 11$') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (816,84,'Testers','TTFN06','MTS 10 $') union allinsert into tblSources (Source_ID, Security_ID,Source_Parent,Source_Code,Source_Security_Name) values (829,130,'Testers','TTFN06','SISTEMA 11$')RESULT of the above inserts:Source_ID Security_ID Source_Parent Source_Code Source_Security_Name121 84 Testers GFI01 MTS 10169 84 Testers QLABND1 MTS 10 .177 130 Testers QLABND1 SIST 11370 84 Testers TR75 MTS 8.375 10405 130 Testers TR76 SIST 8.875 11517 84 Testers EUROVCSC45 MTS 10 $520 130 Testers EUROVCSC45 SISTEMA 11 $562 130 Testers NFBC SIST11623 84 Testers 4GDC MTS 10$635 130 Testers 4GDC SISTEMA 11$742 84 Testers TTFN01 MTS 10 $752 130 Testers TTFN01 SISTEMA 11$816 84 Testers TTFN06 MTS 10 $829 130 Testers TTFN06 SISTEMA 11$---------------------my row_number() queryselect t.Security_ID, s.Source_ID, s.Source_Code, max(h.Bid_Price), max(h.Bid_Size), max(h.Import_Date) as Import_Datefrom( select Security_ID, Source_ID, Source_Code, null, null ROW_NUMBER() OVER (PARTITION BY Security_ID ORDER BY Import_Date DESC) AS RecID from tblSources) tblTEST t inner join tblSources s on t.Security_ID = s.Security_ID inner join tblPricesSourcesImportHistory h on s.Source_ID = h.Source_ID where (t.Bid_Price_Best_Latest is null) and len(h.Bid_Price) > 1group by t.Security_ID, s.Source_ID, s.Source_Code, h.Bid_Price, h.Bid_Sizeorder by Security_ID, Import_Date desc |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-21 : 09:56:44
|
18th time...SELECT Security_ID, Source_Code, Bid_Price, Bid_Size, Import_Date FROM (SELECT Security_ID, Source_Code, Bid_Price, Bid_Size, Import_Date, ROW_NUMBER() OVER (PARTITION BY Security_ID ORDER BY Import_Date DESC) AS RecIDFROM tblTest) AS dWHERE RecID = 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-01-21 : 10:20:51
|
| As I require data from other tables I have modified it as below.Does it look ok to you? thanksSELECT Security_ID, Source_Code, Bid_Price, Bid_Size, Import_Date FROM (SELECT t.Security_ID, s.Source_Code, h.Bid_Price, h.Bid_Size, h.Import_Date, ROW_NUMBER() OVER (PARTITION BY t.Security_ID ORDER BY Import_Date DESC) AS RecIDFROM tblTest t inner join tblSources s on t.Security_ID = s.Security_ID inner join tblPricesSourcesImportHistory h on s.Source_ID = h.Source_ID) AS dWHERE RecID = 1 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-21 : 10:26:15
|
I can't tell. It depends on your business rules where to append the JOIN. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-21 : 10:27:09
|
Run the query. If the results look OK, the query is problably working. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-01-21 : 10:28:55
|
| Just ran it. Looks ok, need to run more tests.If it is ok, I will expand the sql and ask you to have a look at it later.Thanks |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-01-21 : 11:52:21
|
| It appears there are records missing using this query:SELECT Security_ID, Bid_Price, Bid_Size, Source_Code, Import_Date FROM (SELECT t.Security_ID, case when dbo.isreallynumeric(ltrim(rtrim(h.Bid_Price))) = 1 then convert(decimal(12, 8), ltrim(rtrim(h.Bid_Price))) end as Bid_Price, h.Bid_Size, isnull(dbo.fnSourceShortCode(s.Source_Code),'') as Source_Code, h.Import_Date, ROW_NUMBER() OVER (PARTITION BY t.Security_ID ORDER BY Import_Date DESC) AS RecIDFROM tblTest t inner join tblSources s on t.Security_ID = s.Security_ID inner join tblPricesSourcesImportHistory h on s.Source_ID = h.Source_ID) AS dWHERE RecID = 1 and ltrim(rtrim(len(Bid_Price))) > 1RESULT:Security_ID Bid_Price Bid_Size Bid_Source Import_Date22 182.62500000 2 G 2008-01-21 13:28:0035 93.00000000 0.5 B 2008-01-21 10:50:0051 98.50000000 0.5 T 2008-01-21 11:21:0056 101.75000000 2 T 2008-01-21 11:59:0075 91.50000000 0.5 T 2008-01-21 10:56:0090 97.25000000 1 B 2008-01-21 10:49:0094 97.75000000 0.5 B 2008-01-21 10:23:0095 94.25000000 0.5 T 2008-01-21 14:29:0096 95.75000000 1 G 2008-01-21 13:52:00125 115.00000000 2 G 2008-01-21 14:06:00135 94.00000000 0.5 B 2008-01-21 10:50:00171 97.25000000 1 B 2008-01-21 10:49:00176 101.25000000 1 G 2008-01-21 13:50:00180 95.25000000 1 B 2008-01-21 10:49:00188 93.50000000 0.5 T 2008-01-21 14:29:00233 98.75000000 0.5 N 2008-01-21 11:33:00255 103.62500000 0.5 T 2008-01-21 14:30:00333 93.00000000 1 T 2008-01-21 14:23:00341 97.50000000 0.5 T 2008-01-21 11:57:00346 96.50000000 0.5 L 2008-01-21 13:48:00355 95.00000000 0.5 L 2008-01-21 10:16:00373 90.00000000 1 T 2008-01-21 14:33:00381 155.00000000 2 B 2008-01-21 14:15:00In case it helps, the query below returns all the Security_IDs that should be shown.select t.Security_ID, Max(h.Import_Date) Import_Date from tblTEST t inner join tblSources s on t.Security_ID = s.Security_ID inner join tblPricesSourcesImportHistory h on s.Source_ID = h.Source_IDgroup by t.Security_ID order by t.Security_IDRESULT:Security_ID Import_Date22 2008-01-21 13:28:0034 2008-01-21 10:50:0035 2008-01-21 10:50:0036 2008-01-21 13:52:0051 2008-01-21 11:21:0056 2008-01-21 11:59:0060 2008-01-21 14:23:0074 2008-01-21 13:52:0075 2008-01-21 10:56:0084 2008-01-21 14:29:0090 2008-01-21 10:49:0094 2008-01-21 10:23:0095 2008-01-21 14:29:0096 2008-01-21 13:52:00125 2008-01-21 14:06:00129 2008-01-21 14:23:00130 2008-01-21 11:35:00135 2008-01-21 10:50:00171 2008-01-21 10:49:00173 2008-01-21 14:31:00176 2008-01-21 13:50:00180 2008-01-21 10:49:00188 2008-01-21 14:29:00189 2008-01-21 14:23:00221 2008-01-21 13:52:00233 2008-01-21 11:33:00249 2008-01-21 13:52:00250 2008-01-21 14:30:00255 2008-01-21 14:30:00333 2008-01-21 14:23:00341 2008-01-21 11:57:00346 2008-01-21 13:48:00355 2008-01-21 10:16:00373 2008-01-21 14:33:00381 2008-01-21 14:15:00 |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-01-21 : 12:01:35
|
| Altered the select as follows:SELECT Security_ID, Bid_Price, Bid_Size, Source_Code, Import_Date FROM (SELECT t.Security_ID, casewhen dbo.isreallynumeric(ltrim(rtrim(h.Bid_Price))) = 1 then convert(decimal(12, 8), ltrim(rtrim(h.Bid_Price))) end as Bid_Price,h.Bid_Size,isnull(dbo.fnSourceShortCode(s.Source_Code),'') as Source_Code, h.Import_Date, ROW_NUMBER() OVER (PARTITION BY t.Security_ID ORDER BY Import_Date DESC) AS RecIDFROM tblTest t inner join tblSources s on t.Security_ID = s.Security_ID inner join tblPricesSourcesImportHistory h ons.Source_ID = h.Source_IDWHERE ltrim(rtrim(len(h.Bid_Price))) > 1) AS dWHERE RecID = 1and ltrim(rtrim(len(Bid_Price))) > 1 |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-01-21 : 12:29:40
|
| It seems the results are coming out nicely so far. Still testing...Do you know how I can join these two select queries? thanksSELECT Security_ID, Bid_Price, Bid_Size, Source_Code, Import_Date FROM ( SELECT t.Security_ID, case when dbo.isreallynumeric(ltrim(rtrim(h.Bid_Price))) = 1 then convert(decimal(12, 8), ltrim(rtrim(h.Bid_Price))) end as Bid_Price, h.Bid_Size, isnull(dbo.fnSourceShortCode(s.Source_Code),'') as Source_Code, h.Import_Date, ROW_NUMBER() OVER (PARTITION BY t.Security_ID ORDER BY Import_Date DESC) AS RecID FROM tblTest t inner join tblSources s on t.Security_ID = s.Security_ID inner join tblPricesSourcesImportHistory h on s.Source_ID = h.Source_ID WHERE ltrim(rtrim(len(h.Bid_Price))) > 1) AS dWHERE RecID = 1 and ltrim(rtrim(len(Bid_Price))) > 1---------SELECT Security_ID, Ask_Price, Ask_Size, Source_Code, Import_Date FROM ( SELECT t.Security_ID, case when dbo.isreallynumeric(ltrim(rtrim(h.Ask_Price))) = 1 then convert(decimal(12, 8), ltrim(rtrim(h.Ask_Price))) end as Ask_Price, h.Ask_Size, isnull(dbo.fnSourceShortCode(s.Source_Code),'') as Source_Code, h.Import_Date, ROW_NUMBER() OVER (PARTITION BY t.Security_ID ORDER BY Import_Date DESC) AS RecID FROM tblTest t inner join tblSources s on t.Security_ID = s.Security_ID inner join tblPricesSourcesImportHistory h on s.Source_ID = h.Source_ID WHERE ltrim(rtrim(len(h.Ask_Price))) > 1) AS dWHERE RecID = 1 and ltrim(rtrim(len(Ask_Price))) > 1 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-21 : 16:19:01
|
The easiest thing isselect *from (1st query here) as t1inner join (2nd query here) as t2 on t2.security_id = t1.security_idI wrote this to you for 4 months ago. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|