Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 186182
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T18:31:42+00:00 2022-06-10T18:31:42+00:00

mysql – Select country has the highest account balance

  • 0

[ad_1]

I am new to SQL.I have two tables Customers and deposit_transactions. I am trying to select the country which has the highest account balance as of latest date. I am joining deposit_transactions table(to get the sum of amount based on customer_id) and joining with customers table to get the Country

Schema (MySQL v8.0)

CREATE TABLE deposit_transactions (
  `deposit_id` VARCHAR(7),
  `customer_id` VARCHAR(5),
  `date` TEXT,
  `transaction_type` VARCHAR(7),
  `amount` INTEGER,
  `currency` VARCHAR(3)
);

INSERT INTO deposit_transactions
  (`deposit_id`, `customer_id`, `date`, `transaction_type`, `amount`, `currency`)
VALUES
  ('DE47653', 'DO900', '1/1/2019', 'pay_in', '10000', 'EUR'),
  ('DE47654', 'DO901', '2/1/2019', 'pay_in', '10000', 'EUR'),
  ('DE47655', 'DO902', '2/1/2019', 'pay_in', '10000', 'EUR'),
  ('UK47656', 'DO903', '3/1/2019', 'pay_in', '10000', 'GBP'),
  ('UK47657', 'DO904', '3/1/2019', 'pay_in', '130000', 'GBP'),
  ('DE47658', 'DO905', '3/1/2019', 'pay_in', '140000', 'EUR'),
  ('DE47659', 'DO906', '3/1/2019', 'pay_in', '10000', 'EUR'),
  ('DE47660', 'DO907', '3/1/2019', 'pay_in', '10000', 'EUR'),
  ('DE47661', 'DO908', '3/1/2019', 'pay_in', '100000', 'EUR'),
  ('DE47662', 'DO909', '4/1/2019', 'pay_in', '10000', 'EUR'),
  ('UK47663', 'DO910', '4/1/2019', 'pay_in', '10000', 'GBP'),
  ('UK47664', 'DO911', '4/1/2019', 'pay_in', '5000', 'GBP'),
  ('UK47665', 'DO912', '4/1/2019', 'pay_in', '4000', 'GBP'),
  ('DE47666', 'DO913', '5/1/2019', 'pay_in', '10000', 'EUR'),
  ('DE47661', 'DO908', '5/1/2019', 'pay_out', '50000', 'EUR'),
  ('DE47667', 'DO914', '11/1/2019', 'pay_in', '30000', 'EUR'),
  ('DE47668', 'DO915', '11/1/2019', 'pay_in', '10000', 'EUR'),
  ('DE47669', 'DO916', '11/1/2019', 'pay_in', '25000', 'EUR'),
  ('DE47670', 'DO917', '11/1/2019', 'pay_in', '50000', 'EUR'),
  ('DE47667', 'DO914', '12/1/2019', 'pay_out', '7000', 'EUR'),
  ('DE47667', 'DO914', '12/15/2019', 'pay_in', '12000', 'EUR'),
  ('DE47671', 'DO918', '1/1/2021', 'pay_in', '9000', 'EUR'),
  ('DE47672', 'DO919', '1/1/2021', 'pay_in', '10000', 'EUR'),
  ('DE47673', 'DO920', '1/1/2021', 'pay_in', '11000', 'EUR'),
  ('DE47674', 'DO921', '1/1/2021', 'pay_in', '12000', 'EUR'),
  ('DE47675', 'DO922', '1/1/2021', 'pay_in', '13000', 'EUR'),
  ('DE47676', 'DO923', '1/1/2021', 'pay_in', '14000', 'EUR'),
  ('DE47677', 'DO924', '1/1/2021', 'pay_in', '30000', 'EUR'),
  ('DE47678', 'DO925', '1/1/2021', 'pay_in', '16000', 'EUR');

CREATE TABLE customers (
  `customer_id` VARCHAR(5),
  `country` VARCHAR(7)
);

INSERT INTO customers
  (`customer_id`, `country`)
VALUES
  ('DO900', 'Germany'),
  ('DO901', 'Germany'),
  ('DO902', 'Spain'),
  ('DO903', 'UK'),
  ('DO904', 'UK'),
  ('DO905', 'Austria'),
  ('DO906', 'Germany'),
  ('DO907', 'Germany'),
  ('DO908', 'Germany'),
  ('DO909', 'Germany'),
  ('DO910', 'UK'),
  ('DO911', 'UK'),
  ('DO912', 'UK'),
  ('DO913', 'Germany'),
  ('DO914', 'Austria'),
  ('DO915', 'Germany'),
  ('DO916', 'Austria'),
  ('DO917', 'Germany'),
  ('DO918', 'Germany'),
  ('DO919', 'Spain'),
  ('DO920', 'Germany'),
  ('DO921', 'Spain'),
  ('DO922', 'Germany'),
  ('DO923', 'Germany'),
  ('DO924', 'Germany'),
  ('DO925', 'Spain'),
  ('DO926', 'Germany'),
  ('DO927', 'Germany'),
  ('DO928', 'Germany'),
  ('DO929', 'Germany'),
  ('DO711', 'UK'),
  ('DO712', 'UK'),
  ('DO713', 'Germany'),
  ('DO714', 'Austria'),
  ('DO715', 'Germany'),
  ('DO716', 'Austria'),
  ('DO717', 'Germany'),
  ('DO718', 'Germany'),
  ('DO719', 'Spain'),
  ('DO720', 'Germany'),
  ('DO721', 'Spain'),
  ('DO722', 'Germany'),
  ('DO723', 'Germany'),
  ('DO724', 'Germany'),
  ('DO725', 'Spain'),
  ('DO726', 'Germany'),
  ('DO727', 'Germany'),
  ('DO728', 'Germany'),
  ('DO729', 'Germany');

Below is the sample query I created. But I am not sure if its correct

Query

SELECT t.customer_id,
       c.country,t.total_amount
FROM  (SELECT customer_id,
              date,
              transaction_type,
              Sum(amount)
                OVER (
                  partition BY customer_id ) AS total_amount_transfered,
              Row_number()
                OVER (
                  partition BY customer_id
                  ORDER BY date DESC)        AS n
       FROM   deposit_transactions)t
      INNER JOIN customers AS c
              ON c.customer_id = t.customer_id
WHERE  t.n = 1
       AND t.transaction_type="pay_in" order by total_amount desc limit 1;

[ad_2]

  • 0 0 Answers
  • 7 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 401 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 371 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 367 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.