GithubHelp home page GithubHelp logo

jyotivgupta / preppn-challenge-2023-week-4 Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 62 KB

Solving Tableau Prep challenge 2023 Week 4 using SQL/Snowflake

Home Page: https://preppindata.blogspot.com/2023/01/2023-week-4-new-customers.html

data-prep data-preparation-and-analysis sql pivot-tables

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

Preppn-Challenge-2023-Week-4

Solving Tableau Prep challenge 2023 Week 4 using SQL/Snowflake

TASK

2023 Week 4 - Data Source Bank acquires new customers every month. They are stored in separate tabs of an Excel workbook. We'd like to consolidate all the months into one dataset, to do comparisons between months.

  • Stack the tables on top of one another i.e. Union them all.

  • Make a Joining Date field based on the Joining Day, Table Names and the year 2023.

  • Reshape the data so there is a field for each demographic, for each new customer.

  • Remove duplicates.

SQL/Snowflake Techniques Used

  1. Common Table Expressions
  2. Pivot and column type-conversion
  3. UNION ALL
  4. Removing Duplicates

Solution

1. Union the Tables

As all the tables had same fields, I used UNION ALL to union the tables. I created an extra column called 'table name' and record the month number the table belonged to in this column. I stored this new table in

            WITH C as

                    (SELECT *, '01' as table_name
                                    FROM PD2023_WK04_JANUARY
                            UNION ALL
                    SELECT *, '02' as table_name
                                    FROM PD2023_WK04_FEBRUARY

                            UNION ALL
                    SELECT *, '03' as table_name
                                    FROM PD2023_WK04_MARCH

                            UNION ALL
                    SELECT *, '04' as table_name
                                    FROM PD2023_WK04_APRIL

                            UNION ALL
                    SELECT *, '05' as table_name
                                    FROM PD2023_WK04_MAY

                            UNION ALL
                    SELECT *, '06' as table_name
                                    FROM PD2023_WK04_JUNE

                            UNION ALL
                    SELECT *, '07' as table_name
                                    FROM PD2023_WK04_JULY
                            UNION ALL
                    SELECT *, '08' as table_name
                                    FROM PD2023_WK04_AUGUST

                            UNION ALL
                    SELECT *, '09' as table_name
                                    FROM PD2023_WK04_SEPTEMBER

                            UNION ALL
                    SELECT *, '10' as table_name
                                    FROM PD2023_WK04_OCTOBER
                            UNION ALL
                    SELECT *, '11' as table_name
                                    FROM PD2023_WK04_NOVEMBER
                            UNION ALL
                    SELECT *, '12' as table_name
                                    FROM PD2023_WK04_DECEMBER

)

2. Creating Joining date and Reshaping the data

Used Date_Part to create th date from the three columns (2023, Table name and Joining Day). Then Pivoted the data using CTE and PIVOT function

            Pre As (
                    SELECT   ID,
                            date_from_parts(2023, DATE_PART('month', Date(table_name, 'Auto')), Joining_Day) as Joining_date,
                            demographic, value
                                    FROM C ),

            POST AS(

                    SELECT
                            ID,
                            Joining_date,
                            ethnicity, account_type,
                            date_of_birth :: DATE as date_of_birth,
                                ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Joining_date) as rn
                                    FROM PRE

                                        PIVOT (MAX(Value) FOR Demographic IN ('Ethnicity', 'Account Type', 'Date of Birth')) As P
                                                    (ID, Joining_date, ethnicity,account_type, date_of_birth)
)

3. Removing the duplicates

In the Post CTE, I created a column 'rn' which gives a row number for each row restarting at every ID. That means for duplicate IDs row number will be 1, 2,.. and so on while for Distinct IDs it will be just 1. To remove duplicate rows, simply keep rows with rn=1

                     SELECT ID,
                            Joining_date,
                            ethnicity,
                            account_type,
                            date_of_birth
                                    FROM POST
                                        where rn = 1 ;

The output:

preppn-challenge-2023-week-4'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.