Sql Server ×Ö·û´®¾ÛºÏº¯Êý
Sql Server ÓÐÈçϼ¸Ö־ۺϺ¯ÊýSUM¡¢AVG¡¢COUNT¡¢COUNT(*)¡¢MAX ºÍ MIN£¬µ«ÊÇÕâЩº¯Êý¶¼Ö»ÄܾۺÏÊýÖµÀàÐÍ£¬ÎÞ·¨¾ÛºÏ×Ö·û´®¡£Èçϱí:AggregationTable
Id Name
1 ÕÔ
2 Ç®
1 Ëï
1 Àî
2 ÖÜ
Èç¹ûÏëµÃµ½ÏÂͼµÄ¾ÛºÏ½á¹û
Id Name
1 ÕÔËïÀî
2 Ç®ÖÜ
ÀûÓÃSUM¡¢AVG¡¢COUNT¡¢COUNT(*)¡¢MAX ºÍ MINÊÇÎÞ·¨×öµ½µÄ¡£ÒòΪÕâЩ¶¼ÊǶÔÊýÖµµÄ¾ÛºÏ¡£²»¹ýÎÒÃÇ¿ÉÒÔͨ¹ý×Ô¶¨Ò庯ÊýµÄ·½Ê½À´½â¾öÕâ¸öÎÊÌâ¡£
1.Ê×ÏȽ¨Á¢²âÊÔ±í£¬²¢²åÈë²âÊÔÊý¾Ý£º
view plaincopy to clipboardprint?
create table AggregationTable(Id int, [Name] varchar(10))
go
insert into AggregationTable
select 1,'ÕÔ' union all
select 2,'Ç®' union all
select 1,'Ëï' union all
select 1,'Àî' union all
select 2,'ÖÜ'
go
create table AggregationTable(Id int, [Name] varchar(10))
go
insert into AggregationTable
select 1,'ÕÔ' union all
select 2,'Ç®' union all
select 1,'Ëï' union all
select 1,'Àî' union all
select 2,'ÖÜ'
go
2.´´½¨×Ô¶¨Òå×Ö·û´®¾ÛºÏº¯Êý
view plaincopy to clipboardprint?
Create FUNCTION AggregateString
(
@Id int
)
RETURNS varchar(1024)
AS
BEGIN
declare @Str varchar(1024)
set @Str = ''
select @Str = @Str + [Name] from AggregationTable
where [Id] = @Id
return @Str
END
GO
Create FUNCTION AggregateString
(
@Id int
)
RETURNS varchar(1024)
AS
BEGIN
declare @Str varchar(1024)
set @Str = ''
select @Str = @Str + [Name] from AggregationTable
where [Id] = @Id
return @Str
END
GO
3.Ö´ÐÐÏÂÃæµÄÓï¾ä£¬²¢²é¿´½á¹û
view plaincopy to clipboardprint?
select dbo.Aggregate
Ïà¹ØÎĵµ£º
--------------------------------------------------------------------
-- ×÷ÕߣºÕŰ®¹ú
-- ÈÕÆÚ£º2010-01-23 15:00:17
-- ÃèÊö: һЩ±È½ÏÓÐÒâ˼µÄSQLÓï¾ä
-- Version:
-- Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86)
-- Feb 9 2007 22:47:0 ......
SQL Server 2005 ¿ª·¢°æ²»ÔÊÐíÔ¶³ÌÁ¬½Ó¡£
¡¡¡¡ÒªÔÊÐíÔ¶³ÌÁ¬½ÓSQL Server 2005 ,ÐèÒªÍê³ÉÒÔϲ½Ö裺
•ÔÚSQLServer ʵÀýÉÏÆôÓÃÔ¶³ÌÁ¬½Ó¡£
•´ò¿ª·þÎñÆ÷ SQLBrowser ·þÎñ¡£
•ÅäÖ÷À»ðǽÒÔÔÊÐíSQLBrowser ·þÎñºÍSQLServer·þÎñÍøÂçͨÐÅ¡£
¡¡¡¡ÔÚSQLServer ʵÀýÉÏÆôÓÃÔ¶³ÌÁ¬½Ó
1.Ö¸Ïò“¿ªÊ¼->³ÌÐ ......
£¨1£©SQL·½·¨£¨1£©
select convert(varchar(5),dateadd(hour,number,cast('00:00' as datetime)),108)+'~'+
convert(varchar(5),dateadd(hour,number+1,cast('00:00' as datetime)),108) as [date]
from master..spt_values
where type = 'P' and
number <= 23
£¨2£©SQL·½·¨£¨2£©
µÃ³öÒ»ÌìµÄʱ¼ä¶Î¼Ç¼¡££¨Èç&nb ......
if exists(select * from sysobjects where name='atzk')--ÅжÏÊÇ·ñ´æÔڴ˱í
drop table atzk
go
create table atzk
(
nid int identity(1,1) primary key,--nid×Ô¶¯±àºÅ£¬²¢ÉèΪÖ÷¼ü¡£
mytitle varchar(50) not null,--֪ͨµÄ±êÌâ¡£
mycontents varchar(200)--·¢²¼Í¨ÖªµÄÄÚÈÝ¡£
) ......