这是我要执行的方案:
有两个表:带列的车辆表:vehicleType,brand。以及带有以下列的装运表:desc,vehicleType,date_delivered,date_dispatched。
我想从车辆表中获取所有的vehicleType,并获取他们在货运表中完成的货运总数。但是问题是,我不知道该怎么做。
Shipment.java
package com.pahrsek.smartfleet.model;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Delivery of goods
* @author JRDomingo
*
*/
@Entity
@Table(name="shipment")
public class Shipment {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long id;
@Column(name="booking_number",unique=true)
public String bookingNumber;
@Column(name="wb_number")
public String wbNumber;
@ManyToOne
@JoinColumn(name="vehicle_id", referencedColumnName="id")
public Vehicle vehicle;
@ManyToOne
@JoinColumn(name="customer_id", referencedColumnName="id")
public Customer customer;
public String origin;
public String depot;
@ManyToOne
@JoinColumn(name="vendor_id", referencedColumnName="id")
public Vendor vendor;
public String type;
@Column(name="commodity_type")
public String commodityType;
@Column(name="truck_type")
public String truckType;
@Enumerated(EnumType.STRING)
public Status status;
@Column(name="delivery_date")
public Date deliveryDate;
@Column(name="fuel_po")
public String fuelPo;
@Column(name="client_ref_no")
public String clientReferenceNumber;
public String remarks;
@ManyToOne
@JoinColumn(name="driver_id",referencedColumnName="id")
public Personnel driver;
@ManyToOne
@JoinColumn(name="helper1_id",referencedColumnName="id")
public Personnel helper1;
@ManyToOne
@JoinColumn(name="helper2_id",referencedColumnName="id")
public Personnel helper2;
public enum Status{
New, Dispatched, Delivered, Completed, Cancelled
}
/******
* ACTUAL DATES IMPLEMENTED
******/
@Column(name="date_created")
public Date dateCreated;
@Column(name="date_dispatched")
public Date dateDispatched;
@Column(name="date_completed")
public Date dateCompleted;
@Column(name="date_cancelled")
public Date dateCancelled;
@Column(name="date_received")
public Date dateReceived;
@Column(name="farthest_destination")
public String farthestDestination;
@Column(name="client_rate")
public Double clientRate;
@Column(name="is_sub_con")
public boolean isSubCon;
@Column(name="sub_con_rate")
public Double subConRate;
@Column(name="fuel")
public Double fuel;
@Column(name="fuel_amount")
public Double fuelAmount;
@Column(name="route_code")
public String routeCode;
@ManyToOne
@JsonIgnore
@JoinColumn(name="dispatched_odometer_id",referencedColumnName="id")
public RegularOdometerUsage dispatchedOdometer;
@ManyToOne
@JsonIgnore
@JoinColumn(name="delivered_odometer_id",referencedColumnName="id")
public RegularOdometerUsage deliveredOdometer;
@ManyToOne
@JsonIgnore
@JoinColumn(name="completed_odometer_id",referencedColumnName="id")
public RegularOdometerUsage completedOdometer;
/**
* index 0 = Driver , index 1 = Helper1, index 2 = Helper2
*/
@JsonIgnore
@OneToMany(mappedBy="shipment",targetEntity=PersonnelRate.class)
public List<PersonnelRate> personnelRates = new ArrayList<PersonnelRate>();
@JsonIgnore
@ManyToOne
@JoinColumn(name="company_id", referencedColumnName="id")
public Company company;
@JsonIgnore
@ManyToOne
@JoinColumn(name="prepared_user_id", referencedColumnName="id")
public User preparedBy;
@JsonIgnore
@ManyToOne
@JoinColumn(name="customer_invoice", referencedColumnName="id")
public CustomerInvoice customerInvoice;
@JsonIgnore
@ManyToOne
@JoinColumn(name="trucker_settlement", referencedColumnName="id")
public TruckerSettlement truckerSettlement;
}
最佳答案
将COUNT()与GROUP BY结合使用可按vehicleType将它们分组在一起
SELECT
vehicle.vehicleType,
count(shipment.id) as totalShipment
FROM
Vehicle as vehicle
INNER JOIN Shipment as shipment ON shipment.vehicleType = vehicle.vehicleType group by vehicle.vehicleType