# Hello everyone, I am new to Matillion

**URL:** <https://matillioncommunity.discourse.group/t/hello-everyone-i-am-new-to-matillion/1352>\
**Category:** Matillion ETL\
**Created:** [May 27, 2021, 1:37pm UTC](https://matillioncommunity.discourse.group/t/hello-everyone-i-am-new-to-matillion/1352 "2021-05-27T13:37:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ImmanuelHasselmann](https://avatars.discourse-cdn.com/v4/letter/i/e36b37/32.png) [@ImmanuelHasselmann](https://matillioncommunity.discourse.group/u/ImmanuelHasselmann)\
**Post date:** [May 27, 2021, 1:37pm UTC](https://matillioncommunity.discourse.group/t/hello-everyone-i-am-new-to-matillion/1352/1 "2021-05-27T13:37:43Z")

</div>

I am currently trying to transform a table.

&nbsp;

 ![0694G00000EtBPPQA3_0D54G00007i82lOSAQ](https://us1.discourse-cdn.com/flex002/uploads/matillioncommunity/original/1X/233b15c2c2356cc8e4df58ec1e8cd436f6b946ae.png)

I would like to get the following result.

&nbsp;

 ![0694G00000EtBHEQA3_0D54G00007i82lOSAQ](https://us1.discourse-cdn.com/flex002/uploads/matillioncommunity/original/1X/3d845d3d857c6216b817a3b038f56237bf67d509.png)

Can anyone help me with this?

---

<div class="post-metadata">

**Author:** ![Bryan](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@Bryan](https://matillioncommunity.discourse.group/u/Bryan)\
**Post date:** [May 28, 2021, 3:32pm UTC](https://matillioncommunity.discourse.group/t/hello-everyone-i-am-new-to-matillion/1352/2 "2021-05-28T15:32:54Z")

</div>

Hi [@ImmanuelHasselmann](https://matillioncommunity.discourse.group/u/ImmanuelHasselmann)​,

Without more information I can't say for sure but it appears you want to pivot the data based on either the StartDate or EndDate. The approach is a little different depending on the warehouse product you are using (Snowflake, BigQuery, Redshift, etc.) You can do this by writing the SQL statement by hand or you can use a Matillion Transformation where you take the base table and apply the pivot transformation to it before landing the data in the destination table. I hope this helps!

---

<div class="post-metadata">

**Author:** ![Henry](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Henry](https://matillioncommunity.discourse.group/u/Henry)\
**Post date:** [June 1, 2021, 10:32am UTC](https://matillioncommunity.discourse.group/t/hello-everyone-i-am-new-to-matillion/1352/3 "2021-06-01T10:32:28Z")

</div>

Hi Immanuel,

&nbsp;

As Bryan mentioned, this looks like a densification pivot. On Snowflake you can take advantage of the [GENERATOR](https://docs.snowflake.com/en/sql-reference/functions/generator.html) function to perform the densification, and the [LAST\_VALUE](https://docs.snowflake.com/en/sql-reference/functions/last_value.html) function for the most-recent lookup.

&nbsp;

If your data is in a table named **manufacturer** , something like this:

&nbsp;

**WITH spine AS (SELECT TO\_DATE('20141231', 'YYYYMMDD')**

**+ ROW\_NUMBER() OVER (ORDER BY SEQ8()) AS mark\_date**

**FROM TABLE(GENERATOR(ROWCOUNT=\>20000))),**

**limiter AS (SELECT MIN(start\_date) AS start\_date, MAX(end\_date) AS end\_date FROM manufacturer),**

**codes AS (SELECT DISTINCT manufacturer FROM manufacturer)**

**SELECT spine.mark\_date,**

**codes.manufacturer,**

**LAST\_VALUE(actual.condition\_value IGNORE NULLS) OVER (PARTITION BY codes.manufacturer ORDER BY spine.mark\_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)**

**FROM spine**

**FULL JOIN limiter**

**FULL JOIN codes**

**LEFT JOIN manufacturer actual ON actual.manufacturer = codes.manufacturer AND actual.start\_date = spine.mark\_date**

**WHERE spine.mark\_date \>= limiter.start\_date**

**AND spine.mark\_date \<= limiter.end\_date**

**ORDER BY spine.mark\_date, codes.manufacturer**

&nbsp;

Henry

&nbsp;

---

<div class="post-metadata">

**Author:** ![ImmanuelHasselmann](https://avatars.discourse-cdn.com/v4/letter/i/e36b37/32.png) [@ImmanuelHasselmann](https://matillioncommunity.discourse.group/u/ImmanuelHasselmann)\
**Post date:** [June 1, 2021, 1:38pm UTC](https://matillioncommunity.discourse.group/t/hello-everyone-i-am-new-to-matillion/1352/4 "2021-06-01T13:38:09Z")

</div>

Unfortunately, I get the wrong result with the code. I want the sum of the valid conditions per date.&nbsp;

---

<div class="post-metadata">

**Author:** ![Henry](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Henry](https://matillioncommunity.discourse.group/u/Henry)\
**Post date:** [June 1, 2021, 4:27pm UTC](https://matillioncommunity.discourse.group/t/hello-everyone-i-am-new-to-matillion/1352/5 "2021-06-01T16:27:16Z")

</div>

OK, I think it's a little simpler then.. you just SUM the values that are within range per code and date.

&nbsp;

**WITH spine AS (SELECT TO\_DATE('20141231', 'YYYYMMDD')**

**+ ROW\_NUMBER() OVER (ORDER BY SEQ8()) AS mark\_date**

**FROM TABLE(GENERATOR(ROWCOUNT=\>20000))),**

**limiter AS (SELECT MIN(start\_date) AS start\_date, MAX(end\_date) AS end\_date FROM manufacturer),**

**codes AS (SELECT DISTINCT manufacturer FROM manufacturer)**

**SELECT spine.mark\_date,**

**codes.manufacturer,**

**SUM(m.condition\_value)**

**FROM spine**

**JOIN limiter ON spine.mark\_date \>= limiter.start\_date AND spine.mark\_date \<= limiter.end\_date**

**FULL JOIN codes**

**LEFT JOIN manufacturer m ON m.manufacturer = codes.manufacturer AND m.start\_date \<= spine.mark\_date AND m.end\_date \>= spine.mark\_date**

**GROUP BY spine.mark\_date, codes.manufacturer**

**ORDER BY 1,2;**

&nbsp;

Henry
