LEETCODE: 1193 Monatstransaktion - Wie funktioniert die Gruppe um 1, 2 in einer SQL -Abfrage [Duplikat]MySql

MySQL DBMS-Forum
Anonymous
 LEETCODE: 1193 Monatstransaktion - Wie funktioniert die Gruppe um 1, 2 in einer SQL -Abfrage [Duplikat]

Post by Anonymous »

Ich arbeite an dem Leetcode-Problem "Monatliche Transaktionen i" und habe eine Frage zu einer Lösung, die ich gefunden habe.

Code: Select all

+------+---------+----------+--------+------------+
| id   | country | state    | amount | trans_date |
+------+---------+----------+--------+------------+
| 121  | US      | approved | 1000   | 2018-12-18 |
| 122  | US      | declined | 2000   | 2018-12-19 |
| 123  | US      | approved | 2000   | 2019-01-01 |
| 124  | DE      | approved | 2000   | 2019-01-07 |
+------+---------+----------+--------+------------+
< /code>
Meine Lösung, die wie erwartet funktioniert, verwendet diese Abfrage: < /p>
SELECT
DATE_FORMAT(trans_date, '%Y-%m') AS month,
country,
COUNT(*) AS trans_count,
SUM(IF(state = 'approved', 1, 0)) AS approved_count,
SUM(amount) AS trans_total_amount,
SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount
FROM Transactions
GROUP BY DATE_FORMAT(trans_date, '%Y-%m'), country;
Erwartete Ausgabe

Code: Select all

Output:
+----------+---------+-------------+----------------+--------------------+-----------------------+
| month    | country | trans_count | approved_count | trans_total_amount | approved_total_amount |
+----------+---------+-------------+----------------+--------------------+-----------------------+
| 2018-12  | US      | 2           | 1              | 3000               | 1000                  |
| 2019-01  | US      | 1           | 1              | 2000               | 2000                  |
| 2019-01  | DE      | 1           | 1              | 2000               | 2000                  |
+----------+---------+-------------+----------------+--------------------+-----------------------+
Ich bin dann auf die Lösung eines anderen Benutzers gestoßen, die Group von verwendet, und ich bin verwirrt darüber, wie es funktioniert:

Code: Select all

SELECT
DATE_FORMAT(trans_date, '%Y-%m') AS month,
country,
COUNT(*) AS trans_count,
SUM(state = 'approved') AS approved_count,
SUM(amount) AS trans_total_amount,
SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount
FROM Transactions
GROUP BY 1, 2;
Insbesondere bin ich von der Gruppe durch 1, 2 verwirrt, da die erste und zweite Spalten der Transaktionen Tabelle ID und Land sind. Wie kann diese Abfragegruppe von trans_date und landes ?>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post