从同一列中的其他数据填写值(Teradata)

时间:2018-05-02 15:03:50

标签: teradata teradata-sql-assistant

我有一个表是查询的结果(它是一个很大的!),看起来像这样。

表尝试按类型计算每个id中的名称集(seq_num 1,2 ..),但是当不同类型出现在同一个id中时它会保留零

我希望得到一个看起来像这样的结果。

This is the result that I would like to have.

不能使用子查询或最大值,因为这会因为查询已经过于复杂而需要由此组成的组。

非常感谢这里的一些帮助。 谢谢!

查询最右边4列以上的代码:

<div class="holder">
	<div class="title">
		<h2>1 line of text 1 line of text</h2>
	</div>
	<div class="title">
		<h2>2 lines of text 2 lines of text 2 lines of text 2 lines of text 2 lines of text</h2>
	</div>
	<div class="title">
		<h2>3 lines of text 3 lines of text 3 lines of text 3 lines of text 3 lines of text 3 lines of text 3 lines of text</h2>
	</div>
	<div class="title">
		<h2>4 lines of text 4 lines of text 4 lines of text 4 lines of text 4 lines of text 4 lines of text 4 lines of text 4 lines of text 4 lines of text 4 lines of text</h2>
	</div>
	<div class="title">
		<h2>5 lines of text 5 lines of text 5 lines of text 5 lines of text 5 lines of text 5 lines of text 5 lines of text 5 lines of text 5 lines of text 5 lines of text 5 lines of text 5 lines of text</h2>
	</div>
	<div class="title">
		<h2>6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text 6 lines of text</h2>
	</div>
</div>

注意:我有其他ID,TYPE没有改变,当它工作正常时,我可以理解为什么会这样。问题在于每个ID的信息都非常不同。

1 个答案:

答案 0 :(得分:0)

您想要一个COUNT(DISTINCT Name) OVER (PARTITION BY ID, TYPE),这在Teradata中不受支持。

最有效的方法是嵌套的OLAP函数,这将导致两个STAT步骤,就像您当前的解决方案一样,所以这不应该效率低下:

SELECT dt.*,
    Max(CASE WHEN type = 'E' THEN nc END) Over (PARTITION BY id) AS NC_E
   ,Max(CASE WHEN type = 'M' THEN nc END) Over (PARTITION BY id) AS NC_M 
   ,Max(CASE WHEN type = 'D' THEN nc END) Over (PARTITION BY id) AS NC_D 
   ,Max(CASE WHEN type = 'C' THEN nc END) Over (PARTITION BY id) AS NC_C 
FROM
 ( 
   SELECT ....
      Dense_Rank() Over (PARTITION BY ID, TYPE_ ORDER BY NAME) AS nc
   FROM ...
 ) AS dt