简体   繁体   中英

How to get data from two SQL Server tables with no relationship?

I've been asked to extract data from two tables but can't quite figure it out and its a bit over my head at this point and could really use some help.

Two tables, not quite parent/child relationship (in my limited understanding at least), and no primary keys that I see (didn't set the DB up, so if you think it would help to add feel free to say so).

Table #1 has server name, share name, path of share, owner of share, and permissions. Server names, share names, owner, and permissions can be duplicates. The combination of server name and path is what is unique to each record (should that be a composite key?). Paths can be nested (D:\\Folder1\\ is a path, as well as D:\\Folder1\\Folder2; see below for example)


Table #1 - Shares

Servername     ShareName     Path          Owner      Permissions
-----------------------------------------------------------------
Server01       Share01       D:\Folder1    jsmith     Everyone
Server01       Share02       D:\Folder2    jsmith     jsmith,ssmith
Server01       Share03       D:\Folder2\A  jdoe       jdoe,jsmith
Server02       Share01       E:\Folder1    ksmith     ksmith,jdoe
Server03       Share01       G:\Folder1    jsmith     Everyone
Server03       Share02       C:\Folder1    jsmith     Everyone



Table #2 is a breakdown of the files listed on those servers. This list can also include file paths that are not listed in table #1. Each file record is unique but the file names/paths/extension/modified date themselves are not.

Table #2 - FileInfo

Hostname     Filename     FolderPath    FileExtension LastModified
-----------------------------------------------------------------
Server01     FileA.txt    D:\Folder1    txt           1/1/1900
Server01     FileB.log    D:\Folder2    log           2/1/2000
Server01     FileC.ini    D:\Folder2\A  ini           3/1/2001
Server02     FileD.xls    E:\Folder1    xls           3/2/2010
Server03     FileE.exe    G:\Folder1    exe           3/2/2011
Server03     FileF.ppt    C:\Folder1    ppt           5/10/1998



From these two I would like to be able to query and get information from both. For instance, for every path listed in Table #1, what the oldest LastModified date for all files of that path in Table #2.

There are ~160k rows in Table #1, and 110 million rows in Table #2 so I'm not sure if that forces any particular approach?

If I understand the question correctly, all you need is a simple JOIN using two columns from each table:

SELECT
  Shares.Servername,
  Shares.Path,
  MIN(FileInfo.LastModified)
FROM Shares
INNER JOIN FileInfo ON
  Shares.Servername = FileInfo.Hostname AND
  Shares.Path = FileInfo.FolderPath
GROUP BY
  Shares.Servername,
  Shares.Path

Could you do something like this with a common table expresion:

;with t2 as (
select *,
row_number() over(partition by FolderPath order by LastModified asc) as RowNum
from Table2)
select t2.LastModified, t1.Path
from t2
inner join Table1 t1 on t2.FolderPath = t1.Path
where RowNum = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM