简体   繁体   English

sql查询返回一行结果

[英]sql query to return the result in one row

I am trying to write the query for existing table data. 我正在尝试为现有表数据编写查询。 TABLE_A and TABLE_B has one record but TABLE_C has 2 records. TABLE_A和TABLE_B有一个记录,而TABLE_C有2个记录。 One for Home address and other is for Work address. 一个用于家庭地址,另一个用于工作地址。

So the following query returns 2 records. 因此,以下查询返回2条记录。 I am trying to get only one record out of 2 records. 我正在尝试仅从2条记录中获得一条记录。

If CITY is NULL, state_id is null for address_type = 1(Home) then get Work(address_type = 2) address. 如果CITY为NULL,则address_type = 1(Home)的state_id为null,然后获取Work(address_type = 2)地址。 If both are null then get 'Home' address. 如果两者均为空,则获取“家庭”地址。 What is the best way to achieve this functionality. 什么是实现此功能的最佳方法。

Thank you for any suggestion. 感谢您的任何建议。

select a.A_ID, a.B_ID, a.A_DESC, b.first_name, b.last_name, c.address_type,  c.city, c.state
from table_A a
left join table_B b on b.B_ID = a.B_ID
left join table_C c on c.B_id = b.B_id
where a.A_ID = 10

TABLE_A TABLE_A

A_ID int
B_ID int 
A_Desc  varchar(20)

TABLE_B TABLE_B

B_ID int
first_name  varchar(30)
last_name   varchar(30)

TABLE_C TABLE_C

C_ID    int
B_ID    int 
address_type    int 
city      varchar(50)
state  int

Result: 结果:

 A_ID     B_ID   A_DESC   first_name  last_name  address_type   city       state
 --------------------------------------------------------------------------------
 10       200    test_     name1        name_last    1            NULL       NULL 
 10       200    test_     name1        name_last    2           City_test    2

I want this final result 我想要这个最终结果

 A_ID     B_ID   A_DESC   first_name  last_name  address_type   city       state
 --------------------------------------------------------------------------------
 10       200    test_     name1        name_last    2           City_test    2

You could use outer apply to look up the address with the highest type: 您可以使用outer apply查找具有最高类型的地址:

select  *
from    table_A a
left join 
        table_B b 
on      b.B_ID = a.B_ID
outer apply
        (
        select  top 1 *
        from    table_C c 
        where   c.B_id = b.B_id
        order by
                case
                when c.address_type = 2 and c.city is not null then 1
                else c.address_type = 1 and c.city is not null then 2
                end
        ) c
where   a.A_ID = 10

Just build this logic into your join condition for left joins to tables b and c. 只需将此逻辑构建到您的联接条件中即可对表b和c进行左联接。

If CITY is NULL, state_id is null for address_type = 1(Home) then get Work(address_type = 2) address. 如果CITY为NULL,则address_type = 1(Home)的state_id为null,然后获取Work(address_type = 2)地址。 If both are null then get 'Home' address 如果两者都为空,则获取“家庭”地址

select a.A_ID, a.B_ID, a.A_DESC, b.first_name, b.last_name,
       coalesce(c1.address_type,c2.address_type) address_type, 
       coalesce(c1.city,c2.city) city, 
       coalesce(c1.state,c2.state) state
from table_A a
left join table_B b on b.B_ID = a.B_ID
left join table_C c1 on c.B_id = b.B_id and c.Address_Type = 1
left join table_C c2 on c.B_id = b.B_id and c.Address_Type = 2

where a.A_ID = 10

the general technique: 通用技术:

select distinct a.A_ID
, a.B_ID
, a.A_DESC
, b.first_name
, b.last_name
, coalesce(home.address_type, work.address_type)
, coalesce(home.city,work.city)
, coalesce(home.state, work.state) 
from table_A a 
left join table_B b on b.B_ID = a.B_ID 
left join table_C home on home.B_id = b.B_id and home.address_type = 1
left join table_C work on work.B_id = b.B_id and home.address_type = 2
where a.A_ID = 10 

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

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