我有两个表,一个是住宿,一个是hotelpaymententry我想要的是将两个表组合而不重复相同的enquiry_id。下面是我的表格,并查询我尝试的内容及其结果: 表住宿
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
表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
我尝试加入n union以获得结果,但内部联接仅返回常见记录并且左和右右连接乘以记录并返回5 * 3记录,并且union返回两个表记录重复enquiry_id RH - 10/03/2014 - 3表示总共8条记录。我想要的是来自两个表的记录,但如果两个表中的enquiryid匹配记录出现一次,如果记录在其自己的表中是唯一的,则在输出表中出现a =。迫切需要帮助。感谢所有人提前帮助。
答案 0 :(得分:0)
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
答案 1 :(得分:0)
要获取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
要从两个表中获取记录,您可以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
答案 2 :(得分:0)
当表格没有正确规范化时,经常会发生这种情况。
最好的解决方案是正确地规范你的桌子 关系。
这不是联盟或加入或不同的情况。
看,我会尝试这样的事,
;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