ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

³£¼ûSQLÃæÊÔÌâ1
2014-11-24 01:01:16 ¡¾´ó ÖРС¡¿ ä¯ÀÀ:4108´Î
Tags£º³£¼û SQL ÊÔÌâ

1.ÓÃÒ»ÌõSQLÓï¾ä ²éѯ³öÿÃſζ¼´óÓÚ80·ÖµÄѧÉúÐÕÃû


name kecheng fenshu
ÕÅÈý ÓïÎÄ 81
ÕÅÈý Êýѧ 75
ÀîËÄ ÓïÎÄ 76
ÀîËÄ Êýѧ 90
ÍõÎå ÓïÎÄ 81
ÍõÎå Êýѧ 100
ÍõÎå Ó¢Óï 90


A: select distinct name from table where name not in (select distinct name from table where fenshu<=80)


2.ѧÉú±í ÈçÏÂ:
×Ô¶¯±àºÅ ѧºÅ ÐÕÃû ¿Î³Ì±àºÅ ¿Î³ÌÃû³Æ ·ÖÊý
1 2005001 ÕÅÈý 0001 Êýѧ 69
2 2005002 ÀîËÄ 0001 Êýѧ 89
3 2005001 ÕÅÈý 0001 Êýѧ 69
ɾ³ý³ýÁË×Ô¶¯±àºÅ²»Í¬,ÆäËû¶¼ÏàͬµÄѧÉúÈßÓàÐÅÏ¢


A: delete tablename where ×Ô¶¯±àºÅ not in(select min(×Ô¶¯±àºÅ) from tablename group by ѧºÅ,ÐÕÃû,¿Î³Ì±àºÅ,¿Î³ÌÃû³Æ,·ÖÊý)


Ò»¸ö½ÐdepartmentµÄ±í£¬ÀïÃæÖ»ÓÐÒ»¸ö×Ö¶Îname,Ò»¹²ÓÐ4Ìõ¼Í¼£¬·Ö±ðÊÇa,b,c,d,¶ÔÓ¦ËĸöÇò¶Ô£¬ÏÖÔÚËĸöÇò¶Ô½øÐбÈÈü£¬ÓÃÒ»ÌõsqlÓï¾äÏÔʾËùÓпÉÄܵıÈÈü×éºÏ.
ÄãÏÈ°´Äã×Ô¼ºµÄÏë·¨×öһϣ¬¿´½á¹ûÓÐÎÒµÄÕâ¸ö¼òµ¥Âð£¿


´ð£ºselect a.name, b.name
from team a, team b
where a.name < b.name


ÇëÓÃSQLÓï¾äʵÏÖ£º´ÓTestDBÊý¾Ý±íÖвéѯ³öËùÓÐÔ·ݵķ¢Éú¶î¶¼±È101¿ÆÄ¿ÏàÓ¦Ô·ݵķ¢Éú¶î¸ßµÄ¿ÆÄ¿¡£Çë×¢Ò⣺TestDBÖÐÓкܶà¿ÆÄ¿£¬¶¼ÓÐ1£­12Ô·ݵķ¢Éú¶î¡£
AccID£º¿ÆÄ¿´úÂ룬Occmonth£º·¢Éú¶îÔ·ݣ¬DebitOccur£º·¢Éú¶î¡£
Êý¾Ý¿âÃû£ºJcyAudit£¬Êý¾Ý¼¯£ºSelect * from TestDB


´ð£ºselect a.*
from TestDB a
,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID=¡¯101¡ä group by Occmonth) b
where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur


************************************************************************************


ÃæÊÔÌ⣺Ôõô°ÑÕâÑùÒ»¸ö±í¶ù
year month amount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
²é³ÉÕâÑùÒ»¸ö½á¹û
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4


´ð°¸Ò»¡¢
select year,
(select amount from aaa m where month=1 and m.year=aaa.year) as m1,
(select amount from aaa m where month=2 and m.year=aaa.year) as m2,
(select amount from aaa m where month=3 and m.year=aaa.year) as m3,
(select amount from aaa m where month=4 and m.year=aaa.year) as m4
from aaa group by year


Õâ¸öÊÇORACLE ÖÐ×öµÄ£º
select * from (select name, year b1, lead(year) over
(partition by name order by year) b2, lead(m,2) over(partition by name order by year) b3,rank()over(
partition by name order by year) rk from t) where rk=1;


¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
ÉÏһƪ£º109µÀSQL ServerÃæÊÔÌâ ÏÂһƪ£º³£¼ûSQLÃæÊÔÌâ2

×îÐÂÎÄÕÂ

ÈÈÃÅÎÄÕÂ

Hot ÎÄÕÂ

Python

C ÓïÑÔ

C++»ù´¡

´óÊý¾Ý»ù´¡

linux±à³Ì»ù´¡

C/C++ÃæÊÔÌâÄ¿