Giter Club home page Giter Club logo

preppn-challenge-2023-week-3's Introduction

Preppn-Challenge-2023-Week-3

Solving Tableau Prep Challenge 2023 Week 3 using SQL/Snowflake

TASK

2023 Week 3 - Data Source Bank has some quarterly targets for the value of transactions that are being performed in-person and online. It's our job to compare the transactions to these target figures.

  • The Transaction Table.

    i. Filter to just look at DSB Transactions.

    ii. Rename the values in the Online or In-person field, Online of the 1 values and In-Person for the 2 values

    iii. Change the date to be the quarter and sum the transaction values for each quarter and for each Type of Transaction

  • The Transaction Table.

    i. Pivot the quaterly targets.

    ii. Remove the 'Q' from the quarter field and make the data type numeric

  • Join the two datasets (on more than one join clause) and calculate the Variance to Target.

SQL/Snowflake Techniques Used

  1. Common Table Expressions and multi-key Joins
  2. Aggregation and column type conversion
  3. Queries with constraints
  4. Filtering and sorting

Solution

1. Prepping the Transaction Table

There are multiple ways to filter the transaction values, in the below case, I have used CONTAIN() function to restrict to transactions that contained DSB in the Transaction_Code column. Case statement replaces the values in the Online or In-person column and Date_part gives the corresponding quarter for the transaction date. For aggregation, I have grouped on Online or In-Person and Quarter (Q) column as asked in the task.

            SELECT  SUM (Value) as V1,
                    CASE
                            WHEN online_or_in_person = 2
                                THEN 'In-Person'
                            WHEN online_or_in_person = 1
                                THEN 'Online'
                                    END as online_or_in_person,
                    DATE_PART ('quarter', DATE(LEFT(TRANSACTION_DATE, 10), 'dd/MM/yyyy')) as Q
                        FROM PD2023_WK01
                            WHERE CONTAINS(TRANSACTION_CODE, 'DSB')
                                GROUP BY 2,3
                                ORDER BY  online_or_in_person, Q
                        ;

2. Prepping the Target Table

While in the challenge it says to Pivot the table, in here I had to use UNPIVOT function to achieve the desired format. It is similar to the 'Transpose' tool in Alteryx.

            SELECT t.ONLINE_OR_IN_PERSON, REPLACE(Month, 'Q', '')::int as Q, Quarterly_Targets, trans.V1,
                    Trans.V1 - Quarterly_Targets as Var
                        FROM PD2023_WK03_TARGETS as t
                            UNPIVOT (Quarterly_Targets FOR month IN (Q1, Q2, Q3, Q4))
                        ;

3. Joining the tables

I have used Common Table Expressions (CTE) to temporarily store the results of Transaction table query and called it "Trans". It will make joining the two tables easier. I can simply join Trans on Target table query on Online or In-person and Quarter columns

    WITH Trans AS(
                    SELECT  SUM (Value) as V1,
                            CASE
                                WHEN online_or_in_person = 2
                                        THEN 'In-Person'
                                WHEN online_or_in_person = 1
                                        THEN 'Online'
                                            END as online_or_in_person,
                            DATE_PART ('quarter', DATE(LEFT(TRANSACTION_DATE, 10), 'dd/MM/yyyy')) as Q

                                FROM PD2023_WK01
                                        WHERE CONTAINS(TRANSACTION_CODE, 'DSB')
                                            GROUP BY 2,3
                                            ORDER BY  online_or_in_person, Q
        )

                    SELECT t.ONLINE_OR_IN_PERSON, REPLACE(Month, 'Q', '')::int as Q, Quarterly_Targets, trans.V1,
                            Trans.V1 - Quarterly_Targets as Var
                                FROM PD2023_WK03_TARGETS as t
                                    UNPIVOT (Quarterly_Targets FOR month IN (Q1, Q2, Q3, Q4))
                                            JOIN Trans
                                                ON t.ONLINE_OR_IN_PERSON = Trans.online_or_in_person
                                                AND REPLACE(Month, 'Q', '')::int = Q
                                ;

The output:

preppn-challenge-2023-week-3's People

Contributors

jyotivgupta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.