姓名 工时
张三 1.0
张三,李四 1.0
李四,王五 1.0
王五 1.0
赵六,陈七,马八,赵九 1.0
想通过SQL语句求出
姓名 工时
张三 2.0
李四 2.0
王五 2.0
赵六 1.0
陈七 1.0
马八 1.0
赵九 1.0
有办法查询嘛?
select count(工时) from 表名 group by 姓名,
试试这个。
张三,李四 逗号隔开的,那么简单也不用上来问了!
@彬彬@科比: 分割,求相同名字出现的次数。
这个有办法,先用游标循环每一条记录(分割第一个字段),插入一个临时表 A
A表数据如下:
张三 1.0
张三 1.0
李四 1.0
李四 1.0
……
然后对临时表A操作
SELECT 姓名,SUM(工时) FROM A GROUP BY 姓名
思路很简单,写个存储过程就ok了
你数据库设计真的很有问题!
贴答案
@彬彬@科比:
1 IF EXISTS(SELECT * FROM tempdb.dbo.sysobjects WHERE id=object_id(N'tempdb..#tb_employee') AND type='U') 2 DROP TABLE #tb_employee 3 ELSE 4 BEGIN 5 CREATE TABLE #tb_employee( 6 EmployID INT IDENTITY(1,1) NOT NULL, 7 EmployName NVARCHAR(500) NOT NULL, 8 EmployWork DECIMAL(5,1) NOT NULL) 9 INSERT INTO #tb_employee(EmployName,EmployWork)VALUES(N'张三','1.0') 10 INSERT INTO #tb_employee(EmployName,EmployWork)VALUES(N'张三,李四','1.0') 11 INSERT INTO #tb_employee(EmployName,EmployWork)VALUES(N'李四,王五','1.0') 12 INSERT INTO #tb_employee(EmployName,EmployWork)VALUES(N'王五','1.0') 13 INSERT INTO #tb_employee(EmployName,EmployWork)VALUES(N'赵六,陈七,马八,赵九','1.0') 14 DECLARE my_cursor CURSOR 15 FOR SELECT EmployName,EmployWork FROM #tb_employee 16 BEGIN 17 DECLARE @EmployName NVARCHAR(500) 18 DECLARE @EmployWork DECIMAL(5,1) 19 CREATE TABLE #tb_employee1( 20 EmployID INT IDENTITY(1,1) NOT NULL, 21 EmployName NVARCHAR(500) NOT NULL, 22 EmployWork DECIMAL(5,1) NOT NULL) 23 OPEN my_cursor 24 FETCH NEXT FROM my_cursor INTO @EmployName,@EmployWork 25 IF(@EmployName IS NOT NULL) 26 BEGIN 27 IF(CHARINDEX(',',@EmployName)=0) 28 BEGIN 29 INSERT INTO #tb_employee1(EmployName,EmployWork)VALUES(@EmployName,@EmployWork) 30 END 31 IF(CHARINDEX(',',@EmployName)<>0) 32 BEGIN 33 DECLARE @temp NVARCHAR(50) 34 DECLARE @i INT 35 SET @temp=@EmployName+',' 36 SET @i=CHARINDEX(',',@temp) 37 WHILE @i>=1 38 BEGIN 39 INSERT INTO #tb_employee1(EmployName,EmployWork)VALUES(LEFT(@temp,@i-1),@EmployWork) 40 SET @temp=SUBSTRING(@temp,@i+1,LEN(@temp)) 41 SET @i=CHARINDEX(',',@temp) 42 END 43 END 44 WHILE(@@FETCH_STATUS<>-1) 45 BEGIN 46 SET @EmployName=NULL 47 SET @EmployWork=NULL 48 FETCH NEXT FROM my_cursor INTO @EmployName,@EmployWork 49 IF(@EmployName IS NOT NULL) 50 BEGIN 51 IF(CHARINDEX(',',@EmployName)=0) 52 BEGIN 53 INSERT INTO #tb_employee1(EmployName,EmployWork)VALUES(@EmployName,@EmployWork) 54 END 55 IF(CHARINDEX(',',@EmployName)<>0) 56 BEGIN 57 DECLARE @temp1 NVARCHAR(50) 58 DECLARE @j INT 59 SET @temp1=@EmployName+',' 60 SET @j=CHARINDEX(',',@temp1) 61 WHILE @j>=1 62 BEGIN 63 INSERT INTO #tb_employee1(EmployName,EmployWork)VALUES(LEFT(@temp1,@j-1),@EmployWork) 64 SET @temp1=SUBSTRING(@temp1,@j+1,LEN(@temp1)) 65 SET @j=CHARINDEX(',',@temp1) 66 END 67 END 68 69 END 70 END 71 END 72 END 73 SELECT EmployName AS 姓名,SUM(EmployWork) AS 工时 FROM #tb_employee1 GROUP BY EmployName 74 DROP TABLE #tb_employee 75 DROP TABLE #tb_employee1 76 CLOSE my_cursor 77 DEALLOCATE my_cursor 78 END
我没有新建表,都是建立的临时表,你可以直接在查询分析器中查询,结果是你想要的!
我帮你写了一个字符串切割的例子,当然你也可以SQL中写个函数
1 DECLARE @A VARCHAR(5000) 2 DECLARE @i INT 3 SET @A='A,B,C,D,D,S,X,C,C,C,D,AAAA,DDDDDD,DEF,ERT,' 4 SET @i=CHARINDEX(',',@A) 5 WHILE @i>=1 6 BEGIN 7 PRINT LEFT(@A,@i-1) 8 SET @A=SUBSTRING(@A,@i+1,LEN(@A)-1) 9 SET @i=CHARINDEX(',',@A) 10 END
sql中有一个函数,专门用来分隔的 一时想不起来,S开头的,不急的话下午给你解答
好的,等你的答案
@彬彬@科比: 嗯!
@彬彬@科比: 不过不建议用那样的数据 '张三,李四' ,一个人一条数据不好么,
这样写还要效率转换不是一样慢么