1)查询返回所有在3月销售额大于或等于20的座席,还返回3月大于或等于20的销售额和3月大于或等于20的销售额。结果排序为降序。 冒号:名称,数量,数量
2)查询返回所有在3月一个月甚至没有一次销售额大于20的代理商。 冒号:姓名
数据库:
DBSale
ID ____ IDAgent _____日期____金额
1 _______ 1 ______ 16-03-03 _____ 15
2 _______ 2 ______ 16-01-03 _____ 25
3 _______ 1 ______ 16-01-03 _____ 17
4 _______ 3 ______ 16 04-03 _____ 55
5 _______ 4 ______ 2016年4月3日_____ 12
6 _______ 5 ______ 16-05--03 _____ 13
7 _______ 5 ______ 16-06-03 _____ 11
8 _______ 4 ______ 16 11-03-16 _____ 20
9 _______ 4 ______ 16-12-03 _____ 21
10 ______ 3 ______ 16-03-03 _____ 45
11 ______ 2 ______ 16-03-03 _____ 45
12 ______ 1 ______ 16-03-03 _____ 11
13 ______ 1 ______ 16-03-03 _____ 19
14 ______ 1 ______ 2013年1月4日_____ 25
15 ______ 1 ______ 16 04-04-16 _____ 65
16 ______ 3 ______ 16 15-04-16 _____ 10
17 ______ 5 ______ 16-16-04- _____ 41
DBUser
ID ___名称
1 ___马克·罗斯伯格
2 ___唐·史密斯
3 ___ Anna Twix
4 ___ Angela Reeves
5 ___ Juan Dovitoso
这些是我不正确的尝试:
1)
SELECT u.name SUM(s.amount) AS amount, COUNT(s.amount) AS count
FROM dbuser AS u JOIN dbsale AS s
ON u.id = s.idagent
ORDER BY s.amount ASC
HAVING s.amount >= 20 AND MONTH(s.date)='3';
2)
SELECT DISTINCT u.name
FROM dbuser AS u INNER JOIN dbsale AS s
ON u.id = s.idagent
WHERE s.amount < 20 AND MONTH(s.date)='3' ;
答案 0 :(得分:0)
您的查询1将是-
SELECT
A.`name`,
SUM(B.amount) AS amount,
COUNT(B.amount) AS count
FROM DBUser A
INNER JOIN DBSale B ON A.ID = B.IDAgent
WHERE MONTH(CAST(B.date AS DATE))=3
AND B.amount >= 20
GROUP BY A.`name`
查询2将是-
SELECT A.`name`
FROM DBUser A
INNER JOIN DBSale B
ON A.ID = B.IDAgent
WHERE MONTH(CAST(B.date AS DATE))=3
GROUP BY A.`name`,A.ID
HAVING SUM(CASE WHEN B.Amount > 20 THEN 1 ELSE 0 END) > 0
根据您的要求添加订单。