【问题标题】:How to show this outuput?如何显示此输出?
【发布时间】:2016-10-21 10:14:00
【问题描述】:

我的查询是

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

输出是这样的并显示重复的行:

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

【问题讨论】:

  • 添加一个选择并按您想要的字段分组:select [yourfileds] from (your original query) group by [yourfields]
  • 您是否尝试将Select DistinctGroup By 添加到此查询? SType 列似乎是分组的好列

标签: c# sql ms-access


【解决方案1】:

您不能SELECT DISTINCT,但其他人可能会觉得它有帮助。 edit 为您提供答案。

关键字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

【讨论】:

    【解决方案2】:

    应该可以的:

    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
    

    【讨论】:

    • 请提出其他查询
    【解决方案3】:

    试试这个查询

    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
    

    【讨论】:

      【解决方案4】:

      distinct 和 group by 仅在记录唯一的情况下才有效。

      由于每行的 pant_type 不同,Distinct 仍将带回所有 4 行。 group by 也是如此。

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

      【讨论】:

      • 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
      猜你喜欢
      • 2013-08-20
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多