2016-10-21 14 views
0

Meine Frage istWie wird dieser Ausgang angezeigt?

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 

Ausgabe wie diese ist und zeigt dublicate Reihen:

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 

aber ich will

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 
+0

fügen Sie eine Auswahl und Gruppe nach den gewünschten Feldern hinzu: ** wählen Sie [Ihre Dateien] aus (Ihrer ursprünglichen Abfrage) Gruppe nach [Ihre Felder] ** – Badiparmagi

+1

haben Sie versucht, 'Select Distinct' und' Group By' hinzuzufügen Abfrage?SType Spalte scheint gut Spalte zu gruppieren von – Misiakw

Antwort

0

, die funktionieren sollte:

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 
+0

Plz Vorschlagen andere Anfrage – sachin

1

Sie können nicht SELECT DISTINCT, aber andere können es hilfreich finden. Für Sie ist die Antwort unter bearbeiten.

Stichwort DISTINCT listet eindeutige Zeilen. Um mehr darüber zu erfahren, lesen Sie dies: http://www.w3schools.com/sql/sql_distinct.asp.

EDIT:

Jetzt habe ich das Problem sehen, habe ich nicht bemerkt, dass die Zahlen nicht früher passen. Das Schlüsselwort DISTINCT hilft in diesem Fall nicht. JOIN verbindet zuerst Shirt_Mes Tabelle, aber es gibt zwei Datensätze, was bedeutet, dass es zwei Zeilen erzeugt. Next JOIN verbindet Tabelle Pant_Mes und Bedingung wählt auch zwei Datensätze aus dieser Tabelle. Jede Zeile von früher wird für diese zwei Datensätze erweitert. In diesem Fall erzeugt es 2 x 2 Zeilen.

Ich denke, es ist schlechte Tabellenschema. Sie können zwei Abfragen tun und die Ergebnisse in der Anwendungslogik kombinieren:

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 

und

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 

Oder Sie alle Waren in einem Tisch legen kann (ich denke, es ist für E-Shop oder so ähnlich). Besser wäre ändern Tabellenschema sein, aber wenn Sie wirklich guten Grund haben, warum Waren zu lagern in separaten Tabellen (und Art der Ware ist nicht guter Grund) Sie diese Abfrage tun kannst:

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 

Um die Abfrage besser zu machen Sie können Shirt_Total und Pant_Total entfernen und Goods_Mes.Total durch Goods_Mes.Qty * Goods_Mes.Price ersetzen.

0

Diese Abfrage

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 
0

Distinct und Gruppe wird nur funktionieren, wenn die Datensätze eindeutig sind.

Distinct wird immer noch alle 4 Zeilen zurückgeben, da pant_type in jeder Zeile unterschiedlich ist. Das gleiche gilt für die Gruppe von.

Was ist die endgültige Ausgabe, die Sie erwarten, da die erforderliche Ausgabe an der Spitze in Bezug auf pant_type keinen Sinn ergibt?

+0

CustomerId CUSTNm SType SQty SPrice STotal PTyp PQTY PTotal PPrice ----------------------- -------------------------------------------------- ------ 17 lakhan sTyp1 2 200 400 PType3 3 900 300 17 lakhan sType2 1 250 250 PType4 1 400 400 – sachin