0
SELECT VendorState, VendorCity
FROM Vendors JOIN COUNT(*)InvoiceDate as TotalInvoices ON Invoices
WHERE VendorState = 'NV' AND 'MI'

我上面的尝试根本不起作用:/我想让它显示

如果状态是 NV 或 MI,则来自 Vendors 表的 VendorState

供应商城

和每个城市的 TotalInvoices,最后在 TOTAL 中从 Invoices 表中的 InvoiceDate 中提取它的计数

4

3 回答 3

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')

这与您的其他问题类似

于 2012-11-15T15:20:39.893 回答
0

尝试

SELECT count(InvoiceDate) as [NumberInvoices], VendorState, VendorCity FROM Vendors inner JOIN Invoices ON Vendor PK* = Invoices FK WHERE VendorState in ('NV', 'MI') group by VendorState, VendorCity

于 2012-11-15T15:20:20.080 回答
0

您需要在Invoices表上指定外键才能Vendors加入Invoices. 这是一个例子:

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为此处应有的任何键。

于 2012-11-15T15:29:46.000 回答