2016-05-09 11 views
0

おそらく私は今週の週末に十分な休息を取っていないと思うかもしれませんが、私はそれを直すことはできません。 ((FKの区分とPKのProductID)Sql group by and join issue

  • 製品(FK InvoiceIDとPK ShipmentID)
  • 出荷
  • 請求書(FK ShipmentIDとFKのProductIDとPK ShippedProductsID)
  • ShippedProducts: 私は、これらのテーブルを持っていますFK得意先とのPK InvoiceID)
  • お客様(FK CountryIdとPK得意先)
  • 国(PK CountryID)

残念ながら、他の方法でスキーマを説明することはできません。私がデータ構造のより良い概観を与えることができるかどうか私に教えてください。

ここ

は私のSQLです(データベースは、Microsoft SQL Server 2008の場合)

SELECT countries.countryid, 
     countryname, 
     isnull(round(sum(InvoiceTotal), 2),0) as TotalInvoice, 
     count(invoices.invoiceid) as nrOfInvoices, 
     count(shipments.shipmentid) as nrOfShipments 
FROM INVOICES 
inner join customers on invoices.CustomerID = customers.customerid 
inner join countries on customers.CountryID = COUNTRIES.CountryID 
inner join shipments on shipments.invoiceid = invoices.invoiceid 
inner join ShippedProducts on ShippedProducts.ShipmentID = shipments.ShipmentID 
group by countryname, COUNTRIES.CountryID, CurrencyName, CURRENCIES.CurrencyID 

私がコメントアウトした場合は、最後のインナーは、私は、内側参加するとき、私は請求書などの権利数を取得しますが、(shippedproductsに)参加します出荷された製品数は、請求書をカウントしませんが、何とか出荷された製品の数をカウントします。 グループにもっと多くのものを追加した場合、それは国によってもうグループ化されず、各請求書と発送品などの行があります。 私は何とかこの月曜日の私の間違いを見ることはできません。多分私はもっとコーヒーが必要です。

+0

なぜこの結合が必要ですか?あなたはそのテーブルの列を取得していますか? –

+0

はい、 'count(ShippedProducts.ShipmentProductsID)as totalProd' –

+1

' count(distinct invoices.invoiceid)をnrOfInvoices'として試してください。 – nscheaffer

答えて

0

グループなしでこれを行うには、ウィンドウ機能を使用できるはずです。このように

SELECT 
    countries.countryid, 
    countryname, 
    isnull(round(sum(InvoiceTotal) over (partition by invoice.invoiceID), 2),0) 
    as TotalInvoice, 
    count(invoices.invoiceid) 
    over (partition by countries.countryid) as nrOfInvoicesPerCountry, 
    count(shipments.shipmentid) over (partition by coutries.countryid) 
    as nrOfShipments 
FROM INVOICES 
inner join customers on invoices.CustomerID = customers.customerid 
inner join countries on customers.CountryID = COUNTRIES.CountryID 
inner join shipments on shipments.invoiceid = invoices.invoiceid 
inner join ShippedProducts on ShippedProducts.ShipmentID = shipments.ShipmentID 
0

あなたの問題は1対多の関係に加わっているようです。

私はあなたのカウントをShippedProducts.ShipmentProductsIDにするか、selectステートメントの副選択に入れるべきだと思います。このようなものかもしれません..

select countries.countryid, 
    countryname, 
    isnull(round(sum(InvoiceTotal), 2),0) as totalInvoice, 
    count(invoices.invoiceid) as nrOfInvoices, 
    count(shipments.shipmentid) as nrOfShipments 
    (select count shipmentproductsid from shipmentproducts where shipmentproducts.shipmentid = shipments.shipmentID) as totalProd 
from invoices 
    inner join customers on invoices.customerid = customers.customerid 
    inner join countries on customers.countryid = countries.countryid 
    inner join shipments on shipments.invoiceid = invoices.invoiceid  
group by countryname, countries.countryid , currencyname, currencies.currencyid 
+0

ありがとう、私はそれを前に試してみました。それはグループに 'shipmentid'を追加するように言われました。それはそれがもはや国に集団化しないという結果となる。私は同じ国でいくつかの行を持っていますが、 –