多个选择语句用于计数

时间:2016-01-13 05:24:26

标签: sql sql-server

我有一个名为data的表。如下所述,我有几个列。我需要做的是我想在这些列中计算属性。现在我正在计算这些属性。但是同样的时间它也打印其他列。我有附加图像。从那只是我想要产品和总列数。也就像我想要Courier和Total在另一个视图中......我应该怎么办?enter image description here

这是我的代码

SELECT Product, Courier, Acc_No, Name, Delivered_Date, 
    Month, Bill_Run, Status, Remarks, count(id) as Total  
FROM data 
    WHERE ID = ID
    [AND Product = "{pro_id,false}"]
    [AND Courier = "{cou_id,false}"]
    [AND Bill_Run = "{bill_id,false}"]
    [AND Dispatch_Type = "{dis_id,false}"]
    [AND Status = "{sta_id,false}"]
    [AND Acc_No = "{acc_no,false}"]
    [AND Name = "{name,false}"]
    [AND Delivered_Date between {date_range,RANGE1} and {date_range,RANGE2}] 
    ORDER BY ID ASC

1 个答案:

答案 0 :(得分:1)

对于第一个:

SELECT Product,
       count(id) AS Total
FROM DATA
WHERE ID = ID [AND Product = "{pro_id,false}"][
  AND Courier = "{cou_id,false}"][
  AND Bill_Run = "{bill_id,false}"][
  AND Dispatch_Type = "{dis_id,false}"][
  AND Status = "{sta_id,false}"][
  AND Acc_No = "{acc_no,false}"][
  AND Name = "{name,false}"][
  AND Delivered_Date BETWEEN {date_range,
                              RANGE1} AND {date_range,
                                           RANGE2}]
ORDER BY ID ASC

对于第二种观点:

SELECT Courier,
       count(id) AS Total
FROM DATA
WHERE ID = ID [AND Product = "{pro_id,false}"][
  AND Courier = "{cou_id,false}"][
  AND Bill_Run = "{bill_id,false}"][
  AND Dispatch_Type = "{dis_id,false}"][
  AND Status = "{sta_id,false}"][
  AND Acc_No = "{acc_no,false}"][
  AND Name = "{name,false}"][
  AND Delivered_Date BETWEEN {date_range,
                              RANGE1} AND {date_range,
                                           RANGE2}]
ORDER BY ID ASC