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
Ïà¹ØÎĵµ£º
ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃæ£¬ºóÃæ ......
¶¯Ì¬Óï¾ä
1 :ÆÕͨSQLÓï¾ä¿ÉÒÔÓÃexecÖ´ÐÐ
Select * from tableName
exec('select * from tableName')
exec sp_executesql N'select * from tableName' -- Çë×¢Òâ×Ö·û´®Ç°Ò»¶¨Òª¼ÓN
2:×Ö¶ÎÃû£¬±íÃû£¬Êý¾Ý¿âÃûÖ®Àà×÷Ϊ±äÁ¿Ê±£¬±ØÐëÓö¯Ì¬SQL
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fnam ......
--×Ô¼ººÜÓÞ´ÀµÄ×ö·¨£¬ÒÔΪֱ½Óɾ³ýÊý¾Ý¿âÓû§Ãû£¬ÔÙ´Óд´½¨Óû§µÇ¼ÃûºÍÊý¾Ý¿âÓû§Ãû¡£
--×Ô¼ºÒÔΪ¿ÉÒÔÖ±½Ó´´½¨ÓëÖ®ÔÏÈÒ»ÑùµÄÊý¾Ý¿âµÇ¼Ãû¾Í¿ÉÒÔ
--µ«ÕâЩ²Ù×÷ÊDz»Æð×÷ÓõÄ
--ÔÒò
--µ±Ó³Éä¹ÂÁ¢Óû§Ê±£¬Ö÷´ÓÊý¾Ý¿âÖеÄSID½«·ÖÅ䏸¹ÂÁ¢Óû§£¬ËùÒÔÿ´ÎÒ»¸öÊý¾Ý¿â±¸¸½¼Ó»òÕß»¹Ôʱ£¬--SIDÔÚSQL Server µÇ¼ÃûºÍÊý¾Ý¿âÓû ......
select *
from
(select [id]=row_number() over (order by getdate()),
date=convert(varchar(8),dateadd(dd,number,'2010-01-01'),112)
from
(select number from master..spt ......