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

我们两清 提交于 2019-12-11 17:27:35

问题


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


回答1:


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



回答2:


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:


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


来源:https://stackoverflow.com/questions/10597387/join-three-tables-and-aggregate-data-from-multiple-rows-for-every-distinct-row-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!