GithubHelp home page GithubHelp logo

data_3.05_activities's Introduction

3 05 Activity 1 to 3

1. Activity:

1.1. Find out the average number of transactions by account.

  • Get those accounts that have more transactions than the average.

Answer:

CREATE TEMPORARY TABLE avg_count_trans
SELECT
    avg(avg_no_of_transactions_by_account) AS avg_count_trans
FROM
    (
        SELECT
            (
                SELECT
                    avg(count(account_id))
            ) AS avg_no_of_transactions_by_account
        FROM
            trans
        GROUP BY
            account_id
    ) AS t;
151272176 8d4bdeb2 53ed 4b35 9309 739d93eb8e0b
SELECT
    account_id,
    (
        SELECT
            floor(avg(count(account_id)))
    ) AS equal_and_less_than_avg
FROM
    trans
GROUP BY
    1
HAVING
    equal_and_less_than_avg <= (
        SELECT
            *
        FROM
            avg_count_trans
    )
ORDER BY
    2 DESC;
151272367 3a368f8e cd88 4b60 a38a 31d3758cb03d
SELECT
    account_id,
    (
        SELECT
            floor(avg(count(account_id)))
    ) AS no_trans_above_avg
FROM
    trans
GROUP BY
    1
HAVING
    no_trans_above_avg > (
        SELECT
            *
        FROM
            avg_count_trans
    )
ORDER BY
    2 DESC;
151272521 e060de56 ee1a 4479 bbff 05969254e7a1

First I calculate the average number of transactions by account, then calculate the total average which is 192. I have separated the output into accounts with a number of transactions equal or below average and accounts with a number of transactions above average.

If I display only the average number of transactions by account the output would be the averages per account from 6 to 548. This would have been redundant as the second part of the question asked me to display accounts that have more transactions than the average.

2. Activity:

2.1. Get a list of accounts from Central Bohemia using a subquery.

Answer:

SELECT
    DISTINCT account_id
FROM
    account
WHERE
    district_id IN (
        SELECT
            DISTINCT A1
        FROM
            district
        WHERE
            A3 = 'central Bohemia'
    );
151357181 cd2990ee 1755 4cbd b8a2 d71cecb79053
  • Rewrite the previous as a join query.

Answer:

SELECT
    account_id,
    a3 AS region
FROM
    `account` a
    INNER JOIN district d ON a.district_id = d.a1
WHERE
    a3 = 'central Bohemia';
151358904 7dc52fda 9c9e 4749 a39e 53f67aca6be1
  • Discuss which method will be more efficient.

3. Activity:

3.1. Find the most active customer for each district in Central Bohemia

Answer:

SELECT
    account_id,
    count(trans_id) AS num_of_transactions
FROM
    trans
WHERE
    account_id IN (
        SELECT
            account_id
        FROM
            account
        WHERE
            district_id IN (
                SELECT
                    a1
                FROM
                    district
                WHERE
                    a3 = 'central Bohemia'
            )
    )
GROUP BY
    1
ORDER BY
    2 DESC
LIMIT
    1;
151362661 b3ee5a0f c484 47ab b894 6505f08f82c9


data_3.05_activities's People

Contributors

jecastrom avatar sandrabosk avatar

Stargazers

 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.