noob to SQL。我必须创建一个查询,按月返回客户列表(按名称)及其收入和访问量。不幸的是,我不知道如何做到这一点,因为我班上的书并没有涵盖与此类似的任何内容。这是我到目前为止所提出的......我想我已经接近但我真的不知道下一步该做什么!
SELECT customer_first_name, customer_middle_initial, customer_last_name,
price – cost as revenue, datepart(mm,date) as Month COUNT(receipt_number)
as Visits
FROM Customers, invoice4477
WHERE Customers.customer_id = invoice4477.customer_id
GROUP BY Visits
ORDER BY customer_last_name desc
你能指点我正确的方向吗?用noob说话?我真的不知道自己在做什么。我使用的记录是这个问题在数据库中真正相关的唯一记录。我只是不明白如何按月订购访问次数和每次访问收入。
此外,我没有办法测试代码,因为我没有实际的数据库,只是表格的截图(这是真正的问题,否则我可能会对其进行故障排除直到我找到答案)。
我想我希望输出看起来像这样:
Customer Name Revenue Visits Month
Joe Bob $100 2 January
Joe Bob $34 1 February
Sally Sue $2443 5 January
Sally Sue $243 4 June
请帮忙!谢谢:))
答案 0 :(得分:1)
适用于 Oracle PL / SQL
SELECT Customer
, Month
, COUNT(ReceiptNo) AS Visits
, SUM(Revenue) AS Revenue
FROM (SELECT customer_first_name || ' ' || customer_middle_initial || '. ' || customer_last_name as Customer
, to_char(date, 'Month') AS Month
, invoice4477.cost - invoice4477.price as Revenue
, receipt_number AS ReceiptNo
FROM Customers
INNER JOIN invoice4477 ON Customers.customer_id = invoice4477.customer_id)
GROUP BY Customer, Month;
架构:列名在我的示例
中有所不同create table x(REC_NO int, CUST_ID int, RDATE date, price int, cost int);
create table cust (cust_id int, fname varchar2(20)
, mname varchar2(20), lname varchar2(20));
insert into x (rec_no, cust_id, rdate, price, cost) values
(100, 1, to_date('08/04/2015','dd/mm/yyyy'), 150, 200);
insert into x (rec_no, cust_id, rdate, price, cost) values
(101, 2, to_date('08/03/2015','dd/mm/yyyy'), 70, 80);
insert into x (rec_no, cust_id, rdate, price, cost) values
(102, 1, to_date('07/01/2015','dd/mm/yyyy'), 50, 90);
insert into x (rec_no, cust_id, rdate, price, cost) values
(103, 3, to_date('07/02/2015','dd/mm/yyyy'), 10, 200);
insert into x (rec_no, cust_id, rdate, price, cost) values
(104, 3, to_date('07/01/2015','dd/mm/yyyy'), 10, 200);
insert into x (rec_no, cust_id, rdate, price, cost) values
(104, 3, to_date('09/01/2015','dd/mm/yyyy'), 29, 200);
insert into cust (cust_id, fname, mname, lname) values
(1, 'john1','x', 'doe');
insert into cust (cust_id, fname, mname, lname) values
(2, 'john2','x', 'doe');
insert into cust (cust_id, fname, mname, lname) values
(3, 'john3','x', 'doe');
<强> SQL 强>
SELECT CNAME
, RMonth as Month
, SUM(REVENUE) as Revenue
, COUNT(REC_NO) as Visits
FROM (SELECT fname || ' ' || mname || '. ' || lname as CName
, to_char(RDATE, 'Month') AS RMonth
, x.cost - x.price as Revenue
, REC_NO
FROM cust INNER JOIN x ON cust.cust_id = x.CUST_ID)
GROUP BY CNAME, RMonth;
我得到的结果如下
| CNAME | MONTH | REVENUE | VISITS |
|--------------|-----------|---------|--------|
| john1 x. doe | April | 50 | 1 |
| john2 x. doe | March | 10 | 1 |
| john3 x. doe | February | 190 | 1 |
| john3 x. doe | January | 361 | 2 |
| john1 x. doe | January | 40 | 1 |
现在,请注意,这可能不是这样做的可行方法,但这可行..: - )
答案 1 :(得分:1)
这是一个T-SQL解决方案。尝试一下,如果需要任何调整,请告诉我。
SELECT C.customer_first_name,
C.customer_last_name,
CONCAT('$',SUM(I.price)) AS Revenue, --not sure whether you wanted price or cost
COUNT(*) AS Visits,
DATENAME(MONTH,I.[date]) AS [Month]
FROM Customers C
INNER JOIN invoice4477 I
ON C.CustomerID = I.CustomerID
GROUP BY C.CustomerID,C.customer_first_name,C.customer_last_name,DATENAME(MONTH,I.[Date])