I have two (simplified) tables/classes set up like this:
public class OrderHeader
{
public OrderHeader()
{
Details = new HashSet<OrderDetail>();
}
[Key]
public int TransactionId { get; set; }
public string CustomerName { get; set; }
public virtual ICollection<OrderDetail> Details { get; set; }
}
public class OrderDetail
{
[Key]
public int DetailId { get; set; }
public int ItemId { get; set; }
public int QuantityOrdered { get; set; }
public int QuantityShipped { get; set; }
public int QuantityRemaining { get; set; }
public int TransactionId { get; set; }
[ForeignKey("TransactionId")]
public virtual OrderHeader Header { get; set; }
}
I am trying to create a view to return:
TransactionId
OrderHeader
as column Number of Details
OrderHeader
where QuantityRemaining
equals zero as columnn Completed
OrderHeader
named Partially Shipped
where QuantityShipped
is greater than zero but less than QuantityOrdered
I have tried querying both the OrderHeader
and the OrderDetails
tables and trying to make sub queries for each column I wanted but I wind up getting multiple lines with the same TransactionId
. And when I use the DISTINCT
select the numbers are off from what I know to be correct.
While admitting this is utterly pitiful, I can get a partial but accurate result set with only two columns:
SELECT
TransactionId,
COUNT(*) AS [Number of Details]
FROM OrderDetails
GROUP BY TransactionID
But I can not get it working with the other two columns.
Something like this ?
SELECT
TransactionId,
COUNT(*) AS [Number of Details],
Sum(CASE WHEN QuantityRemaining = 0 THEN 1 ELSE 0 END) AS [Completed],
Sum(CASE WHEN QuantityShipped > 0 AND QuantityShipped < QuantityOrdered THEN 1 ELSE 0 END) AS [Partially Shipped]
FROM OrderDetails
GROUP BY TransactionID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.