2012-11-15 54 views
0
SELECT VendorState, VendorCity 
FROM Vendors JOIN COUNT(*)InvoiceDate as TotalInvoices ON Invoices 
WHERE VendorState = 'NV' AND 'MI' 

总数我上面不会在所有的工作的尝试:/我想把它从卖方表显示计算发票

VendorState如果状态是NV或MI

VendorCity

和TotalInvoices为每个城市和最终在总计绘制它从发票表InvoiceDate的计数

回答

0

尝试

SELECT COUNT(InvoiceDate)为[NumberInvoices],VendorState,VendorCity 从供应商处INNER JOIN发票ON 卖方PK * = 发票FK WHERE VendorState在( 'NV', 'MI') 组由VendorState ,VendorCity

0

你不能以这种方式连接表。您需要在两个表之间共享一个共享的值,如供应商ID或其他内容。

因此,首先,它是:

select a.vendorstate, a.vendorcity, sum(invoices) as 'the sum' 
from vendors a inner join invoices b on a.vendorid = b.vendorid 
group by a.vendorstate, a.vendorcity 
where state in ('NV','MI') 

这类似于你的其他问题

0

你需要指定为了加入VendorsInvoicesInvoices表的外键。这里有一个例子:

SELECT v.VendorState, v.VendorCity, COUNT(i.InvoiceDate) AS Invoices 
FROM Vendors v WITH(NOLOCK) 
JOIN Invoices i WITH(NOLOCK) ON i.VendorID = v.VendorID 
WHERE v.VendorState IN ('NV', 'MI') 
GROUP BY v.VendorState, v.VendorCity 
ORDER BY v.VendorState, v.VendorCity 

很显然,您需要更改加入i.VendorID = v.VendorID到任何按键应该在这里。