SQL Server查询同一表中的值

[英]SQL Server Query for values within same Table


I have a data table (not very well structured) in which I have the following

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

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

For this I want to Select all those Client ID that are on Street = Evergreen BUT also with ZIP 75244, I have over 700K rows so, exporting all would be a big issue.

为此,我想选择街道上的所有客户端ID =常青树也是ZIP 75244,我有超过700K行,所以导出所有这将是一个大问题。

My idea was:

我的想法是:

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

But it wont give me the accurate results in this case I would like to get the values for ClientIDs 111 and 222 because the match the criteria Im looking for Street= Evergreen adn Zip=75244

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

Is there a way to do this?

有没有办法做到这一点?

3 个解决方案

#1


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

#2


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'

Just use an alias with a join so you can "use your table twice"

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

#3


0  

Try this

Select e.ClientId from Table1 e
Where e.value in ('Evergreen','75244')
GROUP BY e.ClientId
HAVING count(distinct e.value) = 2 

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/08/20/9fa248ed7cb47af12329ecf5efdeb351.html



 
  © 2014-2022 ITdaan.com 联系我们: