求算法

foxal 2011-06-30 05:10:02
有这样两组数据

---------------------------
数据1
---------------------------
ID 产品
1 A
2 B
3 C
---------------------------
数据2
---------------------------
ID 产品ID 颜色
1 A 红
1 A 黄
2 B 红
3 C 红
3 C 绿
3 C 黑
---------------------------
如果不用循环的话怎样能得出下面的结果
---------------------------
结果
---------------------------
ID 产品 颜色
1 A 红
Null Null 黄
2 B 红
3 C 红
Null Null 绿
Null Null 黑
---------------------------

本人想了很久没有想出算法,如果用循环的话查询的时间太长,求高手赐教
---------------------------
...全文
91 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hello926 2011-07-02
  • 打赏
  • 举报
回复

CREATE TABLE a(id int,productname varchar(10))
insert into a
select 1, 'A' union all
select 2 ,'B' union all
select 3 ,'C'

create table b(id int ,productname varchar(10),color nvarchar(10))
insert into b
select 1, 'A', N'红' UNION ALL
select 1, 'A', N'黄' UNION ALL
select 2, 'B', N'红' UNION ALL
select 3, 'C', N'红' UNION ALL
select 3, 'C', N'绿' UNION ALL
select 3, 'C', N'黑'

with t
as(
select ret=row_number() over(partition by a.id order by a.id), a.id,a.productname, b.color from a right join b on a.id=b.id)
select case when ret=1 then id else null end,case when ret=1 then productname else null end ,color from t
cd731107 2011-06-30
  • 打赏
  • 举报
回复


create table t2(id int,product varchar(5),color varchar(10))
insert into t2
select 1,'a','red' union all
select 1,'a','yellow' union all
select 2,'b','blue' union all
select 2,'b','green' union all
select 2,'b','red' union all
select 3,'c','red' union all
select 3,'c','blue'
go

declare @a int,@b int
select @a=0,@b=0
update t2 set @a=@b,@b=id,product=case when id<>@a then product end, id=case when id<>@a then id end
from t2

select* from t2

drop table t2
/*
id product color
----------- ------- ----------
1 a red
NULL NULL yellow
2 b blue
NULL NULL green
NULL NULL red
3 c red
NULL NULL blue

(所影响的行数为 7 行)
*/
foxal 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 acherat 的回复:]

SQL code

--排序后的结果集的样式应该是:
/*
id product color rid
----------- ------- ---------- --------------------
1 a red 1
1 a yellow 2
2 b ……
[/Quote]

嗯!非常感谢!又学到了很有用的知识。
老潘 2011-06-30
  • 打赏
  • 举报
回复
DECLARE @t table(
ID int
,产品ID varchar(2)
,颜色 varchar(2)
);
INSERT INTO @t
SELECT 1, 'A', '红' UNION ALL
SELECT 1, 'A', '黄' UNION ALL
SELECT 2, 'B', '红' UNION ALL
SELECT 3, 'C', '红' UNION ALL
SELECT 3, 'C', '绿' UNION ALL
SELECT 3, 'C', '黑'
;

WITH t AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY ID ORDER BY 产品ID) AS RowNo,*
FROM @t
)
SELECT CASE WHEN t2.ID IS NULL THEN t1.ID ELSE NULL END AS ID
,CASE WHEN t2.ID IS NULL THEN t1.产品ID ELSE NULL END AS 产品ID
,t1.颜色
FROM t t1 LEFT JOIN t t2 ON t1.ID=t2.ID AND t1.RowNo=t2.RowNo+1
;

/*结果
ID 产品ID 颜色
----------- ---- ----
1 A 红
NULL NULL 黄
2 B 红
3 C 红
NULL NULL 绿
NULL NULL 黑
*/
AcHerat 2011-06-30
  • 打赏
  • 举报
回复

--排序后的结果集的样式应该是:
/*
id product color rid
----------- ------- ---------- --------------------
1 a red 1
1 a yellow 2
2 b blue 1
2 b green 2
2 b red 3
3 c red 1
3 c blue 2
*/
--那么将上边rid为1也就是序号为1的id和product显示,其他显示为null,这里用到case when。
--也就是上面几楼给出的语句,思想是这样。
foxal 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acherat 的回复:]

SQL code

create table t1(id int,product varchar(5))
insert into t1
select 1,'a' union all
select 2,'b' union all
select 3,'c'
go

create table t2(id int,product varchar(5),color varchar(10))
insert……
[/Quote]

非常感谢您的帮助!
foxal 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 maco_wang 的回复:]

SQL code

declare @数据1 table (ID int,产品 varchar(1))
insert into @数据1
select 1,'A' union all
select 2,'B' union all
select 3,'C'

declare @数据2 table (ID int,产品ID varchar(1),颜色 varchar(2))
insert into……
[/Quote]


明白啦!非常感谢!虽然我举的例子跟我实际做的有很大差别,不过您的提供的思路对我有很大的帮助!非常感谢!
同时也感谢2楼的朋友!
AcHerat 2011-06-30
  • 打赏
  • 举报
回复

create table t1(id int,product varchar(5))
insert into t1
select 1,'a' union all
select 2,'b' union all
select 3,'c'
go

create table t2(id int,product varchar(5),color varchar(10))
insert into t2
select 1,'a','red' union all
select 1,'a','yellow' union all
select 2,'b','blue' union all
select 2,'b','green' union all
select 2,'b','red' union all
select 3,'c','red' union all
select 3,'c','blue'
go

--SQL2005
with cte as
(
select a.id,a.product,b.color,px=row_number() over (partition by a.id,a.product order by getdate())
from t1 a join t2 b on a.id = b.id
)

select (case when px=1 then id else null end) as id,
(case when px=1 then product else null end) as product,color
from cte

/*
id product color
----------- ------- ----------
1 a red
NULL NULL yellow
2 b blue
NULL NULL green
NULL NULL red
3 c red
NULL NULL blue

(7 行受影响)
*/

--SQL200

select *,rid = identity(int,1,1)
into #tb
from t2

select (case when (select count(*) from #tb where id = t.id and rid <= t.rid)=1 then id else null end) as id,
(case when (select count(*) from #tb where id = t.id and rid <= t.rid)=1 then product else null end) as product,color
from #tb t

/*
id product color
----------- ------- ----------
1 a red
NULL NULL yellow
2 b blue
NULL NULL green
NULL NULL red
3 c red
NULL NULL blue

(7 行受影响)
*/

drop table t1,t2,#tb
foxal 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acherat 的回复:]

简单来说是按你两表查询的结果集的ID和产品分组排序,序号为1的显示ID和产品名,其他的为空或者NULL。
[/Quote]

呃,说实话,不是很理解,您能稍微详细的解释一下么,尤其是这个“序号为1”......
叶子 2011-06-30
  • 打赏
  • 举报
回复

declare @数据1 table (ID int,产品 varchar(1))
insert into @数据1
select 1,'A' union all
select 2,'B' union all
select 3,'C'

declare @数据2 table (ID int,产品ID varchar(1),颜色 varchar(2))
insert into @数据2
select 1,'A','红' union all
select 1,'A','黄' union all
select 2,'B','红' union all
select 3,'C','红' union all
select 3,'C','绿' union all
select 3,'C','黑'

select b.*,a.颜色 from
(select row_number() over (partition by 产品ID order by ID) as rid,* from @数据2) a
left join @数据1 b on a.产品ID=b.产品 and a.rid=1
/*
ID 产品 颜色
----------- ---- ----
1 A 红
NULL NULL 黄
2 B 红
3 C 红
NULL NULL 绿
NULL NULL 黑
*/
AcHerat 2011-06-30
  • 打赏
  • 举报
回复
简单来说是按你两表查询的结果集的ID和产品分组排序,序号为1的显示ID和产品名,其他的为空或者NULL。
AcHerat 2011-06-30
  • 打赏
  • 举报
回复

with cte as
(
select a.id,a.product,b.color,px=row_number() over (partition by a.id,a.product order by getdate())
from a join b on a.id = b.id
)

select (case when px=1 then id else '' end) as id,
(case when px=1 then product else '' end) as product,color
from cte

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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