LISTAGG功能给我重复

时间:2014-10-24 14:37:27

标签: sql oracle11g

此查询中的每个区域#对应多个状态。 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

1 个答案:

答案 0 :(得分:0)

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;