我不会撒谎,这是一个新的任务,我无法解决这个问题。
到目前为止,从这个查询中,我可以获得每个公寓大楼的所有租金收入。我只需要能够按月分开它。
这是我目前的查询:
SELECT cmplxName AS 'Complex Name', rentTrandate AS 'month' , sum(rentPaid) AS 'Rent Revenue'
FROM complex
INNER JOIN apartment
ON complex.cmplxID = apartment.cmplxID
INNER JOIN lease
ON apartment.aptNumber = lease.aptNumber and apartment.cmplxID = lease.cmplxID
INNER JOIN rent
ON lease.leaseID = rent.leaseID
GROUP BY cmplxName
这是数据的外观:
complexName month rentRevenue
Fox Run March 1250.00
Oak Meadows March 1500.00
Oak Meadows April 700.00
Villa Maria March 1200.00
Villa Maria April 600.00
这就是我的数据现在的样子:
Fox Run 2015-03-01 1250.00
Oak Meadows 2015-03-01 2200.00
Villa Maria 2015-03-01 1800.00
感谢您的耐心等待。
答案 0 :(得分:0)
嗨keelsinith这将完成如下 使用此SELECT DATENAME(月,rentTrandate) DATENAME是一个SQL方法,需要2个参数DATETIME(interval,YourDateValue) 间隔需要一年,一周,一个月
SELECT cmplxName AS 'Complex Name', SELECT DATENAME(month,rentTrandate) AS 'month' , sum(rentPaid) AS 'Rent Revenue'
FROM complex INNER JOIN公寓 ON complex.cmplxID = apartment.cmplxID INNER JOIN租约 ON apartment.aptNumber = lease.aptNumber和apartment.cmplxID = lease.cmplxID INNER JOIN租金 ON lease.leaseID = rent.leaseID GROUP BY cmplxName
答案 1 :(得分:0)
您只需更改月份字段的定义,并将分组更改为按组分组以及复杂名称:
SELECT cmplxName AS 'Complex Name', MONTHNAME(rentTrandate) AS 'month' , sum(rentPaid) AS 'Rent Revenue'
FROM complex
INNER JOIN apartment
ON complex.cmplxID = apartment.cmplxID
INNER JOIN lease
ON apartment.aptNumber = lease.aptNumber and apartment.cmplxID = lease.cmplxID
INNER JOIN rent
ON lease.leaseID = rent.leaseID
GROUP BY cmplxName, month
答案 2 :(得分:0)
我建议您同时包含年份和月份。此外,强烈建议使用表别名和限定列名称:
SELECT c.mplxName as Complex_Name, year(r.rentTrandate) as yyyy,
month(r.rentTrandate) as mm, sum(rentPaid) as Rent_Revenue
FROM complex c INNER JOIN
apartment a
ON c.cmplxID = a.cmplxID INNER JOIN
lease l
ON a.aptNumber = l.aptNumber and a.cmplxID = l.cmplxID INNER JOIN
rent r
ON l.leaseID = r.leaseID
GROUP BY c.cmplxName, year(r.rentTrandate), month(r.rentTrandate)
ORDER BY c.cmplxName, year(r.rentTrandate), month(r.rentTrandate);
这将月份作为数字,但这更适合于对结果集进行排序。