INNER JOIN中的SQL区别

时间:2014-01-13 18:09:08

标签: sql sql-server join distinct sqlcmd

我在许多表上创建了一个SELECT。必须按原样指定输出(通过文件中的SQLCMD输出)

问题是,输出中有太多行。

代码(来自MS SQL Server Management Express):

SELECT DISTINCT 
                      AORDER.cOrderId AS 'order-id', APOS.cOrderItemId AS 'order-item-id', 'CustomerReturn' AS 'adjustment-reason-code', AORDER.cCurrency AS currency, 
                      CONVERT(DECIMAL(16, 2), ROUND(GSPOS.fVKPreis * GSPOS.nAnzahl, 2)) AS 'item-price-adj', CONVERT(DECIMAL(16, 2), 
                      CASE WHEN GSPOS.nAnzahl = APOS.nQuantityPurchased THEN ROUND(APOS.fShippingPrice, 2) ELSE '0.00' END) AS 'shipping-price-adj', 
                      GSPOS.nAnzahl AS quantity
FROM         tgutschriftpos AS GSPOS INNER JOIN
                      tgutschrift AS GS ON GSPOS.tGutschrift_kGutschrift = GS.kGutschrift INNER JOIN
                      tbestellung AS B ON GS.kRechnung = B.tRechnung_kRechnung INNER JOIN
                      pf_amazon_bestellung AS AORDER ON B.cInetBestellNr = AORDER.cOrderId INNER JOIN
                      pf_amazon_bestellungpos AS APOS ON AORDER.kAmazonBestellung = APOS.kAmazonBestellung AND GSPOS.cArtNr = APOS.cArtNr
WHERE     (APOS.cOrderItemId IS NOT NULL) AND (GS.cStatus IS NULL) AND (NOT (GSPOS.tArtikel_kArtikel = 0)) AND (NOT (GSPOS.cArtNr IS NULL))
GROUP BY GSPOS.kGutschriftPos, AORDER.cOrderId, GSPOS.nAnzahl, GSPOS.fVKPreis, APOS.cOrderItemId, AORDER.cCurrency, APOS.nQuantityPurchased, 
                      APOS.fShippingPrice

输出原样:

123456789   12345   CustomerReturn  EUR 14,95   0,00    1,0000
123456789   12345   CustomerReturn  EUR 29,90   3,87    2,0000
123456789   6789    CustomerReturn  EUR 14,95   1,93    1,0000
123456789   6789    CustomerReturn  EUR 29,90   0,00    2,0000

输出应该是:

123456789   12345   CustomerReturn  EUR 14,95   0,00    1,0000
123456789   6789    CustomerReturn  EUR 14,95   1,93    1,0000

我希望有人可以帮助我吗?

谢谢,

0 个答案:

没有答案