1

我需要创建一个报告,用户可以在区域 > 区 > 委员会 > 事件中切换数据。我使用无法更改的集合数据视图。我需要定义一个区和一个区域,因为它们在数据库中没有正确定义,然后将它们相互关联。我已经接近了,因为我能够分配 17 个不同的“地区代码”——是的,他们有代码,但没有正确的描述——使用 CASE 语句来组成区域:

CASE WHEN DistrictCode LIKE 'DST10%' THEN 4
WHEN DistrictCode LIKE 'DST13%' THEN 2
WHEN DistrictCode LIKE 'DST1%' THEN 2
WHEN DistrictCode LIKE 'DST2%' THEN 2
WHEN DistrictCode LIKE 'DST3%' THEN 3
WHEN DistrictCode LIKE 'DST5%' THEN 3
WHEN DistrictCode LIKE 'DST7%' THEN 2
WHEN DistrictCode LIKE 'DST8%' THEN 1
WHEN DistrictCode LIKE 'DSTC4%' THEN 3
WHEN DistrictCode LIKE 'DSTC6%' THEN 2
WHEN DistrictCode LIKE 'DSTC9%' THEN 1
WHEN DistrictCode LIKE 'DT11%' THEN 4
WHEN DistrictCode LIKE 'DT12%' THEN 4
WHEN DistrictCode LIKE 'DT15%' THEN 4
WHEN DistrictCode LIKE 'DT16%' THEN 4
WHEN DistrictCode LIKE 'DT17%' THEN 4
WHEN DistrictCode LIKE 'UP17%' THEN 4
WHEN DistrictCode LIKE 'UPL11%' THEN 4
ELSE 5
END AS Region

使用“地区描述”数据,我可以创建地区:

CASE WHEN DistrictDesc IN ('1' , '1A' , '1B') THEN 'District 1' 
    WHEN DistrictDesc IN ('2' , '2A' , '2B' , '2C') THEN 'District 2'
    WHEN DistrictDesc IN ('3' , '3A' , '3B') THEN 'District 3'
    WHEN DistrictDesc IN ('4' , '4A' , '4B' , '4C' , '4D' , '4E' , '4F') THEN 'District 4'
    WHEN DistrictDesc IN ('5' , '5A' , '5B' , '5C') THEN 'District 5'
    WHEN DistrictDesc IN ('6' , '6A') THEN 'District 6'
    WHEN DistrictDesc IN ('7' , '7A' , '7B') THEN 'District 7'
    WHEN DistrictDesc IN ('8' , '8A' , '8B' , '8C') THEN 'District 8'
    WHEN DistrictDesc IN ('9' , '9A') THEN 'District 9'
    WHEN DistrictDesc IN ('10' , '10A' , '10B' , '10C') THEN 'District 10'
    WHEN DistrictDesc IN ('11' , '11A' , '11B' , '11C') THEN 'District 11'
    WHEN DistrictDesc IN ('12' , '12A' , '12B' , '12C') THEN 'District 12'
    WHEN DistrictDesc IN ('13' , '13A') THEN 'District 13'
    WHEN DistrictDesc IN ('14' , '14A' , '14B' , '14C' , '14D') THEN 'District 14'
    WHEN DistrictDesc IN ('15' , '15A' , '15B' , '15C' , '15D') THEN 'District 15'
    WHEN DistrictDesc IN ('16' , '16A' , '16B' , '16C') THEN 'District 16'
    WHEN DistrictDesc IN ('17' , '17A' , '17B') THEN 'District 17'
    ELSE ISNULL (DistrictDesc,'No District')
    END AS District

这适用于 SSMS 以返回查询,但是当我在 VS Report Builder 中预览报告时,我需要将 Region 设置为报告的参数。无论是设置区域值还是让它从查询中检索值,我都会在区域预览中的下拉列表中获得 1、2、3、4 和 5(我的区域)的倍数。

我怎样才能让它只为 1、2、3、4 或 5 返回一次?有没有办法可以结合这些。如果我使用 Distinct 那么我会丢失一些数据,不是吗?我尝试使用 IN 并列出每个地区代码,但返回了相同的问题。

4

1 回答 1

0

您上面的代码不可维护。

您应该创建一个映射您的代码和描述的地区表,并加入该表,并将其用作参数的来源。这将具有解决您的问题和改进您的查询的双重好处

于 2012-10-18T08:14:41.100 回答