简体   繁体   English

当一个表的约束键具有很少的空值时,如何合并两个表的结果

[英]How to combine results of two tables when constraint key of one table has few null values

I am having two tables one is accomodation and one is hotelpaymententry what i want is to combine both the tables without repeating same enquiry_id. 我有两个表,一个是住宿,一个是hotelpaymententry,我想要的是将两个表合并而不重复相同的enquiry_id。 below are my tables and query what i tried and its result: Table Accomodation 以下是我的表,并查询我尝试过的内容及其结果:表容纳

Enquiry_Id             Checkin      CheckOut    hotelname              Amount
RH - 26/02/2014 - 10 2014-03-18 2014-03-19  Spice Routes Houseboats     15500
RH - 04/03/2014 - 1  2014-03-19 2014-03-20  Spice Routes Houseboats     9000
RH - 04/03/2014 - 2  2014-03-17 2014-03-18  Spice Routes Houseboats     24000
RH - 05/03/2014 - 6  2014-04-04 2014-04-05  Spice Routes Houseboats     9000
RH - 10/03/2014 - 3  2014-06-17 2014-06-18  Spice Routes Houseboats     33000

Table HotelPyamentEntry 餐桌酒店

![enter image description here][1]

 Hotel                   EnquiryId         Checkin    CheckOUt   Totalamount  paynow  dateofpayment 

Spice Routes Houseboats     NULL             NULL       NULL        NULL        5000    2014-12-09
Spice Routes Houseboats     NULL             NULL       NULL        NULL        6000    2014-12-10
Spice Routes Houseboats RH - 10/03/2014 - 3  2014-06-17 2014-06-18  33000      15000    2014-12-08

i tried join n union both for the result but inner join returns only common records and left & right join multiplies the records and return 5*3 records and union returns both tables records repeating the enquiry_id RH - 10/03/2014 - 3 means total 8 records. 我尝试对结果进行联接n联合,但内部联接仅返回公共记录,左右联接将记录相乘并返回5 * 3记录,联合返回两个表记录并重复enquiry_id RH-10/03/2014-3表示总计8条记录。 what i want is record from both tables but if enquiryid matches in both tables record appears once and if record is unique in its own table it appears a=in output table. 我想要的是来自两个表的记录,但是如果两个表中的enquiryid匹配记录都出现一次,并且如果记录在其自己的表中是唯一的,那么它将在输出表中出现。 help is badly needed. 急需帮助。 thanks to all for help in advance. 感谢所有人的帮助。

在此处输入图片说明

  select Enquiry_Id,Checkin,CheckOut,hotelname,Amount from Table Accomodation
    union //union doesn't take redundant data
    select Enquiry_Id,Checkin,CheckOut,hotelname,Amount from Table HotelPyamentEntry

To get all records from accomodation and matching records from HotelPyamentEntry you can write as: 要从accomodation获取所有记录并从HotelPyamentEntry获取匹配记录,您可以编写为:

SELECT A.Enquiry_Id  
,A.Checkin 
,A.CheckOut 
,A.hotelname 
,A.Amount
,H.paynow
,H.dateofpayment
FROM @Accomodation A 
outer apply
(
SELECT hotel 
,Enquiry_Id  
,Checkin 
,CheckOut 
,Totalamount 
,paynow 
,dateofpayment 
FROM @HotelPyamentEntry HP
WHERE HP.Enquiry_Id = A.Enquiry_Id )AS H

and to get records from both the tables you can do a FULL join as 并从两个表中获取记录,您可以执行FULL join

SELECT DISTINCT ISNULL(A.Enquiry_Id  ,H.Enquiry_Id)AS Enquiry_Id
,ISNULL(A.Checkin ,H.Checkin)AS Checkin
,ISNULL(A.CheckOut ,H.CheckOut)AS CheckOut
,ISNULL(A.hotelname ,H.hotel)AS hotelname
,ISNULL(A.Amount,H.Totalamount)AS Amount
,H.paynow
,H.dateofpayment
FROM @Accomodation A
FULL JOIN @HotelPyamentEntry H ON A.Enquiry_Id= H.Enquiry_Id

DEMO 演示

one often struck when tables are not properly normalize. 当表未正确归一化时,经常会发生这种情况。

Best solution would be to normalize your table with proper relationship . 最好的解决方案是使用适当的关系规范化您的表。

It is not a case of Union or join or distinct . 这不是联合,联合或独立的情况。

See I will try something like this, 看我会尝试这样的事情,

;With CTE As
(
SELECT  A.Enquiry_Id  AS Enquiry_Id
,A.Checkin AS Checkin
,A.CheckOut CheckOut
,A.hotelname  hotelname
,Amount AS Amount
,0 paynow
,0 dateofpayment
FROM @Accomodation A
UNION all
SELECT H.Enquiry_Id 
,H.Checkin
,H.CheckOut
,H.hotelname
,H.Totalamount
,H.paynow
,H.dateofpayment


FROM @HotelPyamentEntry H 
)
-- analyze this and check which rows you want and which which rows tyou want to eliminate and why.
--then accordsingly put ranking function with partition by and order

select * 
ROW_NUMBER(partition by order by )rownum 
from CTE

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

相关问题 如果联接列中的值为空,则联接两个表并仅在一个表上产生结果 - Joining two tables and producing results only on one table when there are null values on joining column 如何联接Oracle中的三个表,其中一个表在外键列中具有空值? - How to join three tables in Oracle where one of the table has null values in the foreign key column? 当一个具有空值时联接两个表 - join two tables when one has null values 当一个表具有大量数据而另一个表具有少量数据时,在SQL Server中联接两个表 - Joining two tables in SQL Server when one table has huge data and other has few data 当一个表有多个对应值时连接两个表 - Joining two tables when one table has multiple corresponding values 如何合并两个表并将其值计为一个? - how to combine two tables and count their values as one? 如何连接两个表:一个表有'text'键,另一个有'number'键 - how to join two tables:one table has 'text' key, the other has 'number' key 当一个表具有多个值时如何联接两个表 - How to join two tables when one has multiple values 需要一条 select 语句来连接通过联结表连接的两个表,但其中一个具有 null 值 - Need a select statement to join two tables that are connected by a junction table, but one has null values 如何合并两个表以从MySQL中的不同表获取结果 - How to combine two tables to get results from different table in MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM