{ "data": { "question": { "questionId": "1268", "questionFrontendId": "1158", "categoryTitle": "Database", "boundTopicId": 33164, "title": "Market Analysis I", "titleSlug": "market-analysis-i", "content": "

Table: Users

\n\n
\n+----------------+---------+\n| Column Name    | Type    |\n+----------------+---------+\n| user_id        | int     |\n| join_date      | date    |\n| favorite_brand | varchar |\n+----------------+---------+\nuser_id is the primary key of this table.\nThis table has the info of the users of an online shopping website where users can sell and buy items.\n
\n\n

 

\n\n

Table: Orders

\n\n
\n+---------------+---------+\n| Column Name   | Type    |\n+---------------+---------+\n| order_id      | int     |\n| order_date    | date    |\n| item_id       | int     |\n| buyer_id      | int     |\n| seller_id     | int     |\n+---------------+---------+\norder_id is the primary key of this table.\nitem_id is a foreign key to the Items table.\nbuyer_id and seller_id are foreign keys to the Users table.\n
\n\n

 

\n\n

Table: Items

\n\n
\n+---------------+---------+\n| Column Name   | Type    |\n+---------------+---------+\n| item_id       | int     |\n| item_brand    | varchar |\n+---------------+---------+\nitem_id is the primary key of this table.\n
\n\n

 

\n\n

Write an SQL query to find for each user, the join date and the number of orders they made as a buyer in 2019.

\n\n

Return the result table in any order.

\n\n

The query result format is in the following example.

\n\n

 

\n

Example 1:

\n\n
\nInput: \nUsers table:\n+---------+------------+----------------+\n| user_id | join_date  | favorite_brand |\n+---------+------------+----------------+\n| 1       | 2018-01-01 | Lenovo         |\n| 2       | 2018-02-09 | Samsung        |\n| 3       | 2018-01-19 | LG             |\n| 4       | 2018-05-21 | HP             |\n+---------+------------+----------------+\nOrders table:\n+----------+------------+---------+----------+-----------+\n| order_id | order_date | item_id | buyer_id | seller_id |\n+----------+------------+---------+----------+-----------+\n| 1        | 2019-08-01 | 4       | 1        | 2         |\n| 2        | 2018-08-02 | 2       | 1        | 3         |\n| 3        | 2019-08-03 | 3       | 2        | 3         |\n| 4        | 2018-08-04 | 1       | 4        | 2         |\n| 5        | 2018-08-04 | 1       | 3        | 4         |\n| 6        | 2019-08-05 | 2       | 2        | 4         |\n+----------+------------+---------+----------+-----------+\nItems table:\n+---------+------------+\n| item_id | item_brand |\n+---------+------------+\n| 1       | Samsung    |\n| 2       | Lenovo     |\n| 3       | LG         |\n| 4       | HP         |\n+---------+------------+\nOutput: \n+-----------+------------+----------------+\n| buyer_id  | join_date  | orders_in_2019 |\n+-----------+------------+----------------+\n| 1         | 2018-01-01 | 1              |\n| 2         | 2018-02-09 | 2              |\n| 3         | 2018-01-19 | 0              |\n| 4         | 2018-05-21 | 0              |\n+-----------+------------+----------------+\n
\n", "translatedTitle": "市场分析 I", "translatedContent": "

Table: Users

\n\n
\n+----------------+---------+\n| Column Name    | Type    |\n+----------------+---------+\n| user_id        | int     |\n| join_date      | date    |\n| favorite_brand | varchar |\n+----------------+---------+\n此表主键是 user_id。\n表中描述了购物网站的用户信息,用户可以在此网站上进行商品买卖。\n
\n\n

 

\n\n

Table: Orders

\n\n
\n+---------------+---------+\n| Column Name   | Type    |\n+---------------+---------+\n| order_id      | int     |\n| order_date    | date    |\n| item_id       | int     |\n| buyer_id      | int     |\n| seller_id     | int     |\n+---------------+---------+\n此表主键是 order_id。\n外键是 item_id 和(buyer_id,seller_id)。\n
\n\n

 

\n\n

Table: Items

\n\n
\n+---------------+---------+\n| Column Name   | Type    |\n+---------------+---------+\n| item_id       | int     |\n| item_brand    | varchar |\n+---------------+---------+\n此表主键是 item_id。\n
\n\n

 

\n\n

请写出一条SQL语句以查询每个用户的注册日期和在 2019 年作为买家的订单总数。

\n\n

任意顺序 返回结果表。

\n\n

查询结果格式如下。

\n\n

 

\n\n

示例 1:

\n\n
\n输入:\nUsers 表:\n+---------+------------+----------------+\n| user_id | join_date  | favorite_brand |\n+---------+------------+----------------+\n| 1       | 2018-01-01 | Lenovo         |\n| 2       | 2018-02-09 | Samsung        |\n| 3       | 2018-01-19 | LG             |\n| 4       | 2018-05-21 | HP             |\n+---------+------------+----------------+\nOrders 表:\n+----------+------------+---------+----------+-----------+\n| order_id | order_date | item_id | buyer_id | seller_id |\n+----------+------------+---------+----------+-----------+\n| 1        | 2019-08-01 | 4       | 1        | 2         |\n| 2        | 2018-08-02 | 2       | 1        | 3         |\n| 3        | 2019-08-03 | 3       | 2        | 3         |\n| 4        | 2018-08-04 | 1       | 4        | 2         |\n| 5        | 2018-08-04 | 1       | 3        | 4         |\n| 6        | 2019-08-05 | 2       | 2        | 4         |\n+----------+------------+---------+----------+-----------+\nItems 表:\n+---------+------------+\n| item_id | item_brand |\n+---------+------------+\n| 1       | Samsung    |\n| 2       | Lenovo     |\n| 3       | LG         |\n| 4       | HP         |\n+---------+------------+\n输出:\n+-----------+------------+----------------+\n| buyer_id  | join_date  | orders_in_2019 |\n+-----------+------------+----------------+\n| 1         | 2018-01-01 | 1              |\n| 2         | 2018-02-09 | 2              |\n| 3         | 2018-01-19 | 0              |\n| 4         | 2018-05-21 | 0              |\n+-----------+------------+----------------+
\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 36, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"9.6K\", \"totalSubmission\": \"17.3K\", \"totalAcceptedRaw\": 9615, \"totalSubmissionRaw\": 17329, \"acRate\": \"55.5%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"Users\":[\"user_id\",\"join_date\",\"favorite_brand\"],\"Orders\":[\"order_id\",\"order_date\",\"item_id\",\"buyer_id\",\"seller_id\"],\"Items\":[\"item_id\",\"item_brand\"]},\"rows\":{\"Users\":[[1,\"2018-01-01\",\"Lenovo\"],[2,\"2018-02-09\",\"Samsung\"],[3,\"2018-01-19\",\"LG\"],[4,\"2018-05-21\",\"HP\"]],\"Orders\":[[1,\"2019-08-01\",4,1,2],[2,\"2018-08-02\",2,1,3],[3,\"2019-08-03\",3,2,3],[4,\"2018-08-04\",1,4,2],[5,\"2018-08-04\",1,3,4],[6,\"2019-08-05\",2,2,4]],\"Items\":[[1,\"Samsung\"],[2,\"Lenovo\"],[3,\"LG\"],[4,\"HP\"]]}}", "metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Users (user_id int, join_date date, favorite_brand varchar(10))\",\n \"Create table If Not Exists Orders (order_id int, order_date date, item_id int, buyer_id int, seller_id int)\",\n \"Create table If Not Exists Items (item_id int, item_brand varchar(10))\"\n ],\n \"mssql\": [\n \"Create table Users (user_id int, join_date date, favorite_brand varchar(10))\",\n \"Create table Orders (order_id int, order_date date, item_id int, buyer_id int, seller_id int)\",\n \"Create table Items (item_id int, item_brand varchar(10))\"\n ],\n \"oraclesql\": [\n \"Create table Users (user_id int, join_date date, favorite_brand varchar(10))\",\n \"Create table Orders (order_id int, order_date date, item_id int, buyer_id int, seller_id int)\",\n \"Create table Items (item_id int, item_brand varchar(10))\",\n \"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"\n ],\n \"database\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Users (user_id int, join_date date, favorite_brand varchar(10))", "Create table If Not Exists Orders (order_id int, order_date date, item_id int, buyer_id int, seller_id int)", "Create table If Not Exists Items (item_id int, item_brand varchar(10))", "Truncate table Users", "insert into Users (user_id, join_date, favorite_brand) values ('1', '2018-01-01', 'Lenovo')", "insert into Users (user_id, join_date, favorite_brand) values ('2', '2018-02-09', 'Samsung')", "insert into Users (user_id, join_date, favorite_brand) values ('3', '2018-01-19', 'LG')", "insert into Users (user_id, join_date, favorite_brand) values ('4', '2018-05-21', 'HP')", "Truncate table Orders", "insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('1', '2019-08-01', '4', '1', '2')", "insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('2', '2018-08-02', '2', '1', '3')", "insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('3', '2019-08-03', '3', '2', '3')", "insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('4', '2018-08-04', '1', '4', '2')", "insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('5', '2018-08-04', '1', '3', '4')", "insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('6', '2019-08-05', '2', '2', '4')", "Truncate table Items", "insert into Items (item_id, item_brand) values ('1', 'Samsung')", "insert into Items (item_id, item_brand) values ('2', 'Lenovo')", "insert into Items (item_id, item_brand) values ('3', 'LG')", "insert into Items (item_id, item_brand) values ('4', 'HP')" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"

\\u7248\\u672c\\uff1aMySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"

mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"

Oracle Sql 11.2.<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "{\"headers\":{\"Users\":[\"user_id\",\"join_date\",\"favorite_brand\"],\"Orders\":[\"order_id\",\"order_date\",\"item_id\",\"buyer_id\",\"seller_id\"],\"Items\":[\"item_id\",\"item_brand\"]},\"rows\":{\"Users\":[[1,\"2018-01-01\",\"Lenovo\"],[2,\"2018-02-09\",\"Samsung\"],[3,\"2018-01-19\",\"LG\"],[4,\"2018-05-21\",\"HP\"]],\"Orders\":[[1,\"2019-08-01\",4,1,2],[2,\"2018-08-02\",2,1,3],[3,\"2019-08-03\",3,2,3],[4,\"2018-08-04\",1,4,2],[5,\"2018-08-04\",1,3,4],[6,\"2019-08-05\",2,2,4]],\"Items\":[[1,\"Samsung\"],[2,\"Lenovo\"],[3,\"LG\"],[4,\"HP\"]]}}", "__typename": "QuestionNode" } } }