请教一个SQL的写法

hh12345688 2018-03-14 05:04:58
请教一个SQL的写法

假设表A
列1 列2
a1 b1
a1 b2
a2 b3
a2 b4
a2 b5
a3 b6
a3 b1
a4 b2
a4 b3
a4 b4
a4 b5
a4 b6
想得到的效果如下:
列3 列4
1 b1
1 b2
2 b3
2 b4
2 b5
3 b6
3 b1
4 b2
4 b3
4 b4
4 b5
4 b6

...全文
355 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
吉普赛的歌 2018-03-14
  • 打赏
  • 举报
回复
USE tempdb
GO
IF OBJECT_ID('t') IS NOT NULL DROP TABLE t
GO
CREATE TABLE t(c1 VARCHAR(10),c2 VARCHAR(10))
GO
INSERT INTO t
SELECT 'a1','b1'
union ALL SELECT 'a1','b2'
union ALL SELECT 'a2','b3'
union ALL SELECT 'a2','b4'
union ALL SELECT 'a2','b5'
union ALL SELECT 'a3','b6'
union ALL SELECT 'a3','b1'
union ALL SELECT 'a4','b2'
union ALL SELECT 'a4','b3'
union ALL SELECT 'a4','b4'
union ALL SELECT 'a4','b5'
union ALL SELECT 'a4','b6'
 
SELECT dense_rank()OVER(ORDER BY c1) AS newC1,c1,c2 FROM t
/*
newC1                c1         c2
-------------------- ---------- ----------
1                    a1         b1
1                    a1         b2
2                    a2         b3
2                    a2         b4
2                    a2         b5
3                    a3         b6
3                    a3         b1
4                    a4         b2
4                    a4         b3
4                    a4         b4
4                    a4         b5
4                    a4         b6
*/
hh12345688 2018-03-14
  • 打赏
  • 举报
回复
引用 4 楼 sinat_28984567 的回复:

IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL
DROP FUNCTION DBO.GET_NUMBER2
GO
CREATE FUNCTION DBO.GET_NUMBER2(@S VARCHAR(100))
RETURNS VARCHAR(100)
AS
BEGIN
WHILE PATINDEX('%[^0-9]%',@S) > 0
BEGIN
set @s=stuff(@s,patindex('%[^0-9]%',@s),1,'')
END
RETURN @S
END
GO
--测试数据
if not object_id(N'Tempdb..#A') is null
	drop table #A
Go
Create table #A([列1] nvarchar(22),[列2] nvarchar(22))
Insert #A
select N'a1',N'b1' union all
select N'a1',N'b2' union all
select N'a2',N'b3' union all
select N'a2',N'b4' union all
select N'a2',N'b5' union all
select N'a3',N'b6' union all
select N'a3',N'b1' union all
select N'a4',N'b2' union all
select N'a4',N'b3' union all
select N'a4',N'b4' union all
select N'a4',N'b5' union all
select N'a4',N'b6'
Go
--测试数据结束
Select dbo.GET_NUMBER2(列1) AS 列1,列2 from #A

不好意思我举的列子不怎么好,重新举一下 假设表A 列1 列2 apple b1 apple b2 orange b3 orange b4 orange b5 banana b6 banana b1 grape b2 grape b3 grape b4 grape b5 grape b6 想得到的效果如下: 列3 列4 1 b1 1 b2 2 b3 2 b4 2 b5 3 b6 3 b1 4 b2 4 b3 4 b4 4 b5 4 b6
hh12345688 2018-03-14
  • 打赏
  • 举报
回复
引用 3 楼 yenange 的回复:
[quote=引用 2 楼 u012825833 的回复:] 不好意思因为是举例子,所以写的a1,a2,a3,a4实际可能是没规律的英文字母
我是取的第2个字母之后的内容, 你觉得有问题? 字母可能在后面? 希望去掉所有字母?[/quote] 不好意思我举的列子不怎么好,重新举一下 假设表A 列1 列2 apple b1 apple b2 orange b3 orange b4 orange b5 banana b6 banana b1 grape b2 grape b3 grape b4 grape b5 grape b6 想得到的效果如下: 列3 列4 1 b1 1 b2 2 b3 2 b4 2 b5 3 b6 3 b1 4 b2 4 b3 4 b4 4 b5 4 b6
吉普赛的歌 2018-03-14
  • 打赏
  • 举报
回复
引用 2 楼 u012825833 的回复:
不好意思因为是举例子,所以写的a1,a2,a3,a4实际可能是没规律的英文字母
我是取的第2个字母之后的内容, 你觉得有问题? 字母可能在后面? 希望去掉所有字母?
二月十六 2018-03-14
  • 打赏
  • 举报
回复

IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL
DROP FUNCTION DBO.GET_NUMBER2
GO
CREATE FUNCTION DBO.GET_NUMBER2(@S VARCHAR(100))
RETURNS VARCHAR(100)
AS
BEGIN
WHILE PATINDEX('%[^0-9]%',@S) > 0
BEGIN
set @s=stuff(@s,patindex('%[^0-9]%',@s),1,'')
END
RETURN @S
END
GO


--测试数据
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([列1] nvarchar(22),[列2] nvarchar(22))
Insert #A
select N'a1',N'b1' union all
select N'a1',N'b2' union all
select N'a2',N'b3' union all
select N'a2',N'b4' union all
select N'a2',N'b5' union all
select N'a3',N'b6' union all
select N'a3',N'b1' union all
select N'a4',N'b2' union all
select N'a4',N'b3' union all
select N'a4',N'b4' union all
select N'a4',N'b5' union all
select N'a4',N'b6'
Go
--测试数据结束
Select dbo.GET_NUMBER2(列1) AS 列1,列2 from #A



hh12345688 2018-03-14
  • 打赏
  • 举报
回复
引用 1 楼 yenange 的回复:
USE tempdb
GO
IF OBJECT_ID('t') IS NOT NULL DROP TABLE t
GO
CREATE TABLE t(c1 VARCHAR(10),c2 VARCHAR(10))
GO
INSERT INTO t
SELECT 'a1','b1'
union ALL SELECT 'a1','b2'
union ALL SELECT 'a2','b3'
union ALL SELECT 'a2','b4'
union ALL SELECT 'a2','b5'
union ALL SELECT 'a3','b6'
union ALL SELECT 'a3','b1'
union ALL SELECT 'a4','b2'
union ALL SELECT 'a4','b3'
union ALL SELECT 'a4','b4'
union ALL SELECT 'a4','b5'
union ALL SELECT 'a4','b6'

SELECT SUBSTRING(c1,2,LEN(c1)) AS newC1,c1,c2 FROM t
/*
newC1      c1         c2
---------- ---------- ----------
1          a1         b1
1          a1         b2
2          a2         b3
2          a2         b4
2          a2         b5
3          a3         b6
3          a3         b1
4          a4         b2
4          a4         b3
4          a4         b4
4          a4         b5
4          a4         b6
*/
不好意思因为是举例子,所以写的a1,a2,a3,a4实际可能是没规律的英文字母
吉普赛的歌 2018-03-14
  • 打赏
  • 举报
回复
USE tempdb
GO
IF OBJECT_ID('t') IS NOT NULL DROP TABLE t
GO
CREATE TABLE t(c1 VARCHAR(10),c2 VARCHAR(10))
GO
INSERT INTO t
SELECT 'a1','b1'
union ALL SELECT 'a1','b2'
union ALL SELECT 'a2','b3'
union ALL SELECT 'a2','b4'
union ALL SELECT 'a2','b5'
union ALL SELECT 'a3','b6'
union ALL SELECT 'a3','b1'
union ALL SELECT 'a4','b2'
union ALL SELECT 'a4','b3'
union ALL SELECT 'a4','b4'
union ALL SELECT 'a4','b5'
union ALL SELECT 'a4','b6'

SELECT SUBSTRING(c1,2,LEN(c1)) AS newC1,c1,c2 FROM t
/*
newC1      c1         c2
---------- ---------- ----------
1          a1         b1
1          a1         b2
2          a2         b3
2          a2         b4
2          a2         b5
3          a3         b6
3          a3         b1
4          a4         b2
4          a4         b3
4          a4         b4
4          a4         b5
4          a4         b6
*/

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧