条件列值,选择

时间:2017-02-16 11:56:20

标签: sql oracle

我有2张桌子"记录"和" Char"。用1 - > N关系 我需要使用子查询/连接进行选择,其中在连接列上显示的值是固定字符串,如" Multiple Chars"或内容Char.char_val

让我说明一下:

记录:

     R_ID | Name        Char: C_ID | R_ID | Char_Val
        1    A                 1        3     c1
        2    B                 2        1     c2
        3    C                 3        1     c3
                               4        2     c3

预期结果:

       R_ID | Name  |   Char_Val
        1    A      Multiple Records
        2    B              c3
        3    C              c1

我想我的查询会是这样的:

Select r.R_ID, r.Name, (conditional select) Char_Val
From Records r, Char c
where r.R_ID = c.R_ID

(条件选择)的建议?

3 个答案:

答案 0 :(得分:2)

您可以使用case语句和聚合来获取固定字符串:

case when count(c.c_id) > 1 then 'Multiple Records' else max(c.char_val) end

您需要按r_idname分组:

select r.r_id, r.name,
  case when count(c.c_id) > 1 then 'Multiple Records'
    else max(c.char_val) end as char_val
from records r
join char c on r.r_id = c.r_id
group by r.r_id, r.name
order by r.r_id;    

我也转而使用ANSI连接而不是旧语法(正如@Thorsten建议的那样)。

这是一个使用CTE生成数据的演示,它们的名称略有不同,因为char是一个保留字:

with t_records (r_id, name) as (
  select 1, 'A' from dual
  union all select 2, 'B' from dual
  union all select 3, 'C' from dual
),
t_char (c_id, r_id, char_val) as (
  select 1, 3, 'c1' from dual
  union all select 2, 1, 'c2' from dual
  union all select 3, 1, 'c3' from dual
  union all select 4, 2, 'c3' from dual
)
select r.r_id, r.name,
  case when count(c.c_id) > 1 then 'Multiple Records'
    else max(c.char_val) end as char_val
from t_records r
join t_char c on r.r_id = c.r_id
group by r.r_id, r.name
order by r.r_id;    

      R_ID N CHAR_VAL        
---------- - ----------------
         1 A Multiple Records
         2 B c3              
         3 C c1              

答案 1 :(得分:1)

以下查询会给出您期望的结果( Char_val 以逗号分隔):

  @Mock
    InfoService infoService;
    @InjectMocks
    private InfoController infoController;

    private MockMvc mockMvc;
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(infoController).build();
    }

    @Test
    public void getAllCategoriesTest() throws Exception {
        POSResponse response=new POSResponse();
        Category category=new Category();
        category.setCountryCode(1);
        category.setDescription("Mother Dairy");
        response.setResponse(category);

        when(infoService.getAllCategories("1")).thenReturn(response);

        mockMvc.perform(get("/categories"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("$.id", is(1)))
                .andExpect(jsonPath("$.description", is("Mother Dairy")));

        verify(infoService, times(1)).getAllCategories("1");
        verifyNoMoreInteractions(infoService);
    }

答案 2 :(得分:1)

按r_id分组。 MIN = MAX或您想要多个记录':

select r_id, r.name, c.char_vals
from
(
  select 
    r_id,
    case when min(char_val) = max(char_val) then min(char_val) else 'Multiple Records' end 
      as char_vals
  from char
  group by r_id
) c
join records r using(r_id)
order by r_id;