如何从具有这种结构的表中sql中获取值?

时间:2018-11-22 14:35:22

标签: sql sql-server

表(浏览器)

+-------+-------------+----------------+--------+
| Users | Application |      Name      | Value  |
+-------+-------------+----------------+--------+
| A1    | AA          | browsername    | IE     |
| A1    | AA          | browserVersion | 11     |
| A1    | AA          | browsername    | chrome |
| A1    | AA          | browserVersion | 60     |
| A1    | AA          | browsername    | safari |
| A1    | AA          | browserVersion | 11     |
| A2    | AA          | browsername    | IE     |
| A2    | AA          | browserVersion | 10     |
| A2    | AA          | browsername    | safari |
| A2    | AA          | browserVersion | 10     |
| A3    | AA          | browsername    | IE     |
| A3    | AA          | browserVersion | 9      |
| A3    | AA          | browsername    | chrome |
| A3    | AA          | browserVersion | 60     |
| A4    | AA          | browsername    | safari |
| A4    | AA          | browserVersion | 11     |
| A4    | AA          | browsername    | IE     |
| A4    | AA          | browserVersion | 10     |
+-------+-------------+----------------+--------+

用户可以通过任何浏览器使用该应用程序。 我想调查使用11版本IE浏览器的用户。我无法使用它,因为Safari和IE具有相同的版本。 有人可以建议我如何获取所需的数据。

2 个答案:

答案 0 :(得分:2)

您可以使用条件聚合:

select user, application
from t
where name in ('browsername', 'browserVersion')
group by user, application
having sum(case when name = 'browsername' and value = 'IE' then 1 else 0 end) > 0 and
       sum(case when name = 'browserVersion' and value = '11' then 1 else 0 end) > 0 ;

答案 1 :(得分:0)

首先,为了简化起见,创建一列“位置”,并确保它们在插入时已编号,就像这样:

+-------+-------+-------------+----------------+--------+
|  pos  | Users | Application |      Name      | Value  |
+-------+-------+-------------+----------------+--------+
|  1    | A1    | AA          | browsername    | IE     |
|  2    | A1    | AA          | browserVersion | 11     |
|  3    | A1    | AA          | browsername    | chrome |
|  4    | A1    | AA          | browserVersion | 60     |
|  5    | A1    | AA          | browsername    | safari |
|  6    | A1    | AA          | browserVersion | 11     |
|  7    | A2    | AA          | browsername    | IE     |
|  8    | A2    | AA          | browserVersion | 10     |
|  9    | A2    | AA          | browsername    | safari |
|  10   | A2    | AA          | browserVersion | 10     |
|  11   | A3    | AA          | browsername    | IE     |
|  12   | A3    | AA          | browserVersion | 9      |
|  13   | A3    | AA          | browsername    | chrome |
|  14   | A3    | AA          | browserVersion | 60     |
|  15   | A1    | AA          | browsername    | safari |
|  16   | A1    | AA          | browserVersion | 11     |
|  17   | A1    | AA          | browsername    | IE     |
|  18   | A1    | AA          | browserVersion | 10     |
+-------+-------+-------------+----------------+--------+

现在,您可以使用CTE和Lead功能来获取所需的内容。

;with X as ( 
   select users, application, name, value as Browser, lead (value) over (order by pos) as Version 
   from table 
   where name = 'browsername'
)
select * 
from X
where browser = 'IE' and version = '11'

PS:始终创建一个身份列,从长远来看,它总是有回报的