永发信息网

SQL sever 按条件拆分每行数据怎么实现

答案:2  悬赏:60  手机版
解决时间 2021-02-18 14:43
比如我的是数据是左边的图,想做成右边的图

如果类别是A,那么不用改变,直接放在一行里;
如果类别是B,那么就是从它‘代码’里抽出3个,每种抽法放一行,比如4选3,就要拆成4行;
如果类别是C,那么从中括号里选择一个配上括号外的数字组成三个数字,每种情况占一行。
每一种拆分都要新建成单独一行。要用case when么?then之后是不是不能插入特别长的,因为之后还要把“1+2+6”拆分为三列,所以比较麻烦
求助!多谢多谢了!回答细致可以提高悬赏分



最佳答案
DECLARE @Room INT,
@Category NCHAr(50),
@Price DECIMAL(10,2),
@StartDate DATE,
@EndDate DATE,
@Index INT,
@TotalDayDiff INT,
@CurrentMonthStart DATE,
@CurrentMonthEnd DATE

DECLARE @Test TABLE
(
ID INT IDENTITY(1,1),
Room INT,
Category NCHAr(50),
Price DECIMAL(10,2),
StartDate DATE,
EndDate DATE
)
INSERT @Test SELECt 1905, N'房租', 3000, '2015-02-06','2015-04-05'
UNIOn SELECT 2000, N'你欠的', 5555, '2014-04-20', '2015-02-07'
SELECT * FROM @Test

DECLARE @Test2 TABLE
(
CurrentMonth CHAr(7),
Room INT,
Category NCHAr(50),
Price VARCHAr(50)
)

SET @Index = 1
WHILE @Index <= (SELECt MAX(ID) FROM @Test)
BEGIN
SELECt @Room = Room, @Category = Category, @Price = Price, @StartDate = StartDate, @EndDate = EndDate
FROM @Test
WHERe ID = @Index

SELECt @TotalDayDiff = DATEDIFF(DAY, @StartDate, @EndDate) + 1
WHILE @StartDate <= @EndDate
BEGIN
SET @CurrentMonthStart = @StartDate
IF YEAR(@StartDate) = YEAR(@EndDate) AND MONTH(@StartDate) = MONTH(@EndDate)
BEGIN
SET @CurrentMonthEnd = @EndDate
END
ELSE
BEGIN
SET @CurrentMonthEnd = DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m,0,@StartDate) + 1, 0))
END

INSERT @Test2 VALUES (
CAST(YEAR(@CurrentMonthStart) AS VARCHAR) + ' ' + CAST(MONTH(@CurrentMonthStart) AS VARCHAR),
@Room,
@Category,
CAST(@Price AS VARCHAR) + '*' + CAST(DATEDIFF(DD, @CurrentMonthStart, @CurrentMonthEnd) + 1 AS VARCHAR) + '/' + CAST(@TotalDayDiff AS VARCHAR))
SET @StartDate = DATEADD(MM, 1, DATEADD(mm, DATEDIFF(mm,0,@StartDate), 0))
END

SET @Index = @Index + 1
END
SELECT * FROM @Test2
全部回答
declare @room int, @category nchar(50), @price decimal(10,2), @startdate date, @enddate date, @index int, @totaldaydiff int, @currentmonthstart date, @currentmonthend date declare @test table ( id int identity(1,1), room int, category nchar(50), price decimal(10,2), startdate date, enddate date ) insert @test select 1905, n'房租', 3000, '2015-02-06','2015-04-05' union select 2000, n'你欠的', 5555, '2014-04-20', '2015-02-07' select * from @test declare @test2 table ( currentmonth char(7), room int, category nchar(50), price varchar(50) ) set @index = 1 while @index begin select @room = room, @category = category, @price = price, @startdate = startdate, @enddate = enddate from @test where id = @index select @totaldaydiff = datediff(day, @startdate, @enddate) + 1 while @startdate begi... union select 2000, datediff(m, @currentmonthstart, price decimal(10, @currentmonthend) + 1 as varchar) + '', startdate date, 0)) end insert @test2 values ( cast(year(@currentmonthstart) as varchar) + ', category nchar(50),@startdate) + 1, @room;2014-04-20', @startdate date;, category nchar(50);/, @enddate date, @enddate = enddate from @test where id = @index select @totaldaydiff = datediff(day, datediff(mm, @category nchar(50), 1,@startdate),1),',0, 3000, dateadd(mm; '你欠的',0, @startdate = startdate, enddate date ) insert @test select 1905;;,2), ', 5555, n', dateadd(mm, room int; select * from @test declare @test2 table ( currentmonth char(7); + cast(@totaldaydiff as varchar)) set @startdate = dateadd(mm, @category, @enddate) + 1 while @startdate <, n', @price decimal(10; + cast(datediff(dd, @price = price;2015-02-07', @category = category;= (select max(id) from @test) begin select @room = room;2015-02-06';*'= @enddate begin set @currentmonthstart = @startdate if year(@startdate) = year(@enddate) and month(@startdate) = month(@enddate) begin set @currentmonthend = @enddate end else begin set @currentmonthend = dateadd(ms, @index int, cast(@price as varchar) + ', ', @totaldaydiff int;房租',2); + cast(month(@currentmonthstart) as varchar), -3, @currentmonthstart datedeclare @room int;2015-04-05', room int, price varchar(50) ) set @index = 1 while @index <, @startdate, @currentmonthend date declare @test table ( id int identity(1, '
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
对你有意思的女生怎么去追?
we can use the flash disk for
我电脑是一体机,请问可以买个摄像头连接视频
【10万个为什么】为什么10万个为什么有10万个
放大wifi信号是买外置网卡好还是用路由做中继
隐形眼镜脱下来没放护理液 两天以后发现再放
大家好,请问栽花需要什么样的土?
阳光舜苑这个地址在什么地方,我要处理点事
植物叶肉细胞中不能合成ATP的细胞结构是BA.
买电动保洁车应该注意哪些方面?
慈霖佛店地址有知道的么?有点事想过去
本人是外省二本学校电气自动化专业的学生,想
【有理数的加法练习题】有理数加法练习题如题
单选题若20种氨基酸的平均分子量为128,由100
怡人烩面城地址有知道的么?有点事想过去
推荐资讯
小米手机4为什么不支持双4g?后续可以自己破
河北张北一中2015年中考录取分数线是多少
金属加工车间属于()。
什么是汗斑
顺义有资质的动物医院,可以给母猫做绝育手术
画出kmno4制取氧气的装制图,并标出仪器名称
她说,天天累死了。我怎样回复比较幽默?
独生子女费一般是每年多少钱?可以领多少年?
常州市焦氏工具厂地址有知道的么?有点事想过
刚领回家的小猫很凶怎么办
关于用水粉画科技的画七年级作品
用PLC如何控制电梯运行
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?