使用不同的数据变体连接两个SQL查询
我有一个返回一些数据的查询:使用不同的数据变体连接两个SQL查询,sql,sql-server,join,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-
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,第一个问题是如何将第一个查询行数据转换为列并用数据填充,因为第一个查询有5行。因此,最终我需要5*4=20列用于新查询,以及从第二个查询传递并上传到新查询的数据
不知道我在这里需要做什么,我迷路了
下面是一个示例,说明如何根据产品类型透视提供的查询。我只写了一些专栏。希望您可以按照示例添加更多内容
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,
下面是一个示例,说明如何根据产品类型透视提供的查询。我只写了一些专栏。希望您可以按照示例添加更多内容
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,
第二个查询中所需的列数是否固定不变?新专栏会出现吗?这是一个交叉表或轴心问题。所以一天大约有六个。因此,您的第一步是在T-SQLi中查找如何做pivot,我真的不知道pivot是什么,我想在这个项目结束后,我会环顾一下它,是的,数据可能会改变,比如行会增加或减少,但列会保持不变,因为第二个querypivot是您在google中键入的关键字,以找到您的答案。像这样:。您需要正确地解释您需要什么,并提供示例输出,或者至少告诉我们哪些列需要进行数据透视。您丢失的一半原因可能是因为您不知道输出会是什么样子。如果您提供示例输出,您可能会少一些损失。请检查此图像:。我想将行转换为列,但可以忽略列:trainingID、userID、TrainingType。转换的每一行都应该有状态、分数、日期、旧名称,因为不同的行。你的图像不会告诉我任何新的信息(而且图像会随着时间的推移而消失)。图像只是您的第二个查询。请发布您想要的输出示例。i、 e.列出实际列第二次查询中所需的列数是否固定不变?新专栏会出现吗?这是一个交叉表或轴心问题。所以一天大约有六个。因此,您的第一步是在T-SQLi中查找如何做pivot,我真的不知道pivot是什么,我想在这个项目结束后,我会环顾一下它,是的,数据可能会改变,比如行会增加或减少,但列会保持不变,因为第二个querypivot是您在google中键入的关键字,以找到您的答案。像这样:。您需要正确地解释您需要什么,并提供示例输出,或者至少告诉我们哪些列需要进行数据透视。您丢失的一半原因可能是因为您不知道输出会是什么样子。如果您提供示例输出,您可能会少一些损失。请检查此图像:。我想将行转换为列,但可以忽略列:trainingID、userID、TrainingType。转换的每一行都应该有状态、分数、日期、旧名称,因为不同的行。你的图像不会告诉我任何新的信息(而且图像会随着时间的推移而消失)。图像只是您的第二个查询。请发布您想要的输出示例。i、 e.列出实际列