| Author |
Topic |
|
1sqlover
Yak Posting Veteran
56 Posts |
Posted - 2006-05-26 : 16:32:21
|
| SELECT * FROM ARCHIVELOG|Volume |StorageDrive |Folder |FolderCreated2090 D:\PHOTOS 289 1-----------------------------------------------2090 E:\ARCHIVE 290 1-----------------------------------------------2091 D:\PHOTOS 289 1-----------------------------------------------2091 NO ARCHIVE 290 0 -----------------------------------------------2092 D:\PHOTOS 291 1-----------------------------------------------2092 NO ARCHIVE 291 0 -----------------------------------------------2093 D:\PHOTOS 292 1-----------------------------------------------2093 NO ARCHIVE 292 0 -----------------------------------------------These are the results I get when I do "SELECT * FROM ARCHIVELOG", how can I create a view that will select all of the rows that have a 0 (as in the archive has not been created) but will display the "StorageDrive" of the previous record?In other words I want my results to look like this|Volume |StorageDrive |Folder |Archived2091 D:\PHOTOS 289 0 -----------------------------------------------2092 D:\PHOTOS 291 0-----------------------------------------------2093 D:\PHOTOS 292 0-----------------------------------------------Is this achievable? If yes, how can I query this and insert it into another DB?epoh |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-26 : 20:23:15
|
"Is this achievable?"Yesselect Volumne, (select top 1 StorageDrive from ARCHIVELOG x where x.Volumn < a.Volumn order by Volumn desc), Folder, 0 as Archivedfrom ARCHIVELOG awhere a.FolderCreated = 0 "how can I query this and insert it into another DB?"insert into db2.dbo.Table(...)select ...from ...where ... KH |
 |
|
|
1sqlover
Yak Posting Veteran
56 Posts |
Posted - 2006-05-26 : 21:05:43
|
| Thanks,How can I get the Top 1 to be the name of a column? Right now all I get is: No column nameepoh |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-05-26 : 21:18:55
|
| [code]select Volumne, (select top 1 StorageDrive from ARCHIVELOG x where x.Volumn < a.Volumn order by Volumn desc) as [Storage Drive] , Folder, 0 as Archivedfrom ARCHIVELOG awhere a.FolderCreated = 0[/code]Srinika |
 |
|
|
1sqlover
Yak Posting Veteran
56 Posts |
Posted - 2006-05-26 : 21:25:39
|
| Sorry never mind I figured that one out.epoh |
 |
|
|
1sqlover
Yak Posting Veteran
56 Posts |
Posted - 2006-05-26 : 21:26:24
|
| thanksepoh |
 |
|
|
1sqlover
Yak Posting Veteran
56 Posts |
Posted - 2006-05-26 : 21:47:00
|
| I guess I should have specified earlier. Volume and Folder are subfolders to StorageDrive. The purpose of StorageDrive is to record whether or not the items have been archived or not.For the purposes of not knowing the correct term I will call the two records with the same information in "Volume" and "Folder" a "Group"The "first" record in the "Group" indicates the following: StorageDrive (is the drive letter and basic path of the where the files are located). *Volume is a subfolder to StorageDrive and Folder is a subfolder to Volume. Files are in the "Folder" fieldThe "second" record in the "Group" indicates the following: StorageDrive (is the drive letter and basic path of the Archive). *Same as aboveI played with the sql stmt that 'khtan' was kind enough to provide and it helped but I could not get it to work.epoh |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-26 : 22:05:17
|
quote: Volume |StorageDrive |Folder |Archived2091 D:\PHOTOS 289 0
Can you explain how to obtain the above record ?Or is this what you want ?select Volume, (select top 1 StorageDrive from ARCHIVELOG x where x.Volume = a.Volume and x.FolderCreated = 1 order by x.Folder) as [Storage Drive], Folder, 0 as Archivedfrom ARCHIVELOG awhere a.FolderCreated = 0 KH |
 |
|
|
1sqlover
Yak Posting Veteran
56 Posts |
Posted - 2006-05-26 : 22:17:23
|
| Okay, this is what I have come up with, with your help:SELECT STORAGEDRIVE, VOLUME, (SELECT TOP 1 STORAGEDRIVE FROM ARCHIVELOG X WHERE X.VOLUME = A.VOLUME) AS STORAGE, FOLDER, 0 AS ARCHIVEDFROM ARCHIVELOG AWHERE A.FOLDERCREATED = 0ORDER BY A.VOLUME, A.FOLDERAnd it returns the following:STORAGEDRIVE,VOLUME,STORAGE,FOLDER,FOLDERCREATED,ARCHIVENO ARCHIVE ,1101,D:\PHOTOS,215,0NO ARCHIVE ,1101,D:\PHOTOS,216,0NO ARCHIVE ,1103,D:\PHOTOS,9,0NO ARCHIVE ,1103,D:\PHOTOS,10,0NO ARCHIVE ,1103,D:\PHOTOS,11,0D:\PHOTOS,1104,D:\PHOTOS,1,0Do you notice how StorageDrive and Storage are not equal. If there was a way to return those records that are not equal my query would be solved.Thanksepoh |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-26 : 22:21:01
|
So you have got what you wanted ? KH |
 |
|
|
1sqlover
Yak Posting Veteran
56 Posts |
Posted - 2006-05-26 : 22:24:31
|
| Close, but not quite. Is there a way to filter out the records that STORAGEDRIVE and STORAGE are not equal, from within this query.epoh |
 |
|
|
1sqlover
Yak Posting Veteran
56 Posts |
Posted - 2006-05-26 : 22:26:23
|
| I am new to sql (as if that wasnt apparent, lol) I tried to do the following:SELECT STORAGEDRIVE, VOLUME, (SELECT TOP 1 STORAGEDRIVE FROM ARCHIVELOG X WHERE X.VOLUME = A.VOLUME) AS STORAGE,FOLDER, 0 AS ARCHIVEDFROM ARCHIVELOG AWHERE A.FOLDERCREATED = 0AND STORAGEDRIVE <> STORAGEORDER BY A.VOLUME, A.FOLDERThat didnt work because it was an alias, am I stuck?epoh |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-26 : 22:27:58
|
Or this ?select a.Volume, b.StorageDrive, b.Folderfrom ARCHIVELOG a inner join ( select Volume, StorageDrive, Folder from ARCHIVELOG where FolderCreated = 1 ) b on a.Volume = b.Volumewhere a.FolderCreated = 0 "will display the "StorageDrive" of the previous record?"Also i would like to highlight that records are not stored in database in any sequence or ordered. So there isn't a first record or last record, previous or next record. You define the sequence of the order retrieve using the ORDER BY.So the statement "SELECT TOP 1 col FROM table" is not meaningful. You should always use TOP with ORDER BY. KH |
 |
|
|
1sqlover
Yak Posting Veteran
56 Posts |
Posted - 2006-05-26 : 22:29:26
|
| Thank you for pointing that out (the Order by). I will try this and see how it works.epoh |
 |
|
|
|