我从两张卡车和订单计算每月的平均汽油数量。到目前为止,我每个月都获得平均值,我可以通过按日期排序和外部SQL的平均值来获得。
我希望得到相同的值,但现在它应该返回订单的所有细节。我将使用它来使用行映射器映射到java以获得一对多关系,并使用具有订单列表的dto类。
select truckId, truckcode, purchasedDate, descriptions, avg(avgPetrolQty) as avgPerMonth from (
SELECT t.truckId, t.truckCode, t.purchasedDate, t.descriptions,
COALESCE(monthname(orderDate),'Not Announced') as month,
IFNULL (avg(petrolQty),0) as avgPetrolQty
from truck t left join orderz o
on t.truckId = o.truckId
where t.truckId = :t.truckId
group by t.truckCode, orderDate,
year(orderDate)
) group by truckcode
到目前为止,这给了我每辆卡车的预期平均每月单一价值,但我想将剩余的订单细节添加到它,例如应该通过id获得的完全加入。我试过这个,但显然不准确
select t.truckId, t.truckCode, t.purchasedDate,
t.descriptions, o.orderId, o.petrolQty, o.orderDate, avg(avgPetrolQty) as avgPerMonth, o.truckId from (truck t
SELECT t.truckId, t.truckCode, t.purchasedDate, t.descriptions, o.orderId, o.petrolQty, o.orderDate, o.truckId
COALESCE(monthname(o.orderDate),'Not Announced') as month,
IFNULL (avg(o.petrolQty),0) as avgPetrolQty
left join orderz o
on t.truckId = o.truckId
where t.truckId = :t.truckId
group by t.truckId, o.orderDate
)
from truck t left join orderz o
on t.truckId = o.truckId
where T.truckId = :t.truckId
group by t.truckId, o.orderDate
每月平均值是最重要的信息。现在我只想获得卡车的全部细节(这意味着所有属于卡车的订单)
SQL表
DROP TABLE IF EXISTS truck;
CREATE TABLE truck (
truckId INT NOT NULL AUTO_INCREMENT,
truckCode VARCHAR(255) NOT NULL UNIQUE,
purchasedDate timestamp NOT NULL,
descriptions VARCHAR(255) NOT NULL,
PRIMARY KEY (truckId)
);
DROP TABLE IF EXISTS orderz;
CREATE TABLE orderz (
orderId INT NOT NULL AUTO_INCREMENT,
petrolQty DOUBLE NOT NULL,
orderDate timestamp NOT NULL,
truckId INT,
PRIMARY KEY (orderId),
FOREIGN KEY (truckId) REFERENCES truck(truckId)
ON UPDATE CASCADE
ON DELETE CASCADE
);
答案 0 :(得分:1)
要做到这一点,你的外部查询应该或多或少地作为一个复印机,所以你在内部查询中完成所有工作并复制到外部select语句只有外部select语句才能做到的事情是获得平均值的平均值
select truckId, truckCode, purchasedDate,
descriptions, orderId, petrolQty, orderDate, avg(avgPetrolQty) as avgPerMonth, truckId from (
SELECT t.truckId, t.truckCode, t.purchasedDate, t.descriptions, o.orderId, o.petrolQty, o.orderDate,
COALESCE(monthname(o.orderDate),'Not Announced') as month,
IFNULL (avg(o.petrolQty),0) as avgPetrolQty
from truck t left join orderz o
on t.truckId = o.truckId
where t.truckId = :t.truckId
group by t.truckCode, o.orderId, o.orderDate
) group by truckCode, orderId
这就是我在java中使用RowMapper和namedParameterJdbcTemplate将其映射到id的方式
public TruckFullDetailDto getTruckFullDetailById (final Integer id) {
SqlParameterSource namedParameterSource =
new MapSqlParameterSource("t.truckId", id);
TruckDetailMapper mapper = new TruckDetailMapper();
namedParameterJdbcTemplate.query(fullTruckDetailsql, namedParameterSource, mapper);
TruckFullDetailDto truck = mapper.getDetail();
return truck;
}
/**
* This is mapper maps one to many relationship.
*/
public class TruckDetailMapper implements RowMapper<TruckFullDetailDto> {
private TruckFullDetailDto detail;
public TruckFullDetailDto mapRow(ResultSet rs, int rowNum)
throws SQLException {
if (detail == null) {
this.detail = new TruckFullDetailDto();
detail.setTruckId(rs.getInt("truckId"));
detail.setTruckCode(rs.getString("truckCode"));
detail.setAvgPerMonth(rs.getDouble("avgPerMonth"));
detail.setDescriptions(rs.getString("descriptions"));
detail.setPurchasedDate(rs.getDate("purchasedDate"));
}
Order order = new Order ();
order.setOrderId(rs.getInt("orderId"));
order.setPetrolQty(rs.getDouble("petrolQty"));
order.setOrderDate(rs.getDate("orderDate"));
order.setTruckId(rs.getInt("truckId"));
this.detail.getOrderList().add(order);
return null;
}
private TruckFullDetailDto getDetail() {
return detail;
}
}