1

我有一个数据表(结构不是很好),其中有以下内容

ClientID | Parameter | Value 
111..........Street..........Evergreen
111..........Zip................75244
111..........Country.........USA
222..........Street..........Evergreen
222..........Zip................75244
222..........Country.........USA
333..........Street..........Evergreen
333..........Zip................75240
333..........Country.........USA
444..........Street..........Evergreen
444..........Zip................75240
444..........Country.........USA
555..........Street..........Evergreen
555..........Zip................75240
555..........Country.........USA
666..........Street..........Some Street
666..........Zip................75244
666..........Country.........USA

为此,我想选择所有在 Street = Evergreen 但也使用 ZIP 75244 的客户 ID,我有超过 700K 行,因此,全部导出将是一个大问题。

我的想法是:

SELECT ClientID 
from (select ClientID from table1 where Value = 'evergreen')
Where Zip = '75244'

但在这种情况下它不会给我准确的结果我想获得 ClientIDs 111 和 222 的值,因为符合我正在寻找的标准 Street= Evergreen adn Zip=75244

有没有办法做到这一点?

4

3 回答 3

2
Select ClientId from MyTable e
inner join MyTable z On e.clientId = z.ClientID
Where e.value = 'Evergreen' and e.Parameter = 'Street'
and z.parameter = 'Zip' and z.Value = '75244'

只需使用带有连接的别名,这样您就可以“使用您的表两次”

于 2013-08-20T01:19:54.410 回答
1

Try this:

select clientid 
from table1 
where (parameter='Street' and value='Evergreen')
and clientid in (select clientid from table1 where parameter='Zip' and value='75244')
于 2013-08-20T01:10:34.577 回答
0

尝试这个

Select e.ClientId from Table1 e
Where e.value in ('Evergreen','75244')
GROUP BY e.ClientId
HAVING count(distinct e.value) = 2 
于 2013-08-20T19:58:39.333 回答