繁体   English   中英

将不确定的列表过滤器应用于表

[英]Apply indeterminate, tabulated filters to a table

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

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

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

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不同的过滤类型- languagecountryplatform.

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

DECLARE @DistinctFilteringCriteria TINYINT;

SELECT @DistinctFilteringCriteria = COUNT(DISTINCT [vcFilterType])
FROM @FilterDataSource;

WITH DataSource AS
(
    SELECT DS.*
          ,COUNT([vcDemographicType]) OVER (PARTITION BY [biPostId]) AS [FilteringCriteriaMatched]
    FROM @DataSource DS
    INNER JOIN @FilterDataSource FDS
        ON DS.[vcDemographicType] = FDS.[vcFilterType]
        AND DS.[vcDemographicValue] = FDS.[vcFilterValue]
)
SELECT [biDemographicId], [biPostId], [vcDemographicType], [vcDemographicValue]
FROM DataSource
WHERE [FilteringCriteriaMatched] = @DistinctFilteringCriteria; 

而已:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM