[英]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
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.