Avatar of Pau Lo
Pau Lo
 asked on

group by min and max query

I am trying to write a query based on users home drives. We have imported a huge CSV inventory of data into an MSSQL, which is basically an inventory of all files each user has within their H drive of which there are over 3000 users and almost 1.4 million files stored between them. I am trying to 'group by' the username, and then get a min and max date (what we would use in Access), of a field called LastWriteTime (type date/time) based one each users home drive, to basically show 'for this users H:\drive this was the first LastWriteTime recorded, and this was the last LastWriteTime recorded. Having the stats per home drive folder would be perfect. So something like...

user - FirstModified - LastModified
\\server\share\user1 - 01/01/2019 - 22/11/2019
\\server\share\user2 - 01/06/2019 - 10/11/2019

The issue/challenge is, the field which represents folder/path (called FullName, type nvarchar(max)) I essentially need to use a segment of the text, e .g. \\server\share\username\ as the unique identifier on which to produce stats on. As entries lower down the inventory will be in format \\server\share\username\folder1\worddoc.docx etc. I am not sure if this would need to be done in multiple stages, but the consistent thing is, the username part would always be that before the  5th back slash, or we could even use the content between the 4th and 5th back slash, e.g. \\server\share\username\ (as everyone's H drive is unique and based upon their AD login name). Given the size of the data I cannot easily do any form of text to columns manipulation in another tool like Excel to extract the username in order to do some min/max date stats, hence we had to get the data into MSSQL in the first place.
DatabasesMicrosoft SQL Server

Avatar of undefined
Last Comment

8/22/2022 - Mon

sample of raw data would help (just a few rows for just a few users)

what version of mssql?
can you use string_split() ?
Pau Lo

MSSQL 2016 SP2.

For some random data, its essentially a database based upon the output of this AD command, just select a directory and its the same data its just we have imported it into MSSQL to get some stats on it:

Get-childitem -path “\\server\share” -recurse | select fullname,name,attributes,lastwritetime,creationtime,LastAccessTime, length | Export-Csv c:\users\me\desktop\inventory.csv –NoTypeInformation

Open in new window

Not familiar with string split to be honest.

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy