读书人

遇到一个SQL有关问题求高手解决

发布时间: 2012-12-16 12:02:32 作者: rapoo

遇到一个SQL问题,求高手解决
如何把数据表,查成这种模式,前面的姓名,卡号,电话只能出现一次,不能重复

姓名 卡号 电话 消费项目 次数

张三 234 1333323 蜡水洗车 1
臭氧消毒 2

[解决办法]

select 姓名, 卡号, 电话, 消费项目 ,count('次数') 次数
from tb
group by 姓名, 卡号, 电话, 消费项目

[解决办法]
select case when 消费项目 = (select min(消费项目) from tb where 姓名 = t.姓名) then 姓名 else '' end 姓名,
case when 消费项目 = (select min(消费项目) from tb where 姓名 = t.姓名) then 卡号 else '' end 卡号,
case when 消费项目 = (select min(消费项目) from tb where 姓名 = t.姓名) then 电话 else '' end 电话,
消费项目, 次数
from tb t

[解决办法]
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
姓名 char(5),
卡号 int,
电话 char(9),
消费项目 char(9),
次数 int
)
go
--插入测试数据
insert into tb select '张三',234,'1333323','蜡水洗车',1
union all select '张三',234,'1333323','臭氧消毒',2
union all select '张三',234,'1333323','消毒',2
union all select '李四',237,'1553323','蜡水洗车',3
union all select '李四',237,'1553323','臭氧消毒',2
go
--代码实现

;with t as(select idd=row_number()over(partition by 姓名 order by 次数),* from tb)
select 姓名=case when idd=1 then rtrim(姓名) else '' end
,卡号=case when idd=1 then rtrim(卡号) else '' end
,电话=case when idd=1 then rtrim(电话) else '' end
,消费项目,次数
from t

/*测试结果

姓名 卡号 电话 消费项目 次数
--------------------------------------
李四2371553323臭氧消毒 2
蜡水洗车 3
张三2341333323蜡水洗车 1
臭氧消毒 2
消毒 2

(5 行受影响)
*/

[解决办法]
我靠
20分的帖子都抢的这么猛!~~

[解决办法]
好生猛哦
[解决办法]
该回复于2010-09-29 11:02:25被版主删除
------解决方案--------------------


学习了......
[解决办法]
学习了......
[解决办法]
学习了......
[解决办法]
确实学习 。。。
[解决办法]

引用:
SQL code
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
姓名 char(5),
卡号 int,
电话 char(9),
消费项目 char(9),
次数 int
)
go
--插入测试数据
insert into tb s……

学习!!
[解决办法]
引用:
我靠
20分的帖子都抢的这么猛!~~


 亮点

3楼的方法好
[解决办法]
引用:
SQL code
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
姓名 char(5),
卡号 int,
电话 char(9),
消费项目 char(9),
次数 int
)
go
--插入测试数据
insert into tb s……

学习了
[解决办法]
select 姓名,卡号,电话,消费项目, 次数 from (
select 姓名 as 姓名1,
case when 次数 = (select min(次数) from tb where 姓名 = t.姓名) then 姓名 else '' end 姓名,
case when 次数 = (select min(次数) from tb where 姓名 = t.姓名) then 卡号 else '' end 卡号,
case when 次数 = (select min(次数) from tb where 姓名 = t.姓名) then 电话 else '' end 电话,
消费项目, 次数 from tb t
) A order by 姓名1 desc ,次数
[解决办法]
学习 。。。
[解决办法]
这。。。。。。。。。。。
[解决办法]
--2000,2005通过 
-- 创建数据表
create table tb
(
姓名 char(5),
卡号 int,
电话 char(9),
消费项目 char(9),
次数 int
)
go
--插入测试数据
insert into tb select '张三',234,'1333323','蜡水洗车',1
union all select '张三',234,'1333323','臭氧消毒',2
union all select '张三',234,'1333323','消毒',2
union all select '李四',237,'1553323','蜡水洗车',3
union all select '李四',237,'1553323','臭氧消毒',2
go
--代码实现
select case when level=1 then 姓名 else '' end 姓名,
case when level=1 then ltrim(卡号) else '' end 卡号,


case when level=1 then ltrim(电话) else '' end 电话,
消费项目,次数
from
(select *,姓名 姓名1,level=(select count(*) from tb a where a.姓名=tb.姓名 and a.消费项目<=tb.消费项目) from tb) t
order by 姓名1 desc,level asc
GO

姓名 卡号 电话 消费项目 次数
----- ------------ --------- --------- -----------
张三 234 1333323 臭氧消毒 2
蜡水洗车 1
消毒 2
李四 237 1553323 臭氧消毒 2
蜡水洗车 3

(5 行受影响)

读书人网 >SQL Server

热点推荐