----------------------------------------------------------------------
第一题(只要重复的都要 输出)
----------------------------------------------------------------------
create table c (id int );
insert into c values (1),(2),(3),(4),(3),(5),(6),(1);
结果:
    
select id from c group by id having count(id)>1 ;


----------------------------------------------------------------------
第二题(报表查询横向输出)
查询后的结果:
Create table d(id int,name varchar(50));
insert into d values(1,'gly');
insert into d values(2,'ptgly');
insert into d values(3,'ybgly');
insert into d values(4,'ptgly');
insert into d values(5,'ybgly');
+---+-----+------+
|gly|ptgly|ybgly |     
|---+-----+------+-
|1  |2    |2     |
+---+-----+------+
-----------------------------------------------------------------------
select 
    
sum(case when name='gly' then 1 else 0 end ) as gly  ,
    
sum(case when name='ptgly' then 1 else 0 end ) as ptgly  ,
    
sum(case when name='ybgly' then 1 else 0 end ) as ybgly  
from d ;



第三题
可以根据上面 2,4 可以得出 。
下面我们详细说下 4题。


-----------------------------------------------------------------------
第四题(复杂组合查询!)
create table table_a (No int, No2 int,num double,itime date);
insert into table_a values
     (1234,567890,33.5,'2004-12-21'),
     (1234,598701,44.8,'2004-11-21'),
     (1234,598701,45.2,'2004-10-01'),
     (1234,567890,66.5,'2004-9-21'),
     (3456,789065,22.5,'2004-10-01'),
     (3456,789065,77.5,'2004-10-27'),
     (3456,678901,48.5,'2004-12-21');
按月统计销售表中货物的销售量数
查询结果如下:
  No, No2 ,   九月,  十月,十一月,十二月
1234,567890, 66.5 ,  0 ,  0  ,  33.5
1234,598701,  0   , 45.2, 44.8, 0
3456,789065, 0  ,  100,  0  ,  0
3456,678901, 0 ,    0,    0  ,  48.5
-----------------------------------------------------------------------
//当然也可以 使用mysql 时间函数 在软件编辑时 你可以输入 String[] 并根据数据动态拼写 sql( case部分!! )
//这个 例子很好 哦!报表可以一句sql 得出!

select NO,NO2,
    
sum(case  when itime like '2004-%9%' then num else 0 endas 9M,
    
sum(case  when itime like '2004-10%' then num else 0 endas 10M,
    
sum(case  when itime like '2004-11%' then num else 0 endas 11M,
    
sum(case  when itime like '2004-12%' then num else 0 endas 12M
from table_a group by no,no2 order by no,no2 ;


--------------------------------------------------------------
第五题
代码,名称
1     a
2     b
11    c
(代码11表示为1的下级)
我要通过一条句子,得出如下结果:

代码,名称,有无下级
1     a     有
2     b     无
11    c     无


Create table TabTest(t_Code varchar(10),t_Name varchar(10));
insert into TabTest values('1','a');
insert into TabTest values('2','b');
insert into TabTest values('11','c');
--------------------------------------------------------------
select tt1.t_Code,tt1.t_name,( 
     
case 
             
when exists (select 1 from tabtest tt2 
                                       
where tt2.t_code like CONCAT(tt1.t_code,'%'and 
                                        tt2.t_code 
<> tt1.t_code )  then 'you' 
             
else 'wu'
      
end ) as you_wu
from tabtest  tt1 ;
posted on 2008-08-15 11:46  墨尘  阅读(455)  评论(0编辑  收藏  举报