SQL 統計問題(100分)

  • 主题发起人 主题发起人 yicheng
  • 开始时间 开始时间
Y

yicheng

Unregistered / Unconfirmed
GUEST, unregistred user!
如表:table<br>id &nbsp; &nbsp; &nbsp; &nbsp; item &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;in &nbsp; &nbsp; &nbsp; out &nbsp; &nbsp; &nbsp; inv<br>1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AAA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 50 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;50<br>2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BBB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 80 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;80 <br>3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AAA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 20 &nbsp; &nbsp; &nbsp; &nbsp;30<br>4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BBB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 40 &nbsp; &nbsp; &nbsp; &nbsp;40<br>5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BBB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 100 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 140<br>6 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BBB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 20 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;160<br><br>以上为一个进销存的表,我现在要单一的筛选AAA或是BBB的话:<br>select inv form table where id=(select max(id) form table where item='AAA')<br>妈即可得到单一item的库存。<br><br>现在,我要通过一条SQL语句得到这样一个表:<br>id &nbsp; &nbsp; &nbsp; &nbsp; item &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inv<br>3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AAA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;30<br>6 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BBB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;160 <br><br>如何做?
 
select inv form table where id in (select max(id) form table where group by item)
 
select id,item,inv form table where id in (select max(id) form table where group by item)
 
Select &nbsp;id,item,inv &nbsp;From &nbsp;table &nbsp;where &nbsp;id &nbsp;in &nbsp;(select max( id) &nbsp;From &nbsp;table &nbsp;group by &nbsp;item)
 
后退
顶部