我有一张桌子应该像这样:
CREATE TABLE orders
(
order_number int PRIMARY KEY,
client varchar(20),
revenue int,
fixed_transport_cost int,
income int,
order_date date
)
我期望的是:
选择在2017年首次下订单的客户,并计算 他们每个人在2018年创造的收入总额(总计)。
我尝试过的是这个
SELECT
clients, SUM(income)
FROM
orders
HAVING
YEAR(order_date) = '2018'
AND income = ANY (SELECT income FROM orders
WHERE YEAR(order_date) = '2017')
GROUP BY
income;
但查询不正确。我的问题是如何检查是否有人已在2017年订购?
答案 0 :(得分:1)
您可以尝试以下查询:
from flask import Flask
from FlaskUserAuthentication.Site.routes import site
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
from FlaskUserAuthentication.API.routes import api
OR
SELECT
client, SUM(income)
FROM
orders
WHERE
YEAR(order_date) = '2018'
AND client IN (
SELECT client
FROM orders
GROUP BY client
HAVING MIN(YEAR(order_date)) = '2017')
GROUP BY
client;
如果您想让我们尝试查询并给您演示,请提供数据样本。