id trueName userName score
1 张三 zs 80
2 张三 zs1 100
3 张三 zs2 90
想要获得张三的总分(userName 随便去一个值即可):
张三 zs 270 或 张三 zs1 270 或 张三 zs1 270
24 个解决方案
#1
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-21 16:48:24
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)
insert [huang]
select 1,'张三','zs',80 union all
select 2,'张三','zs1',100 union all
select 3,'张三','zs2',90
--------------开始查询--------------------------
select truename,(SELECT TOP 1 username FROM huang ORDER BY CHECKSUM(NEWID())) username,SUM(score)
from [huang]
GROUP BY truename
----------------结果----------------------------
/*
truename username
-------- -------- -----------
张三 zs1 270
*/
#2
数据越多随机性越明显
#3
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-21 16:48:24
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)
insert [huang]
select 1,'张三','zs',80 union all
select 2,'张三','zs1',100 union all
select 3,'张三','zs2',90
--------------开始查询--------------------------
select truename,(SELECT TOP 1 username FROM huang ORDER BY CHECKSUM(NEWID())) username,SUM(score)score
from [huang]
GROUP BY truename
----------------结果----------------------------
/*
truename username score
-------- -------- -----------
张三 zs2 270
*/
#4
create table #tb(id int,trueName varchar(10),userName varchar(10),score int)
insert into #tb
select 1,'张三','zs',80
union all select 2,'张三','zs1',100
union all select 3,'张三','zs2',90
select top 1 *
from
(
select trueName,userName,score=(select SUM(score) from #tb b where a.trueName=b.trueName) from #tb a
)t
order by NEWID()
drop table #tb
/*
trueName userName score
张三 zs1 270
*/
#5
你这个访问在于会用ORDER BY NEWID(),NEWID()随机产生的编号,可随机 排序
#6
create table #t([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)
insert #t
select 1,'张三','zs',80 union all
select 2,'张三','zs1',100 union all
select 3,'张三','zs2',90
go
select [trueName],MAX([userName]),SUM([score])
FROM #t
GROUP BY [trueName]
#7
select trueName, min(userName),sum(score)
from tb
group by trueName
from tb
group by trueName
#8
从随机度来说,checksum(newid())可以实现几乎是最高的随机性,当然数据量少的时候基本上没有什么随机性可言
#9
大神,你的方法好像所有的username都是一个值(万一有李四组也有三条记录 username还是zs)!我要的是username取小组中的任意一条记录,而不是所有的记录username都是一个值
#10
是这样吗:
if object_id('tb') is not null drop table tb
go
create table tb([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)
insert tb
select 1,'张三','zs',80 union all
select 2,'张三','zs1',100 union all
select 3,'张三','zs2',90
select trueName,max(username) as username,sum(score) as score
from tb
GROUP BY truename
/*
trueName username score
张三 zs2 270
*/
#11
select truename,max(username)username,SUM(score)
from [huang]
GROUP BY truename
order by checksum(newid())
from [huang]
GROUP BY truename
order by checksum(newid())
#12
你终于有回复了》。。。
#13
这个好像行啊,我不明白Max能用于字符串吗?
#14
max min都可以用于字符串,
#15
联机丛书上的原话:MAX can be used with numeric, character, uniqueidentifier, and datetime columns, but not with bit columns
#16
嗯,还是版主强!
#17
版主万岁!哈哈!
#18
我一开始用max做字符串比较是,提示错误,可能是我哪写错了吧!
还有就是order by checksum(newid()) 什么用途,不加好像也行啊!
还有就是order by checksum(newid()) 什么用途,不加好像也行啊!
#19
我这个order by可以不加,但是是我目前见过随机性最强的,如果你要“随机”取数,用这个是很不错的
#20
由于指定了max或者min,所以其实没什么随机性可言了
#21
改进了一下,这样就能满足你的“随机”了
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-21 17:46:44
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)
insert [huang]
select 1,'张三','zs',80 union all
select 2,'张三','zs1',100 union all
select 3,'张三','zs2',90 union all
select 4,'李四','zs',80 union all
select 5,'李四','zs1',100 union all
select 6,'李四','zs2',90
--------------开始查询--------------------------
select truename,(SELECT TOP 1 username FROM huang b WHERE huang.trueName=b.trueName ORDER BY CHECKSUM(NEWID())) truename,SUM(score)score
from [huang]
GROUP BY dbo.huang.trueName
----------------结果----------------------------
/*
truename truename score
-------- -------- -----------
李四 zs2 270
张三 zs2 270
*/
#22
谢谢版主,谢谢大家!
#23
#24
--FEE累加,REASON随便取一个即可
SELECT standards, sum(fee), get_reason1(standards) as reason1
FROM tb GROUP BY standards;
CREATE OR REPLACE FUNCTION get_reason1 (standardVar in tb.standards%TYPE)
RETURN VARCHAR2
IS
reason1 VARCHAR2(32767) := NULL;
BEGIN
FOR cur_rec IN (SELECT Reason FROM tb WHERE standards = standardVar and rownum=1) LOOP
reason1 := cur_rec.Reason;
END LOOP;
RETURN reason1;
END;
#1
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-21 16:48:24
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)
insert [huang]
select 1,'张三','zs',80 union all
select 2,'张三','zs1',100 union all
select 3,'张三','zs2',90
--------------开始查询--------------------------
select truename,(SELECT TOP 1 username FROM huang ORDER BY CHECKSUM(NEWID())) username,SUM(score)
from [huang]
GROUP BY truename
----------------结果----------------------------
/*
truename username
-------- -------- -----------
张三 zs1 270
*/
#2
数据越多随机性越明显
#3
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-21 16:48:24
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)
insert [huang]
select 1,'张三','zs',80 union all
select 2,'张三','zs1',100 union all
select 3,'张三','zs2',90
--------------开始查询--------------------------
select truename,(SELECT TOP 1 username FROM huang ORDER BY CHECKSUM(NEWID())) username,SUM(score)score
from [huang]
GROUP BY truename
----------------结果----------------------------
/*
truename username score
-------- -------- -----------
张三 zs2 270
*/
#4
create table #tb(id int,trueName varchar(10),userName varchar(10),score int)
insert into #tb
select 1,'张三','zs',80
union all select 2,'张三','zs1',100
union all select 3,'张三','zs2',90
select top 1 *
from
(
select trueName,userName,score=(select SUM(score) from #tb b where a.trueName=b.trueName) from #tb a
)t
order by NEWID()
drop table #tb
/*
trueName userName score
张三 zs1 270
*/
#5
你这个访问在于会用ORDER BY NEWID(),NEWID()随机产生的编号,可随机 排序
#6
create table #t([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)
insert #t
select 1,'张三','zs',80 union all
select 2,'张三','zs1',100 union all
select 3,'张三','zs2',90
go
select [trueName],MAX([userName]),SUM([score])
FROM #t
GROUP BY [trueName]
#7
select trueName, min(userName),sum(score)
from tb
group by trueName
from tb
group by trueName
#8
从随机度来说,checksum(newid())可以实现几乎是最高的随机性,当然数据量少的时候基本上没有什么随机性可言
#9
大神,你的方法好像所有的username都是一个值(万一有李四组也有三条记录 username还是zs)!我要的是username取小组中的任意一条记录,而不是所有的记录username都是一个值
#10
是这样吗:
if object_id('tb') is not null drop table tb
go
create table tb([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)
insert tb
select 1,'张三','zs',80 union all
select 2,'张三','zs1',100 union all
select 3,'张三','zs2',90
select trueName,max(username) as username,sum(score) as score
from tb
GROUP BY truename
/*
trueName username score
张三 zs2 270
*/
#11
select truename,max(username)username,SUM(score)
from [huang]
GROUP BY truename
order by checksum(newid())
from [huang]
GROUP BY truename
order by checksum(newid())
#12
你终于有回复了》。。。
#13
这个好像行啊,我不明白Max能用于字符串吗?
#14
max min都可以用于字符串,
#15
联机丛书上的原话:MAX can be used with numeric, character, uniqueidentifier, and datetime columns, but not with bit columns
#16
嗯,还是版主强!
#17
版主万岁!哈哈!
#18
我一开始用max做字符串比较是,提示错误,可能是我哪写错了吧!
还有就是order by checksum(newid()) 什么用途,不加好像也行啊!
还有就是order by checksum(newid()) 什么用途,不加好像也行啊!
#19
我这个order by可以不加,但是是我目前见过随机性最强的,如果你要“随机”取数,用这个是很不错的
#20
由于指定了max或者min,所以其实没什么随机性可言了
#21
改进了一下,这样就能满足你的“随机”了
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-21 17:46:44
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)
insert [huang]
select 1,'张三','zs',80 union all
select 2,'张三','zs1',100 union all
select 3,'张三','zs2',90 union all
select 4,'李四','zs',80 union all
select 5,'李四','zs1',100 union all
select 6,'李四','zs2',90
--------------开始查询--------------------------
select truename,(SELECT TOP 1 username FROM huang b WHERE huang.trueName=b.trueName ORDER BY CHECKSUM(NEWID())) truename,SUM(score)score
from [huang]
GROUP BY dbo.huang.trueName
----------------结果----------------------------
/*
truename truename score
-------- -------- -----------
李四 zs2 270
张三 zs2 270
*/
#22
谢谢版主,谢谢大家!
#23
#24
--FEE累加,REASON随便取一个即可
SELECT standards, sum(fee), get_reason1(standards) as reason1
FROM tb GROUP BY standards;
CREATE OR REPLACE FUNCTION get_reason1 (standardVar in tb.standards%TYPE)
RETURN VARCHAR2
IS
reason1 VARCHAR2(32767) := NULL;
BEGIN
FOR cur_rec IN (SELECT Reason FROM tb WHERE standards = standardVar and rownum=1) LOOP
reason1 := cur_rec.Reason;
END LOOP;
RETURN reason1;
END;