如何显示此输出?

时间:2016-10-21 10:14:01

标签: c# sql ms-access

我的查询是

SELECT 
    CustBillPayment.CustomerId, CustBillPayment.Customer_Name, 
    CustBillPayment.CustBill_Total, CustBillPayment.CustBill_Paid,
    CustBillPayment.CustBill_ReamingAmt, CustBillPayment.Created_Date, 
    CustBillPayment.Delivery_Date, CustBillPayment.Updated_Date, 
    Shirt_Mes.Shirt_Type, Shirt_Mes.Shirt_Qty, Shirt_Mes.Shirt_Price, 
    Shirt_Mes.Shirt_Total, 
    Pant_Mes.Pant_Type, Pant_Mes.Pant_QTY, Pant_Mes.Pant_Total, 
    Pant_Mes.Pant_Price
FROM 
    ((CustBillPayment 
INNER JOIN
    Shirt_Mes ON CustBillPayment.CustomerId = Shirt_Mes.CustomerId) 
INNER JOIN
    Pant_Mes ON CustBillPayment.CustomerId = Pant_Mes.CustomerId) 
WHERE 
    CustBillPayment.CustomerId = 17

输出是这样的,显示了dublicate行:

CustomerId CustNm  SType   SQty SPrice  STotal PType    PQTY    PTotal  PPrice
-------------------------------------------------------------------------------
17         lakhan  sType1   2    200     400   PType3   3         900     300
17         lakhan  sType1   2    200     400   PType4   1         400     400
17         lakhan  sType2   1    250     250   PType3   3         900     300
17         lakhan  sType2   1    250     250   PType4   1         400     400

但我想要

CustomerId CustNm  SType   SQty SPrice  STotal PType    PQTY    PTotal  PPrice
-------------------------------------------------------------------------------
17         lakhan  sType1   2    200     400   PType3   3         900     300
17         lakhan  sType2   1    250     250   PType4   1         400     400

4 个答案:

答案 0 :(得分:1)

您不能SELECT DISTINCT,但其他人可能会觉得有帮助。您可以在编辑下找到答案。

关键字DISTINCT列出了唯一的行。要了解有关它的更多信息,请阅读:http://www.w3schools.com/sql/sql_distinct.asp

修改

现在我看到了问题,我没有注意到数字不适合早些时候。在这种情况下,关键字DISTINCT无济于事。 JOIN首先加入Shirt_Mes表,但有两条记录,这意味着它将生成两行。下一个JOIN加入表Pant_Mes,条件也从该表中选择两个记录。前面的每一行都扩展了这两个记录。在这种情况下,它产生2 x 2行。

我猜,这是糟糕的表架构。您可以执行两个查询并将结果合并到应用程序逻辑中:

SELECT
  CustBillPayment.CustomerId,
  CustBillPayment.Customer_Name,
  CustBillPayment.CustBill_Total,
  CustBillPayment.CustBill_Paid,
  CustBillPayment.CustBill_ReamingAmt,
  CustBillPayment.Created_Date,
  CustBillPayment.Delivery_Date,
  CustBillPayment.Updated_Date,
  Shirt_Mes.Shirt_Type,
  Shirt_Mes.Shirt_Qty,
  Shirt_Mes.Shirt_Price,
  Shirt_Mes.Shirt_Total
FROM
  CustBillPayment
  JOIN Shirt_Mes ON CustBillPayment.CustomerId = Shirt_Mes.CustomerId
WHERE
  CustBillPayment.CustomerId=17

SELECT
  CustBillPayment.CustomerId,
  CustBillPayment.Customer_Name,
  CustBillPayment.CustBill_Total,
  CustBillPayment.CustBill_Paid,
  CustBillPayment.CustBill_ReamingAmt,
  CustBillPayment.Created_Date,
  CustBillPayment.Delivery_Date,
  CustBillPayment.Updated_Date,
  Pant_Mes.Pant_Type,
  Pant_Mes.Pant_QTY,
  Pant_Mes.Pant_Total,
  Pant_Mes.Pant_Price
FROM
  CustBillPayment
  JOIN Pant_Mes ON CustBillPayment.CustomerId = Pant_Mes.CustomerId
WHERE
  CustBillPayment.CustomerId=17

或者您可以将所有商品放在一张桌子上(我猜这是用于电子商店或类似的东西)。更好的是更改表模式,但如果您有充分的理由将货物存储在单独的表中(并且货物类型不是很好的理由),您可以执行以下查询:

SELECT
  CustBillPayment.CustomerId,
  CustBillPayment.Customer_Name,
  CustBillPayment.CustBill_Total,
  CustBillPayment.CustBill_Paid,
  CustBillPayment.CustBill_ReamingAmt,
  CustBillPayment.Created_Date,
  CustBillPayment.Delivery_Date,
  CustBillPayment.Updated_Date,
  Goods_Mes.Type,
  Goods_Mes.Qty,
  Goods_Mes.Price,
  Goods_Mes.Total
FROM
  CustBillPayment
  JOIN (SELECT Shirt_Mes.Shirt_Type AS Type,
               Shirt_Mes.Shirt_Qty AS Qty,
               Shirt_Mes.Shirt_Price AS Price,
               Shirt_Mes.Shirt_Total AS Total,
               Shirt_Mes.CustomerId FROM Shirt_Mes
        UNION
        SELECT Pant_Mes.Pant_Type AS Type,
               Pant_Mes.Pant_Qty AS Qty,
               Pant_Mes.Pant_Price AS Price,
               Pant_Mes.Pant_Total AS Total,
               Pant_Mes.CustomerId FROM Pant_Mes
        ) Goods_Mes ON CustBillPayment.CustomerId = Goods_Mes.CustomerId
WHERE
  CustBillPayment.CustomerId=17

为了更好地进行查询,您可以删除Shirt_TotalPant_Total,并将Goods_Mes.Total替换为Goods_Mes.Qty * Goods_Mes.Price

答案 1 :(得分:0)

这应该有效:

SELECT DISTINCT CustBillPayment.CustomerId,
            CustBillPayment.Customer_Name,
            CustBillPayment.CustBill_Total,
            CustBillPayment.CustBill_Paid,
            CustBillPayment.CustBill_ReamingAmt,
            CustBillPayment.Created_Date,
            CustBillPayment.Delivery_Date,
            CustBillPayment.Updated_Date,
            Shirt_Mes.Shirt_Type,
            Shirt_Mes.Shirt_Qty,
            Shirt_Mes.Shirt_Price,
            Shirt_Mes.Shirt_Total,
            Pant_Mes.Pant_Type,
            Pant_Mes.Pant_QTY,
            Pant_Mes.Pant_Total,
            Pant_Mes.Pant_Price
  FROM ((CustBillPayment INNER JOIN Shirt_Mes ON CustBillPayment.CustomerId = Shirt_Mes.CustomerId) INNER JOIN
        Pant_Mes ON CustBillPayment.CustomerId = Pant_Mes.CustomerId)
 WHERE CustBillPayment.CustomerId = 17
 GROUP BY Shirt_Mes.Shirt_Type

答案 2 :(得分:0)

尝试此查询

SELECT CBP.CustomerId, CBP.Customer_Name, 
       CBP.CustBill_Total, CBP.CustBill_Paid, 
       CBP.CustBill_ReamingAmt, CBP.Created_Date, 
       CBP.Delivery_Date, CBP.Updated_Date, 
       SMES.Shirt_Type, SMES.Shirt_Qty, SMES.Shirt_Price, 
       SMES.Shirt_Total, PMES.Pant_Type, PMES.Pant_QTY,    
       PMES.Pant_Total, PMES.Pant_Price 
FROM   CustBillPayment AS CBP 
LEFT JOIN Shirt_Mes AS SMES ON CBP.CustomerId = SMES.CustomerId 
LEFT JOIN Pant_Mes AS PMES ON CBP.CustomerId = PMES.CustomerId 
WHERE CBP.CustomerId = 17

答案 3 :(得分:0)

区别和分组仅在记录唯一时才有效。

由于pant_type在每一行中不同,因此仍然会带回所有4行。对于小组来说也是如此。

您期望的最终输出是什么,因为top_type上的所需输出对于pant_type没有意义?