select 可以搞定 select distinct ITEM_NO ,...., min(price) from table group by ITEM_NO
to mfj(): 你確實錯了. select distinct ITEM_NO ,...., min(price) from table group by ITEM_NO 這條語句會報錯的.
他的環境就是SQLServer.是Oracle也是一樣的錯.你這樣寫報的錯是: Column VENDOR_NO is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.為什麼呢. 因為SQLServer不能確定是那個值的VENDOR_NO.
如果只是這樣. select distinct ITEM_NO, min(price) from table group by ITEM_NO 是可以通過的,而且distinct都可以不要. 但是還有一個VENDOR_NO是通過分組和distinct都不能造成唯一值的. 而ITEM_NO和min(price)按ITEM_NO分組後都是每組的唯一的值. 因此語法上就不能通過.
select * from table where (item_no,price) in (select item_no,min(price) from table group by item_no)
select distinct item_no,VENDOR_NO,PRICE from table where item_no in (select item_no,min(price) from table group by item_no)
db2_seeker(小顺) 用的是什么数据库, 我的是SQL Server
你的要求没写清楚,db2_seeker(小顺)应该用的是ORACLE,但他写的会显示出两条最小price的记录,但你没有说明应该显示出哪个verdor_no的min(price)的记录。 create table test ( item_no number(9,0) not null, verdor_no varchar2(10) not null, price number(9,2) not null, other_column number(9,0));insert into test( item_no , verdor_no , price ) values( 1, 'v1000', 2); insert into test( item_no , verdor_no , price ) values( 2, 'v2000', 6); insert into test( item_no , verdor_no , price ) values( 3, 'v3000', 5); insert into test( item_no , verdor_no , price ) values( 4, 'v4000', 7); insert into test( item_no , verdor_no , price ) values( 5, 'v5000', 8);commit; select * from test a where (a. price, a.item_no) in (select min(b.price), b.Item_no from test b group by b.item_no) order by verdor_no; ITEM_NO VERDOR_NO PRICE OTHER_COLUMN -------- ---------- ---------- ------------ 1 v1000 2 2 v1000 2 1 v3000 2 你可以在结果集上过滤得到你所要的。
一、 二 、 三 问题 全部要求SELECT 出来。(注: 一定是做得到的, 而且不很长) 现想找出一个最经典的来。例: select aa.item_no,min(vendor_no),aa.price,min(aa.aa) from aa where aa.item_no+convert(char(10),price) in (select item_no+convert(char(10),min(price)) from aa group by item_no) group by item_no,price 一个较普通的。还有更好的
我可能理解错了ALLTEC(荔枝)的意思,不过我觉得那样完全可以解决类似的问题了。by the way, 我用的是,哎,db2
select distinct ITEM_NO ,...., min(price) from table group by ITEM_NO
你確實錯了.
select distinct ITEM_NO ,...., min(price) from table group by ITEM_NO
這條語句會報錯的.
Column VENDOR_NO is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.為什麼呢.
因為SQLServer不能確定是那個值的VENDOR_NO.
先建張表,然後插8條紀錄,然後再run你的語句.
你看看會不會報我剛才貼的錯誤.如果不報錯,我這輩子就不玩數據庫了.
select distinct ITEM_NO, min(price) from table group by ITEM_NO
是可以通過的,而且distinct都可以不要.
但是還有一個VENDOR_NO是通過分組和distinct都不能造成唯一值的.
而ITEM_NO和min(price)按ITEM_NO分組後都是每組的唯一的值.
因此語法上就不能通過.
,我想应该可以通过表的进一步细分来解。
from table where item_no in (select item_no,min(price) from table group by item_no)
create table test (
item_no number(9,0) not null,
verdor_no varchar2(10) not null,
price number(9,2) not null,
other_column number(9,0));insert into test(
item_no ,
verdor_no ,
price )
values( 1, 'v1000', 2);
insert into test(
item_no ,
verdor_no ,
price )
values( 2, 'v2000', 6);
insert into test(
item_no ,
verdor_no ,
price )
values( 3, 'v3000', 5);
insert into test(
item_no ,
verdor_no ,
price )
values( 4, 'v4000', 7);
insert into test(
item_no ,
verdor_no ,
price )
values( 5, 'v5000', 8);commit;
select * from test a
where (a. price, a.item_no) in
(select min(b.price), b.Item_no from test b
group by b.item_no)
order by verdor_no;
ITEM_NO VERDOR_NO PRICE OTHER_COLUMN
-------- ---------- ---------- ------------
1 v1000 2
2 v1000 2
1 v3000 2
你可以在结果集上过滤得到你所要的。
一、 二 、 三 问题 全部要求SELECT 出来。(注: 一定是做得到的, 而且不很长) 现想找出一个最经典的来。例:
select aa.item_no,min(vendor_no),aa.price,min(aa.aa) from aa
where aa.item_no+convert(char(10),price) in
(select item_no+convert(char(10),min(price)) from aa group by item_no)
group by item_no,price 一个较普通的。还有更好的