2

I need help writing an SQL Query which would get me the results as I want.

I have a document upload feature, in which a document can be upload for clients on multiple locations. The table structure is simple, there are just 5 columns; FileID, FileName, Title, Description and LocationID. If a user uploads a document for 3 locations, 3 entries are entered in to this table. Lets say

FileID FileName    Title      Description      LocationID
1      File1       File1      Description 1    100
2      File1       File1      Description 1    21
4      File1       File1      Description 1    181

A user uploads another file for 4 locations, the data gets entered and the table now looks like this

FileID FileName    Title      Description      LocationID
1      File1.doc   File1      Description 1    100
2      File1.doc   File1      Description 1    21
4      File1.doc   File1      Description 1    181
5      File2.pdf   File2      Description 2    123
6      File2.pdf   File2      Description 2    12
7      File2.pdf   File2      Description 2    126
8      File2.pdf   File2      Description 2    100

Now what i want in the result is

FileName    Title      Description      LocationCSV
File1.doc   File1      Description 1    100, 21, 181
File2.pdf   File2      Description 2    123, 12, 126, 100
valex
  • 23,966
  • 7
  • 43
  • 60
Taha Rehman Siddiqui
  • 2,441
  • 5
  • 32
  • 58

3 Answers3

5

In SQL Server you can use FOR XML PATH:

SELECT DISTINCT FileName,
  Title,
  Description,
  STUFF((SELECT ', ' + cast(LocationID as varchar(20))
         from yourtable m
         where p.FileName = m.FileName
         group by LocationID, fileid
         order by fileid
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') LocationCSV
from yourtable p

See SQL Fiddle with Demo

Or another version using FOR XML PATH:

SELECT FileName,
  Title,
  Description, LEFT(LocationCSV , LEN(LocationCSV)-1) LocationCSV
FROM yourtable p
CROSS APPLY
(
    SELECT cast(LocationID as varchar(20)) + ','
    FROM yourtable m
    WHERE p.FileName = m.FileName
    FOR XML PATH('')
) m (LocationCSV)
group by FileName, Title, Description, LocationCSV

See SQL Fiddle with Demo

Result:

|  FILENAME | TITLE |   DESCRIPTION |        LOCATIONCSV |
----------------------------------------------------------
| File1.doc | File1 | Description 1 |       100, 21, 181 |
| File2.pdf | File2 | Description 2 |  123, 12, 126, 100 |
Taryn
  • 242,637
  • 56
  • 362
  • 405
3

In MySQL you can use GROUP_CONCAT

select FileName,
       Title,
       Description, 
       group_concat(LocationID) as  LocationCSV 
from t group by FileName,Title,Description
valex
  • 23,966
  • 7
  • 43
  • 60
  • +1 didn't know about `group_concat`. Thanks. I'll have to check if SQL Server has something like this. – jimhark Dec 14 '12 at 11:28
0

Take a look at the answer of Bradc. I like this method to concat columns.

Simulating group_concat MySQL function in Microsoft SQL Server 2005?

Community
  • 1
  • 1
syed mohsin
  • 2,948
  • 2
  • 23
  • 47