繁体   English   中英

LISTAGG函数给我重复项

[英]LISTAGG function gives me duplicates

此查询中的每个区号都对应一个以上的州。 LISTAGG非常适合串联每个分区#的州列表,但是它给了我重复的州。 我怎样才能解决这个问题? 我应该补充一点,之所以会得到重复,是因为每个地区的零售商店数量很多,所以我在每个地区的许多州内都有多家商店。 但我想看看是否有一种方法来获取唯一状态...

SELECT  distinct DISTRICT_NBR,
        LISTAGG( str_state_abbr, ',') 
          WITHIN GROUP (order by str_state_abbr) AS States
from DIM_LOCATION
where DISTRICT_NBR in (1, 3, 4, 7, 8, 9, 10, 11, 12, 15, 16, 17, 18, 20, 21, 22, 23, 25, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 44, 45, 46, 47, 48, 50, 52, 53, 54, 55, 56, 57, 58, 59, 100, 101, 102, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 122, 123, 124, 125, 126, 127, 128, 130, 131, 134, 135, 136, 140, 143, 152, 153, 154, 155, 156, 157, 158, 159, 160, 163, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 184, 185, 186, 188, 189, 190, 191, 193, 194, 195, 196, 198, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 221, 222, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 249, 250, 251, 252, 253, 254, 255, 256, 258, 259, 260, 261, 263, 266, 267, 268, 270, 271, 274, 275, 276, 277, 282, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 297, 300, 302, 304, 305, 306, 307, 308, 310, 311, 313, 315, 316, 317, 318, 319, 324, 325, 326, 327, 328, 330, 351, 352, 354, 355, 358, 359, 362, 364, 365, 366, 367, 369, 370, 371, 372, 373
)
and STR_STATE_ABBR is not null
group by DISTRICT_NBR
order by DISTRICT_NBR
select  district_nbr,
        listagg( str_state_abbr, ',')         
          within group (order by str_state_abbr) as states          
from (select distinct district_nbr, str_state_abbr from dim_location
       where district_nbr in (1, 3, 4, 7, 8, 9, 10, 11, 12, 15, 16, 17, 18, 20, 21, 22, 23, 25, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 44, 45, 46, 47, 48, 50, 52, 53, 54, 55, 56, 57, 58, 59, 100, 101, 102, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 122, 123, 124, 125, 126, 127, 128, 130, 131, 134, 135, 136, 140, 143, 152, 153, 154, 155, 156, 157, 158, 159, 160, 163, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 184, 185, 186, 188, 189, 190, 191, 193, 194, 195, 196, 198, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 221, 222, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 249, 250, 251, 252, 253, 254, 255, 256, 258, 259, 260, 261, 263, 266, 267, 268, 270, 271, 274, 275, 276, 277, 282, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 297, 300, 302, 304, 305, 306, 307, 308, 310, 311, 313, 315, 316, 317, 318, 319, 324, 325, 326, 327, 328, 330, 351, 352, 354, 355, 358, 359, 362, 364, 365, 366, 367, 369, 370, 371, 372, 373)
         and str_state_abbr is not null
) 
group by district_nbr
order by district_nbr;

暂无
暂无

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

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