Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接数据透视表,重命名列。SQL_Sql_Sql Server_Tsql_Join_Pivot - Fatal编程技术网

连接数据透视表,重命名列。SQL

连接数据透视表,重命名列。SQL,sql,sql-server,tsql,join,pivot,Sql,Sql Server,Tsql,Join,Pivot,我有下面的查询,它从包含日期、货币、mvcleanccy和spotsek的表中进行选择 问题1。如何从DKK、EUR重命名这些列。。。至DKK_MV、EUR_MV 问题2。我有相同的轴心,唯一的区别是'MV_SEK'=mvcleancy*spotsek被MV=mvcleancy取代。 如果我想在查询中连接位置日期上的这两个数据透视,如何在不创建两个单独的表并在其后连接的情况下进行连接 SELECT * FROM( SELECT currency ,'MV_SEK' = mvc

我有下面的查询,它从包含日期、货币、mvcleanccy和spotsek的表中进行选择

问题1。如何从DKK、EUR重命名这些列。。。至DKK_MV、EUR_MV

问题2。我有相同的轴心,唯一的区别是
'MV_SEK'=mvcleancy*spotsek
MV=mvcleancy
取代。 如果我想在查询中连接位置日期上的这两个数据透视,如何在不创建两个单独的表并在其后连接的情况下进行连接

SELECT *
FROM(
SELECT 
    currency 
    ,'MV_SEK' = mvcleanccy*spotsek
    ,todaypositiondate
from T1
) as src
PIVOT
(
 sum(MV_SEK)
for
currency in ([DKK], [EUR], [NOK], [SEK], [USD])
)
as pivottable
Order by todaypositiondate desc

我认为通过条件聚合,您的解决方案会更简单:

select todaypositiondate,
       sum(case when currency = 'DKK' then mvcleanccy * spotsek end) as dkk_mv,
       sum(case when currency = 'EUR' then mvcleanccy * spotsek end) as eur_mv,
       . . .
       sum(case when currency = 'DKK' then mvcleanccy end) as dkk,
       sum(case when currency = 'EUR' then mvcleanccy end) as eur,
       . . .
from t1
group by todaypositiondate
order by todaypositiondate;

我认为通过条件聚合,您的解决方案会更简单:

select todaypositiondate,
       sum(case when currency = 'DKK' then mvcleanccy * spotsek end) as dkk_mv,
       sum(case when currency = 'EUR' then mvcleanccy * spotsek end) as eur_mv,
       . . .
       sum(case when currency = 'DKK' then mvcleanccy end) as dkk,
       sum(case when currency = 'EUR' then mvcleanccy end) as eur,
       . . .
from t1
group by todaypositiondate
order by todaypositiondate;

问题2完全不清楚您使用的是哪种DBMS?您的查询是非标准SQL.SQL 2008 R2(SP1)。。。以前我知道“SQL”是一种查询语言,而不是DBMS产品(因此没有产品“SQL 2008”)。但我猜您指的是“SQLServer2008”,问题2完全不清楚您使用的是哪种DBMS?您的查询是非标准SQL.SQL 2008 R2(SP1)。。。以前我知道“SQL”是一种查询语言,而不是DBMS产品(因此没有产品“SQL 2008”)。但我猜您指的是“SQL Server 2008”