JOIN three tables and aggregate data from multiple

2019-09-18 11:04发布

JOIN three tables and aggregate data from multiple rows for every DISTINCT row in separate column

i have a table where one item is mapped with multiple items.

Key 1 | Key 2
1       2
1       5
1       6
1       4
1       8

I have another table like this

Key 1 | ShortKey1Desc
1       'Desc short'

i have one more table where i have data like this

Key 1 | Description
1       'Desc a'
1       'Desc c'
1       'Desc aa'
1       'Desc tt'

i need to write a sql query for my view where table would be generated like this

Key 1 | AllKeys2ForKey1 | AllDescriptionsForKey1           | ShortKey1Desc
1     | 2;5;6;4;8       | Desc a; Desc c; Desc aa; Desc tt | Desc short

Key 1 is a string type field so i need to join them table using that string key

what i'm trying is to create a view for comfortable data access. need to create a query what will not take ages. i already tried to do it with Functions but it takes ages for load.

any help on this one would be highly appreciated. thanks a lot

3条回答
来,给爷笑一个
2楼-- · 2019-09-18 11:21

to convert rows into one single result you will need to save values in a variable, below is sample code just to give you an idea

Declare @AllKeys2ForKey1 varchar(50)
set @AllKeys2ForKey1 = ''
SELECT @AllKeys2ForKey1 = @AllKeys2ForKey1  + cast([Key 2] as varchar(3)) + ','
  FROM [AllKeys2ForKey1Table] where [KEY 1] = 1


Declare @AllDescriptionsForKey1 varchar(100)
set @AllDescriptionsForKey1 = ''
SELECT @AllKeys2ForKey1 = @AllKeys2ForKey1  + [Description] + ','
  FROM [AllDescriptionsForKey1Table] where [KEY 1] = 1

Declare @ShortKey1Desc varchar(100)
set @ShortKey1Desc = ''
SELECT @ShortKey1Desc = @ShortKey1Desc  + [ShortKey1Desc] + ','
  FROM [ShortKey1DescTable] where [KEY 1] = 1  


Select [KEY 1], 
        substring(@AllKeys2ForKey1,1,len(@AllKeys2ForKey1) - 1) as 'AllKeys2ForKey1 ',
        substring(@AllDescriptionsForKey1,1,len(@AllDescriptionsForKey1) - 1) as 'AllDescriptionsForKey1',
        substring(@ShortKey1Desc,1,len(@ShortKey1Desc) - 1)  as 'ShortKey1Desc'
from Table where [KEY 1]= 1 
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-09-18 11:25

Assuming that you are unable to change the data structures to make a more efficient query, this will work:

--Populate sample data
SELECT 1 as key1,       2  as key2 INTO #tbl1
UNION ALL SELECT 1,       5 
UNION ALL SELECT 1,       6 
UNION ALL SELECT 1,       4 
UNION ALL SELECT 1,       8 

SELECT 1  as key1,     'Desc short' as shortkeydesc INTO #tbl2

SELECT 1   as key1,    'Desc a'  as [description] INTO #tbl3
UNION ALL SELECT 1,       'Desc c' 
UNION ALL SELECT 1,       'Desc aa' 
UNION ALL SELECT 1,       'Desc tt' 

--Combine data into semi-colon separated lists
SELECT 
key1
,STUFF(
    (
    SELECT
      ';' + CAST(t2.key2 AS VARCHAR(10))
    FROM #tbl1 t2
    WHERE t2.key1 = tbl1.key1
    FOR XML PATH('')
    ), 1, 1, ''
  )
,STUFF(
    (
    SELECT
      ';' + tbl2.shortkeydesc
    FROM #tbl2 tbl2
    WHERE tbl2.key1 = tbl1.key1
    FOR XML PATH('')
    ), 1, 1, ''
  )
,STUFF(
    (
    SELECT
      ';' + tbl3.[description]
    FROM #tbl3 tbl3
    WHERE tbl3.key1 = tbl1.key1
    FOR XML PATH('')
    ), 1, 1, ''
  )
FROM #tbl1 tbl1
GROUP BY tbl1.key1
查看更多
Rolldiameter
4楼-- · 2019-09-18 11:27

You Must Write CLR Aggregate Function for Solving This Question. for write CLR Aggregate Function : 1: Run Microsoft Visual Stadio 2: Create New Project 3: then Select Data Project 4: CLR Aggregate Function

After Create Your Aggregate Function Create Your Query Such as Below

 Select A.Key1, OwnAggregateFn(B.Description), OwnAggregateFn(C.Key2), ...
 From A
 inner join B ON B.Key1 = A.Key1
 inner join C ON C.Key1 = A.Key1
 ... 
 Group By A.Key1
查看更多
登录 后发表回答