Sql server 具有多个结果的子查询
我正在使用Sql Server,我编写了一个查询。当我执行该查询时,出现以下错误 错误: “Msg 512,第16级,状态1,第1行 子查询返回的值超过1个。当子查询后跟=、!=、=或子查询用作表达式时,不允许这样做。“ 最后我知道了为什么会出现这个错误。我正在尝试从子查询返回多个结果Sql server 具有多个结果的子查询,sql-server,Sql Server,我正在使用Sql Server,我编写了一个查询。当我执行该查询时,出现以下错误 错误: “Msg 512,第16级,状态1,第1行 子查询返回的值超过1个。当子查询后跟=、!=、=或子查询用作表达式时,不允许这样做。“ 最后我知道了为什么会出现这个错误。我正在尝试从子查询返回多个结果 (SELECT TOP 2 CAST(isbreakfast AS VARCHAR(10)) + '_' + room_desc FROM tbl_hotel_room_booking WHERE pb.prod
(SELECT TOP 2 CAST(isbreakfast AS VARCHAR(10)) + '_' + room_desc FROM tbl_hotel_room_booking WHERE pb.product_booking_id = product_booking_id) 'room_desc',
任何人都可以帮助我如何在这里返回多个结果。如果您基本上离开了,请将查询与该表连接,并为前2条记录设置筛选器,我想您会得到想要的结果
SELECT
b.booking_id,
b.booking_ref,
b.destination,
b.room_format,
b.checkin,
b.checkout,
b.booking_status,
b.currency,
b.rooms,
b.booking_date,
b.adults,
b.childs,
b.seniors,
b.youth,
b.child2,
b.booking_type,
pb.amount_charged,
pb.original_total_cost 'total_amount',
b.special_request,
b.cancelled_date,
b.emergency_contact,
b.emergency_email,
CAST(brb.isbreakfast AS VARCHAR(10)) + '_' + brb.room_desc AS 'room_desc'
tba.traveller_address1,
tba.traveller_address2,
tba.traveller_city,
tba.traveller_state,
tba.traveller_country,
tba.traveller_zip_code,
th.transaction_amount,
th.reversal_amount,
th.branch_id,
th.agent_id,
th.transaction_type,
pb.product_booking_id,
pb.product_status,
pb.provider_confirmation_code,
pb.booking_source,
pb.product_name,
pb.product_type,
pb.product_id,
tft.ticket
(SELECT TOP 1 as_of_date + '_' + hotel_penalty FROM tbl_hotel_room_cancellation WHERE booking_id='0BCE1ABC-EA42-4030-A9F1-CA8D176825C0') 'as_of_date',
tpd.adult_price,tpd.cost_currency,tpd.sell_currency,tpd.booking_fee,tpd.taxes,tpd.exchange_rate,tpd.subagency_markup_val,
tpd.charged_amount,tpd.child_price,tpd.senior_price,tpd.youth_price, tpd.actual_price,(ISNULL(tpd.markup_total,0) + ISNULL(tpd.subagency_markup_val,0)) 'markup_total', tpd.markup_total 'agency_markup'
FROM tbl_booking b
INNER JOIN tbl_product_booking pb ON b.booking_id = pb.booking_id
LEFT JOIN tbl_hotel_room_booking brb ON pb.product_booking_id = brb.product_booking_id
LEFT JOIN tbl_transaction_history th ON b.booking_ref = th.reference_number
LEFT JOIN tbl_traveller_billing_address tba ON b.booking_id = tba.booking_id
LEFT JOIN tbl_purchasedproduct_details tpd ON b.booking_id = tpd.booking_id
LEFT JOIN tbl_flight_tickets tft ON b.booking_id = tft.booking_id
WHERE b.booking_id = '0BCE1ABC-EA42-4030-A9F1-CA8D176825C0'
AND brb.isbreakfast IN (Select Top 2 IsBreakfast
FROM tbl_hotel_room_booking)
AND (th.transaction_type IS NULL
OR LOWER(th.transaction_type) = 'booking'
OR LOWER(th.transaction_type) = 'creditcardbooking' OR Lower(th.transaction_type) = 'manualbooking')
如果我有两个房间具有相同的产品id,那么我需要返回两个房间的描述。很多事情你需要处理1。建议不要使用select子句中的子查询2。没有order by的top子句几乎没有意义,无法获取错误..Msg 4104,16级,状态1,第52行多部分标识符“b.booking_id”无法绑定。Msg 4104,级别16,状态1,第54行无法绑定多部分标识符“b.booking_ref”。Msg 4104,级别16,状态1,第56行无法绑定多部分标识符“b.booking_id”。Msg 4104,级别16,状态1,第58行无法绑定多部分标识符“b.booking_id”。Msg 4104,级别16,状态1,第60行无法绑定多部分标识符“b.booking_id”。抱歉,我不想这样。我想要用逗号分隔房间描述..我现在已经用过了,但是我如何在上面的查询中使用这些行` DECLARE@Names VARCHAR(8000)SELECT@Names=COALESCE(@Names+',','')++COALESCE(房间描述,CAST(isbreakas VARCHAR(10)))从tbl_hotel_room_booking,其中product_booking_id=74097选择@names`您不能同时选择和设置变量。您可以删除select变量并放置新的declare vraiable(名称),它应该看起来像select@names=COALESCE(room_desc,CAST(isbreakn AS VARCHAR(10)),并且您应该在查询结束时使用yout where语句;其中b.booking\u id=0BCE1ABC-EA42-4030-A9F1-CA8D176825C0,tbl\u hotel\u room\u booking其中product\u booking\u id=74097,brb.IsBreash。。。
SELECT
b.booking_id,
b.booking_ref,
b.destination,
b.room_format,
b.checkin,
b.checkout,
b.booking_status,
b.currency,
b.rooms,
b.booking_date,
b.adults,
b.childs,
b.seniors,
b.youth,
b.child2,
b.booking_type,
pb.amount_charged,
pb.original_total_cost 'total_amount',
b.special_request,
b.cancelled_date,
b.emergency_contact,
b.emergency_email,
CAST(brb.isbreakfast AS VARCHAR(10)) + '_' + brb.room_desc AS 'room_desc'
tba.traveller_address1,
tba.traveller_address2,
tba.traveller_city,
tba.traveller_state,
tba.traveller_country,
tba.traveller_zip_code,
th.transaction_amount,
th.reversal_amount,
th.branch_id,
th.agent_id,
th.transaction_type,
pb.product_booking_id,
pb.product_status,
pb.provider_confirmation_code,
pb.booking_source,
pb.product_name,
pb.product_type,
pb.product_id,
tft.ticket
(SELECT TOP 1 as_of_date + '_' + hotel_penalty FROM tbl_hotel_room_cancellation WHERE booking_id='0BCE1ABC-EA42-4030-A9F1-CA8D176825C0') 'as_of_date',
tpd.adult_price,tpd.cost_currency,tpd.sell_currency,tpd.booking_fee,tpd.taxes,tpd.exchange_rate,tpd.subagency_markup_val,
tpd.charged_amount,tpd.child_price,tpd.senior_price,tpd.youth_price, tpd.actual_price,(ISNULL(tpd.markup_total,0) + ISNULL(tpd.subagency_markup_val,0)) 'markup_total', tpd.markup_total 'agency_markup'
FROM tbl_booking b
INNER JOIN tbl_product_booking pb ON b.booking_id = pb.booking_id
LEFT JOIN tbl_hotel_room_booking brb ON pb.product_booking_id = brb.product_booking_id
LEFT JOIN tbl_transaction_history th ON b.booking_ref = th.reference_number
LEFT JOIN tbl_traveller_billing_address tba ON b.booking_id = tba.booking_id
LEFT JOIN tbl_purchasedproduct_details tpd ON b.booking_id = tpd.booking_id
LEFT JOIN tbl_flight_tickets tft ON b.booking_id = tft.booking_id
WHERE b.booking_id = '0BCE1ABC-EA42-4030-A9F1-CA8D176825C0'
AND brb.isbreakfast IN (Select Top 2 IsBreakfast
FROM tbl_hotel_room_booking)
AND (th.transaction_type IS NULL
OR LOWER(th.transaction_type) = 'booking'
OR LOWER(th.transaction_type) = 'creditcardbooking' OR Lower(th.transaction_type) = 'manualbooking')