使用不同的数据变量连接两个SQL查询

时间:2016-08-21 23:17:31

标签: sql sql-server join

我有这个查询返回一些数据:

select 
    id, name, username, email, password, 
    first_name, last_name, usertype,
    block, sendemail, registerDate, lastvisitDate,
    activation, params, uuid 
from 
    jml2_users 
where 
    uuid in ('51840915-e570-430d-9911-7247d076f6e7', '51912193-6694-4ca5-94c9-9f31d076f6e7',
             '51927ada-6370-4433-8a06-30d2d076f6e7', '51c05ad7-d1d0-4eb6-bc6b-424bd076f6e7',
             'd047adf1-a6af-891e-94a2d0b225dcd1b6', '2aba38f2-d7a7-0a7a-eff2be3440e3b763')

,另一个查询就是这个

SELECT 
    ct.TrainingID, ct.UserID, ct.TrainingType, ct.TrainingStatus, 
    ct.TrainingScore, ct.TrainingDate, 
    dbo.fn_StripCharacters(ctt.product_type,'^a-z0-9') as product_type, 
    ctt.product_type as oldName 
FROM 
    clientTraining as ct 
INNER JOIN 
    clientTraningTypes as ctt ON ct.TrainingType = ctt.TypeID 
WHERE 
    1=1 
    AND UserID IN ('51840915-e570-430d-9911-7247d076f6e7', '51927ada-6370-4433-‌​8a06-30d2d076f6e7') 
    AND TrainingType IN (SELECT TypeID 
                         FROM complaincetestlinks 
                         WHERE parent_client_id = 1039  
                           AND isactive = 1 AND isdeleted = 0)

两个查询都返回不同的结果,两个表中的userid和uuid都有相同的数据,我的意思是我们可以做一个连接,但问题是:我希望第二个查询数据是行应该转换为新的列查询和数据应该复制到新查询,并基于userid和uuid

连接到第二个查询

我是SQL的noob,第一个问题是我如何将第一个查询行数据转换为列并用数据填充它,因为第一个查询有5行。所以最终我需要5 * 4 = 20列用于新查询以及要传递的数据并从第二个查询上传到新查询

不知道我需要做什么,我迷路了

enter image description here

1 个答案:

答案 0 :(得分:0)

以下是如何在product_type上透视提供的查询的示例。我只做了一些专栏。希望您可以按照示例添加更多内容。

SELECT
TrainingID, 
UserID, 
TrainingType, 
TrainingStatus, 
MAX(CASE 
   WHEN product_type = 'MarketLinkedCDs'  THEN TrainingDate ELSE NULL 
END)  TrainingDate_MarketLinkedCDs,
MAX(CASE 
   WHEN product_type = 'StructuredNotes'  THEN TrainingDate ELSE NULL 
END)  TrainingDate_StructuredNotes,
MAX(CASE 
   WHEN product_type = 'BufferedRangeAccrualNotes'  THEN TrainingDate ELSE NULL 
END)  TrainingDate_BufferedRangeAccrualNotes,

MAX(CASE 
   WHEN product_type = 'MarketLinkedCDs'  THEN TrainingScore ELSE NULL 
END)  TrainingScore_MarketLinkedCDs,
MAX(CASE 
   WHEN product_type = 'StructuredNotes'  THEN TrainingScore ELSE NULL 
END)  TrainingScore_StructuredNotes,
MAX(CASE 
   WHEN product_type = 'BufferedRangeAccrualNotes'  THEN TrainingScore ELSE NULL 
END)  TrainingScore_BufferedRangeAccrualNotes

FROM
(
    SELECT ct.TrainingID, ct.UserID, ct.TrainingType, ct.TrainingStatus, 
    ct.TrainingScore, 
    ct.TrainingDate, dbo.fn_StripCharacters(ctt.product_type,'^a-z0-9') as product_type, 
    ctt.product_type as oldName FROM clientTraining as ct INNER JOIN 
    clientTraningTypes as ctt ON ct.TrainingType = ctt.TypeID WHERE 1=1 and 
    UserID in ('51840915-e570-430d-9911-7247d076f6e7',
    '51927ada-6370-4433-‌​8a06-30d2d076f6e7') 
    and TrainingType IN (select TypeID from complaincetestlinks where 
    parent_client_id = 1039 and isactive = 1 and isdeleted = 0)
) F

GROUP BY
TrainingID, 
UserID, 
TrainingType, 
TrainingStatus,