将不确定的表格筛选器应用于表格

时间:2017-10-14 18:14:05

标签: sql sql-server xml tsql

我在数据库中有一张人口统计记录表。

| biDemographicId | biPostId | vcDemographicType | vcDemographicValue |
|-----------------|----------|-------------------|--------------------|
|               1 |        1 | country           | CA                 |
|               2 |        1 | language          | FR                 |
|               3 |        1 | platform          | IOS                |
|               4 |        2 | country           | US                 |
|               5 |        2 | language          | EN                 |
|               6 |        2 | platform          | IOS                |
|               7 |        3 | country           | US                 |
|               8 |        3 | language          | ES                 |
|               9 |        3 | platform          | WEB                |  

说我想要的唯一记录在哪里:

  • 国家/地区为CA 美国
  • 语言是FR。
  • 平台是IOS。

这些过滤器以XML格式传递,然后制成表格:

<Demographics>
    <Demographic type="country">US</Demographic>
    <Demographic type="country">CA</Demographic>
    <Demographic type="lang">FR</Demographic>
    <Demographic type="platform">IOS</Demographic>
</Demographics>

| vcFilterType | vcFilterValue |
|--------------|---------------|
| language     | FR            |
| country      | CA            |
| country      | US            |
| platform     | IOS           |

我想要的结果如下:

| biDemographicId | biPostId | vcDemographicType | vcDemographicValue |
|-----------------|----------|-------------------|--------------------|
|               1 |        1 | country           | CA                 |
|               2 |        1 | language          | FR                 |
|               3 |        1 | platform          | IOS                |

或更一般地说:

{
  { posts | type=type1 & value=value1,1 } 
  ∪ ... 
  ∪ { posts | type=type1 & value=value1,N }
}
∩
...
∩
{
  { posts | type=typeM & value=valueN,1 } 
  ∪ ... 
  ∪ { posts | type=typeM & value=valueM,N }
}

使用过滤表:

| vcFilterType | vcFilterValue |
|--------------|---------------|
| type1        | value1,1      |
| ...          | ...           |
| type1        | value1,N      |
| ...          | ...           |
| typeM        | valueM,1      |
| ...          | ...           |
| typeM        | valueM,N      |

我的问题是类型和值是变量,所以我不能只做WHERE type = 'country' AND value = 'CA'

1 个答案:

答案 0 :(得分:1)

假设我们已经定义了数据表和过滤表:

DECLARE @DataSource TABLE
(
    [biDemographicId] INT
   ,[biPostId] INT
   ,[vcDemographicType] VARCHAR(12)
   ,[vcDemographicValue] VARCHAR(3)
);

INSERT INTO @DataSource ([biDemographicId], [biPostId], [vcDemographicType], [vcDemographicValue])
VALUES (1, 1, 'country', 'CA')
      ,(2, 1, 'language', 'FR')
      ,(3, 1, 'platform', 'IOS')
      ,(4, 2, 'country', 'US')
      ,(5, 2, 'language', 'EN')
      ,(6, 2, 'platform', 'IOS')
      ,(7, 3, 'country', 'US')
      ,(8, 3, 'language', 'ES')
      ,(9, 3, 'platform', 'WEB');

DECLARE @FilterDataSource TABLE
(
    [vcFilterType] VARCHAR(12)
   ,[vcFilterValue] VARCHAR(3)
);

INSERT INTO @FilterDataSource ([vcFilterType], [vcFilterValue])
VALUES ('language', 'FR')
      ,('country', 'CA')
      ,('country', 'US')
      ,('platform', 'IOS');

我们可以做的是计算DISTINCT过滤类型 - 例如,在您的情况下,我们有3个不同的过滤类型 - languagecountry和{{ 1}}

我们只需要获取这些与这三种类型的值匹配的记录:

platform.

那就是:

enter image description here