在下面的select语句中我有707行
select detail_serial, Price from apa_invoice_detail
我还有另一个选择语句
Select max(detail_serial)
,asc_item.item_name_2
,asc_group.group_name_2
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2
这个给了我197行
我需要从第二个语句中的detail_serial的第一个语句中获取价格(仅适用于197行)
我试过了:
select detail_serial, Price from apa_invoice_detail where detail_serial in
(Select max(detail_serial)
,asc_item.item_name_2
,asc_group.group_name_2
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2)
但它给了我"当子查询没有用EXISTS引入时,只能在选择列表中指定一个表达式。"
我该怎样解决这个问题?
答案 0 :(得分:1)
尝试删除内部select语句中的列
select detail_serial, Price from apa_invoice_detail where detail_serial in
(Select max(detail_serial)
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2)
答案 1 :(得分:0)
使用EXISTS
代替IN
。
;WITH SecondQueryResults AS
(
Select max(detail_serial) detail_serial
, asc_item.item_name_2
, asc_group.group_name_2
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2
)
select
detail_serial,
Price
from
apa_invoice_detail T
where
EXISTS (SELECT 'serial exists on query result' FROM SecondQueryResults AS S WHERE S.detail_serial = T.detail_serial)
...或者只选择detail_serial
子查询中的1列(IN
)。