【问题标题】:Converting Rows to column values - Return single row将行转换为列值 - 返回单行
【发布时间】:2014-11-19 23:33:08
【问题描述】:

我是 SQL 编程的新手,我正在编写一份关于我们的财务过帐的报告。

帖子使用由 1 到多个维度组合而成的字符串进行排序。这些维度存储在数据库中,我希望能够将字符串分成每个财务行的列。

DimensionAttributeLevelValueAllView 包含维度的字符串,但它被分隔成行

Displayvalue  
703310
5022
PEN

我尝试使用 case-when 结构将值过滤到列中,但随后它会生成单独的行:

Account  | Department  | Misc
703310 
         | 5022
                       | PEN

代替

Account  | Department  | Misc
703310   | 5022        | PEN

下面的查询应该将上面的维度与相应的已发布行组合在一起,但它会像上面一样创建一个单独的行,每行具有相同的财务金额。

SELECT
    Case When B.ValueOrdinal='1' Then B.Displayvalue end as 'Account',
    case when B.ValueOrdinal='2' then B.Displayvalue end as 'Department',
    case when B.ValueOrdinal='3' then B.Displayvalue end as 'Misc',
    A.Text,
    Sum(A.reportingcurrencyamount) as 'Posted Amount',
    A.Recid

From GeneralJournalAccountEntry A
    Inner Join DimensionAttributeLevelValueAllView B on A.Ledgerdimension = B.ValueCombinationRecID
    Inner Join DIMENSIONATTRIBUTEVALUECOMBINATION C on A.Ledgerdimension = C.RecID

Where C.accountstructure in ('5637145326','5637165585')

Group by A.Recid, B.Valueordinal, B.Displayvalue, A.Text

我已经考虑过动态 Pivot 功能,但我似乎不知道如何为此使用它。

提前致谢

【问题讨论】:

  • 你应该注意你的partition字段。

标签: sql sql-server axapta dynamics-ax-2012


【解决方案1】:

您聚合的字段太多。试试这个:

SELECT MAX(Case When B.ValueOrdinal='1' Then B.Displayvalue end) as Account,
       MAX(case when B.ValueOrdinal='2' then B.Displayvalue end) as Department,
       MAX(case when B.ValueOrdinal='3' then B.Displayvalue end) as Misc,
       A.Text,
       Sum(A.reportingcurrencyamount) as PostedAmount,
       A.Recid
From GeneralJournalAccountEntry A Inner Join
     DimensionAttributeLevelValueAllView B
     on A.Ledgerdimension = B.ValueCombinationRecID Inner Join
     DIMENSIONATTRIBUTEVALUECOMBINATION C
     on A.Ledgerdimension = C.RecID
Where C.accountstructure in ('5637145326','5637165585')
Group by A.Recid,  A.Text;

我不鼓励您对列别名使用单引号。仅对字符串和日期常量使用单引号。一般情况下,只需尝试为列名称提供根本不需要任何转义字符的列名(例如,不要在名称中添加空格)。

【讨论】:

  • 非常感谢!我原以为是团队造成了问题,只是我自己无法让它工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多