-2

我们有 CurrencyRatetable 如下:

在此处输入图像描述

我们需要将该表水平排列为:

TransactionCurrency|EUR rate|INR Rate|USD Rate|Effectie Date
GBP                |0.893   |0.0115  |0.7992  |2019-06-18 00:00:00:000
INR                |78.0005 |1       |69.7242 |2019-06-18 00:00:00:000
USD                |1.1187  |0.0143  |1       |2019-06-18 00:00:00:000
EUR                |1       |0.0128  |0.8939  |2019-06-18 00:00:00:000
GBP                |0.8902  |0.0114  |0.7943  |2019-06-19 00:00:00:000
INR                |78.0755 |1       |69.6667 |2019-06-19 00:00:00:000
USD                |1.1207  |0.0144  |1       |2019-06-19 00:00:00:000
EUR                |1       |0.0128  |0.8923  |2019-06-19 00:00:00:000

等等

我有基本的 SQL 知识。PIVOT 功能可以做到这一点吗?查询的起点应该是什么?由于我的试验给出了错误。

select * from (SELECT  
      [BaseCurrency] ,
      [TransactionCurrency], 
      [EffectiveDate],
      [Rate]
  FROM [PDI].[dbo].[Dim_CurrencyRates]
  )cur
  PIVOT(
 
  FOR [BaseCurrency] in ('EUR','INR','USD')
  )As PivTab
4

1 回答 1

0

我经常max在不需要聚合的时候使用。

select 
    TransactionCurrency,
    EUR as [EUR rate],
    INR as [INR Rate],
    USD as [USD Rate],
    EffectiveDate
from [PDI].[dbo].[Dim_CurrencyRates]
pivot(
    max(Rate) 
    for BaseCurrency in (EUR, INR, USD)) as pv
于 2020-07-15T08:18:58.420 回答