我有这样的SupplierLangs表: ID、SupplierId、SrcLngId、TrgLngId
它包含这样的数据:
1, 1000, 1, 2
2, 1000, 1, 3
3, 1000, 1, 4
4, 1000, 2, 3
5, 1000, 2, 4
6, 1001, 1, 2
7, 1001, 1, 4
8, 1001, 2, 4
9, 1002, 3, 4当语言表如下所示: ID,LangName
1, En
2, De
3, Fr
4, Pl这意味着供应商: 1000可以在以下几个方面进行转换:
En -> De
En -> Fr
En -> Pl
De -> FR
DE -> pl1001可以在以下几个方面进行翻译:
En -> De
En -> Pl
De -> Pl1002可以在以下几个方面进行翻译:
Fr -> Pl我需要一个结果,显示有多少供应商用给定的src语言翻译: LangId计数
1 2 (1000, 1001)
2 2 (1000, 1001)
3 1 (1002)
4 0 (no one translate from polish)我所做的只是计算有多少供应商用硬编码语言翻译:
select count(distinct SupplierId) from SupplierLangs
where SupplierId in
(
select sr.SupplierId from SupplierLangs sr
where sr.SrcLngId = 1
group by sr.SupplierId
)有了上述数据,返回2(两个供应商从英语翻译),但如何扩展到所有语言,以取得上述成果?
提前感谢
发布于 2013-12-10 22:16:13
我找到了正确的查询(但我使用的是临时表)-它是一个没有它的解决方案吗?
select distinct sr.SrcLngId, sr.SupplierId
into #myTable99
from SupplierLangs sl
select SrcLngId, count(SrcLngId) as [count] from #myTable99 temp
group by SrcLngId
drop table #myTable99谢谢大家的回复!
发布于 2013-12-10 08:32:09
尝尝这个
SELECT Count(DISTINCT supplierid)
FROM supplierlangs
WHERE supplierid IN (SELECT sr.supplierid
FROM supplierlangs sr
--where sr.SrcLngId = 1 --Remove this
GROUP BY sr.supplierid)
HAVING Count(DISTINCT supplierid) > 0 --this will remove the record having no translation发布于 2013-12-10 08:36:52
这应该给你一个清单,有源语言,目标语言和多少供应商翻译。
不加入:
SELECT SrcLngId, TrgLngId, count(*) FROM SupplierLangs
GROUP BY SrcLngId, TrgLngId加入:
SELECT L_SRC.ID, L_TRG.ID, COUNT(*) FROM SupplierLangs
INNER JOIN Languages AS L_SRC ON L_SRC.ID = SupplierLangs.SrcLngId
INNER JOIN Languages AS L_TRG ON L_TRG.ID = SupplierLangs.TrgLngId
GROUP BY L_SRC.ID, L_SRC.ID您可以从它创建一个视图。
这将过滤一种语言。
SELECT L_SRC.ID, L_SRC.ID, COUNT(*) FROM SupplierLangs
INNER JOIN Languages AS L_SRC ON L_SRC.ID = SupplierLangs.SrcLngId
INNER JOIN Languages AS L_TRG ON L_TRG.ID = SupplierLangs.TrgLngId
WHERE SupplierLangs.SrcLngId = 1
GROUP BY L_SRC.ID, L_SRC.ID您应该对SupplierId、SrcLngId、SupplierLangs的TrgLngId (如果您还没有)有一个唯一的约束,以避免有任何数据两次。
/edit:应该工作的:
SELECT SupplierSrc.SrcLngId, count(*) FROM
(
SELECT distinct SupplierId, SrcLngId FROM SupplierLangs AS SupplierSrc
)
GROUP BY SupplierSrc.SrcLngId https://stackoverflow.com/questions/20489341
复制相似问题