????? ??? ?????? ?? ????? SELECT Transact-SQL ???? ????

?????? ????????? ?????? ?????????
???? ???????: 186133 - ??? ???????? ???? ????? ????? ??? ???????.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ????? ????? ?????? ???? ???? ??? ????? ????? SELECT ???????? ????? ???? ????? ?? ???? ???? ????? ??? ? ??? ???? ?? ???? ???????. ?? ????? ?? ????? ????? ??????? ???????. ?????? ????? ????? ????? ???????? ???????; ????? ???? ????? ??? SQL Server ????? ? ???? ????????. ????? ??? ??????? ??? ??? ???????. ????? ?? ???? ????? ???? ?? ??????? "???? ?? ???." ???? ??? ?????? ???? ??? ????? ?? ???? ?? ?????? ????? ?? ???????? ???? ??????? "???? ??" ??? ?????? ???????? ??? ????.

?????? ????? ??????? ??????? ??? ????? ?????? pubs (????????). ???? ???????? ????? ?????? ????? Northwind ?? ????? ????? ?????? pubs (????????) ??? ????? ?? SQL Server 2005. ???? ????? ????? ???????? ??? ?? "???? ??????? ?? Microsoft". ????? ?? ?????????? ?? ?????? ???? Microsoft ?????? ??? ?????:
http://go.microsoft.com/fwlink/?linkid=30196
??? ????? SQL2000SampleDb.msi ??????? ????? ???????? ????? ??????? ?????? ?????? ??????? ??? SQL2000SampleDb.msi. ???? ???????? ???? ??????? SQL2000SampleDb.msi ??????? ?????? ????? ???????? "?" ??? ?????? ?? ?????? ??????:
????? Server 2000 C:\SQL ????? ??????
???? ????????? ???????? ?? ????? ???????? ?????? ??????? ?????? ???????.

??? ??? ?????? SQL Server 2005

?? ???????? ??????? ??????? ????? ???????? ?????? ???? ????? ?? SQL Server 2005. ?????? ??? ???? ?? ????????? ??? ??????? ????? ?? ?????? ???? ???? ????? Microsoft (MSDN) ?????? ??? ?????:
http://msdn2.microsoft.com/en-us/library/ms189798.aspx

???? 1

?? ??? ??????:
  • ????? ?????? 1 ???????.
  • ?? ?????? 2 ???????.
  • ??????? ??? "????? ?????? ?????? ????? ???? ???? ??."
  • ????? ???? ??????? ????? ?? ???? ?????? ????? ????? + ?????? ????? + ?????? ??????? ??????.
  • ???? ??? ???? ???? ??????? ?????? count(*).
???????:
   select rank=count(*), a1.au_lname, a1.au_fname
   from authors a1, authors a2
   where a1.au_lname + a1.au_fname >= a2.au_lname + a2.au_fname
   group by a1.au_lname, a1.au_fname
   order by rank
				
??????? ????????? ???????? ??????? ?? SQL Server 2005.
   select rank() OVER (ORDER BY a.au_lname, a.au_fname) as rank, a.au_lname, a.au_fname
   from authors a
   order by rank 
?????:
   Rank        Au_Lname              Au_Fname
   ----        --------------        -----------
     1         Bennet                Abraham
     2         Blotchet-Halls        Reginald
     3         Carson                Cheryl
     4         DeFrance              Michel
     5         del Castillo          Innes
     6         Dull                  Ann
     7         Greene                Morningstar
     8         Green                 Marjorie
     9         Gringlesby            Burt
    10         Hunter                Sheryl
    11         Karsen                Livia
    12         Locksley              Charlene
    13         MacFeather            Stearns
    14         McBadden              Heather
    15         O'Leary               Michael
    16         Panteley              Sylvia
    17         Ringer                Albert
    18         Ringer                Anne
    19         Smith                 Meander
    20         Straight              Dean
    21         Stringer              Dirk
    22         White                 Johnson
    23         Yokomoto              Akiko

   (23 row(s) affected)
				

???? 2

?? ??? ??????:
  • ??? ????? ???? ??? ??? ?????.
  • ?????? 1 ?? ??? ????? ???? ?? ????? ?????? ????: ??? stor_id qty=sum(qty) ?? ????? ???????? ??? stor_id.
  • ?????? 2 ?? ??? ????? ???? ?? ????? ?????? ????: ??? stor_id qty=sum(qty) ?? ????? ???????? ??? stor_id.
  • ??????? ??? "??? ??? ???? ??."
  • ????? ????????? ????? (?????) ?????? ????? * ?????? ????? ?? qty.
???????:
   select rank=count(*), s1.stor_id, qty=sum(s1.qty)
   from (select stor_id, qty=sum(qty) from sales group by stor_id) s1,
        (select stor_id, qty=sum(qty) from sales group by stor_id) s2
   where s1.qty >= s2.qty
   group by s1.stor_id
   order by rank
				
?????:
   Rank     Stor_Id    Qty
   ----     -------    ---
   1         6380        8
   2         7896      120
   3         8042      240
   4         7067      360
   5         7066      625
   6         7131      780

   (6 row(s) affected)
				
?????? ????? ?? ?????? ?????? ??? ?????. ??? ???? ??? ????? ????? ???????? ??? ???? ??? ????? ??????. ??? ??? ??? ???????. ????? ??????? ??? ??????? ????? ????? ????? ??? ???? ??? ?????? ??? ???? ?? ??????.

??????? ???????? ???????? ??????? ?? SQL Server 2005.
select row_number() over (order by qty desc) as rank,s1.stor_id,s1.qty
from (select stor_id, qty=sum(qty) from sales group by stor_id) as s1
?????:
rank     stor_id  qty
-------  -------  ------
1        7131     130
2        7066     125
3        7067     90
4        8042     80
5        7896     60
6        6380     8

(6 row(s) affected)
?????? ?? SQL Server 2005 ????? ???? ??????? ??????? ?? ??????? ? ??????? ??? ??????? ????? ?????.

???? 3

?? ??? ??????:
  • ????? ?? ???????? ?????? ??????? ?????? ???.
  • ?????? 1 ?? ?????? ???????? ?????? ??????:
            select t.pub_id, sales=sum(s.qty*t.price)
            from sales s, titles t
            where s.title_id=t.title_id
              and t.price is not null
            group by t.pub_id
    					
  • ?????? 2 ?? ?????? ???????? ?????? ??????:
            select t.pub_id, sales=sum(s.qty*t.price)
            from sales s, titles t
            where s.title_id=t.title_id
              and t.price is not null
            group by t.pub_id
    					
  • ??????? ??? "earns ??????? ???? ??".
???????:
   select rank=count(*), s1.pub_id, sales=sum(s1.sales)
   from    (select t.pub_id, sales=sum(s.qty*t.price)
           from sales s, titles t
           where s.title_id=t.title_id
             and t.price is not null
           group by t.pub_id) s1,
           (select t.pub_id, sales=sum(s.qty*t.price)
           from sales s, titles t
           where s.title_id=t.title_id
             and t.price is not null
           group by t.pub_id) s2
   where s1.sales>= s2.sales
   group by s1.pub_id
   order by rank
				
?????:
   Rank     Pub_Id   Sales
   ----     ------   --------
   1         0736    1,961.85
   2         0877    4,256.20
   3         1389    7,760.85

   (3 row(s) affected)
				
?????? ????? ?? ???? "????????" ??? ?????. ??? ???? ??? ????? ???????? ???????? ??? ??????? ??????.

??????? ????????? ???????? ??????? ?? SQL Server 2005.
select rank() over (order by sales desc) as rank,s1.pub_id,s1.sales 
from (select t.pub_id, sales=sum(s.qty*t.price)
     from sales s inner join titles t
     on s.title_id=t.title_id
     where  t.price is not null
     group by t.pub_id) as s1
???????:
rank     pub_id  sales
-------  ------  ---------
1        1389    2586.95
2        0877    2128.10
3        0736    1961.85

(3 row(s) affected)
				
?????? ????? ???? ?????? ????? ??????? ?? ??? ??? ??????? ???????? ?????.

????

  • ????? ????? ??? ??? ??? ???? ????? ?? ??? ???? ?? ??????. ???? ???? ??? ??????? ???? ????. ?? ????? ????? ???? ?? ??????? ???? ????? ?????? ?????? ????.
  • ??? ?? ???? ???? ??? ?? ??? ?????. ????? ???? ??? ????????? ???? ??? ?????? ??? ?????. ??? ?? ??? ??? ?????? ???? ????? ????? ???? ??? ?? ???? ????? ?????? ???? ??? ????? ??????? ?? ???? ??????; ??????? ???? ????? ????? ?? ???.

    ?????? ??? ??? ?????? SQL Server 2005 ? ????? ??????? ?????? row_number() ?????? ??? ?????? ??? ??? ????? ?? ?????? ???????.
Example:
   select rank=count(*), s1.title_id, qty=sum(s1.qty)
   from (select title_id, qty=sum(qty) from sales group by title_id) s1,
        (select title_id, qty=sum(qty) from sales group by title_id) s2
   where s1.qty >= s2.qty
   group by s1.title_id
   order by rank
Result:
   Rank    Title_Id    Qty
   ----    --------    ----
   1       MC2222        10
   4       BU1032        60
   4       BU7832        60
   4       PS3333        60
   7       PS1372       140
   7       TC4203       140
   7       TC7777       140
   10      BU1111       250
   10      PS2106       250
   10      PS7777       250
   11      PC1035       330
   12      BU2075       420
   14      MC3021       560
   14      TC3218       560
   15      PC8888       750
   16      PS2091      1728

   (16 row(s) affected)
				

?????

  • ????? ??????? ??? ??????????? ?? ??? ????? ?????? ???????.
  • ???? ????? ???????? ????? ????? ??? ??? ??????.
?????? 1:
   CREATE VIEW v_pub_rank
   AS
   select rank=count(*), s1.title_id, qty=sum(s1.qty)
   from (select title_id, qty=sum(qty) from sales group by title_id) s1,
        (select title_id, qty=sum(qty) from sales group by title_id) s2
   where s1.qty >= s2.qty
   group by s1.title_id
				
?????????:
   select  publisher=convert(varchar(20),replicate (' ', power(2,rank)) +
           pub_id +
           replicate(' ', 15-power(2,rank))+': '),
           earnings=qty
   from v_pub_rank
				
?????:
   Publisher       Earnings
   -------------   --------
     0736          : 1,961.85
       0877        : 4,256.20
           1389    : 7,760.85
				
??????? ????????? ???????? ??????? ?? SQL Server 2005.
CREATE VIEW v_pub_rank
AS
select rank() over (order by sales) as rank,s1.pub_id,s1.sales 
	from (select t.pub_id, sales=sum(s.qty*t.price)
	from sales s, titles t
	where s.title_id=t.title_id
	and t.price is not null
	group by t.pub_id) as s1
GO

select  publisher=convert(varchar(20),replicate (' ', power(2,rank)) +
	pub_id + replicate(' ', 15-power(2,rank))+': '),
	earnings=sales
from v_pub_rank order by rank
GO
?????:
publisher            earnings
-------------------- ---------------------
  0736             : 1961.85
    0877           : 2128.10
        1389       : 2586.95

(3 row(s) affected)

?????? 2:
   CREATE VIEW v_title_rank
   AS
   select rank=count(*), s1.title_id, qty=sum(s1.qty)
   from (select title_id, qty=sum(qty) from sales group by title_id) s1,
        (select title_id, qty=sum(qty) from sales group by title_id) s2
   where s1.qty >= s2.qty
   group by s1.title_id
				
?????????:
   select  Book=convert(varchar(45),replicate (' ', 2*rank) +
           title_id +
           replicate(' ', 35-2*rank)+': '),
           qty
   from v_title_rank
   order by rank
				
?????:
   Book                                          Qty
   -------------------------------------------   ----
     MC2222                                 :      10
           BU1032                           :      60
           BU7832                           :      60
           PS3333                           :      60
                 PS1372                     :     140
                 TC4203                     :     140
                 TC7777                     :     140
                       BU1111               :     250
                       PS2106               :     250
                       PS7777               :     250
                         PC1035             :     330
                           BU2075           :     420
                               MC3021       :     560
                               TC3218       :     560
                                 PC8888     :     750
                                   PS2091   :    1728

   (16 row(s) affected)
				
??????? ????????? ???????? ??????? ?? SQL Server 2005.
CREATE VIEW v_title_rank
AS
select rank() over (order by qty) as rank, s1.title_id,s1.qty
from (select title_id, qty=sum(qty) from sales group by title_id) as s1
GO

select Book=convert(varchar(45),replicate (' ', 2*rank) +
title_id + replicate(' ', 35-2*rank)+': '), qty
from v_title_rank
order by rank
GO
?????:
Book                                          qty
--------------------------------------------- -----------
  MC2222                                 :    10
    BU1032                               :    15
    BU7832                               :    15
    PS3333                               :    15
          TC4203                         :    20
          TC7777                         :    20
          PS1372                         :    20
                BU1111                   :    25
                PS7777                   :    25
                PS2106                   :    25
                      PC1035             :    30
                        BU2075           :    35
                          MC3021         :    40
                          TC3218         :    40
                              PC8888     :    50
                                PS2091   :    108

(16 row(s) affected)

 

???????

???? ???????: 186133 - ????? ??? ??????: 15/????/1427 - ??????: 5.4
????? ???
  • Microsoft SQL Server 2000 Standard Edition
  • Microsoft SQL Server 4.21a Standard Edition
  • Microsoft SQL Server 6.0 Standard Edition
  • Microsoft SQL Server 6.5 Standard Edition
  • Microsoft SQL Server 7.0 Standard Edition
  • Microsoft SQL Server 2005 Standard Edition
  • Microsoft SQL Server 2005 Developer Edition
  • Microsoft SQL Server 2005 Enterprise Edition
  • Microsoft SQL Server 2005 Express Edition
  • Microsoft SQL Server 2005 Workgroup Edition
????? ??????: 
kbmt kbhowtomaster KB186133 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????186133

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com