也许只是因为我本周末睡眠不足,但似乎无法修复它。 我有这些表:
这是我的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
如果我注释掉最后一个内部联接(带有发货产品),我会得到正确数量的发票等,但是当我加入发货产品时,计数不会计算invoiceid,但不知道货物的数量是多少。 如果我向该组添加更多内容,它将不再按国家/地区分组,并且每个发票和发货等都有一行。 我不知何故在星期一不能看到我的错误。也许我只需要更多的咖啡。
答案 0 :(得分:0)
看来你的问题是加入一对多的关系。
我认为您应该将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
答案 1 :(得分: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