sql需求,大家帮帮忙拉

好奇都是要学的 2011-08-05 11:43:36
我现在有个基础型号表为A表, 我又创建了个B表为型号组表,B表的数据来源A表, 现在我创建了2个型号组为“型号A”,“型号B”,
A表
prac1 型号 flag
W-1 AAA 3
W-2 BBB 3
W-3 CCC 3
W-4 DDD 3
T-5 EEE 4
T-6 FFF 4
T-7 GGG 4
C1 QC 5
C2 EV 5
C3 QX 5
C4 BBE 5
B表
组名 型号 属性
型号A W-1 门套
型号A W-2 门套
型号A W-3 门套
型号B EEE 木门
型号B FFF 木门
型号C QC 装饰材料
型号C EV 装饰材料
型号C QX 装饰材料
在具体匹配型号的时候是排除选种的型号。也就是说根据排除B表数据into到C表, 才能使用 ,
B表属性如果为“门套”那么A表就取 SELECT distinct prac1 型号 FROM base_data where flag=3 and prac1<>''
B表属性如果为“木门”那么A表就取 SELECT distinct bname 型号 FROM base_data where flag=4 and bname<>''
B表属性如果为“装饰材料”那么A表就取 select distinct bname 型号 from base_data where flag=5 and (prac1 like '%线%' or prac1 like '%板%' or prac1 like '%条%') and bname<>''
最后结果为
C表
组名 型号 属性
型号A W-4 门套
型号B GGG 木门
型号C BBE 装饰材料

帮我分析下写出来。
...全文
161 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
AcHerat 元老 2011-08-05
  • 打赏
  • 举报
回复

create table a
(prac1 varchar(10),型号 varchar(10),flag int)
insert into a
select 'W-1','AAA',3 union all
select 'W-2','BBB',3 union all
select 'W-3','CCC',3 union all
select 'W-4','DDD',3 union all
select 'T-5','EEE',4 union all
select 'T-6','FFF',4 union all
select 'T-7','GGG',4 union all
select 'C1','QC',5 union all
select 'C2','EV',5 union all
select 'C3','QX',5 union all
select 'C4','BBE',5
go
create table b
(组名 varchar(10),型号 varchar(10),属性 varchar(10))
insert into b
select '型号A','W-1','门套' union all
select '型号A','W-2','门套' union all
select '型号A','W-3','门套' union all
select '型号B','EEE','木门' union all
select '型号B','FFF','木门' union all
select '型号C','QC','装饰材料' union all
select '型号C','EV','装饰材料' union all
select '型号C','QX','装饰材料'
go

select a.prac1,a.型号,a.flag,b.属性,b.组名
into #tb
from a left join b on b.型号 = (case when a.flag = 3 then a.prac1 else a.型号 end)

update t
set t.属性 = (select max(属性) from #tb where flag = t.flag),
t.组名 = (select max(组名) from #tb where flag = t.flag)
from #tb t

select 组名,型号,属性
into c
from #tb e
where not exists (select 1 from b where 组名 = e.组名
and 型号 = (case when e.flag = 3 then e.prac1 else e.型号 end))

select *
from c

drop table a,b,c,#tb


/***********

组名 型号 属性
---------- ---------- ----------
型号A DDD 门套
型号B GGG 木门
型号C BBE 装饰材料

(3 行受影响)
--小F-- 2011-08-05
  • 打赏
  • 举报
回复
prac1 like '%线%' or prac1 like '%板%' or prac1 like '%条%'
这些是从哪里来的哦?
AcHerat 元老 2011-08-05
  • 打赏
  • 举报
回复
楼主你本身给的数据有问题:


A表
prac1 型号 flag
W-1 AAA 3
W-2 BBB 3
W-3 CCC 3
W-4 DDD 3
T-5 EEE 4
T-6 FFF 4
T-7 GGG 4
C1 QC 5
C2 EV 5
C3 QX 5
C4 BBE 5
B表
组名 型号 属性
型号A W-1 门套
型号A W-2 门套
型号A W-3 门套
型号B EEE 木门
型号B FFF 木门
型号C QC 装饰材料
型号C EV 装饰材料
型号C QX 装饰材料



看看你B表型号对应的A表,这样能出来什么!
快溜 2011-08-05
  • 打赏
  • 举报
回复
可否给点创建表的sql?应该是对的吧
  • 打赏
  • 举报
回复
以上都不对。 都没考虑 属性条件, 都没读题啊。
快溜 2011-08-05
  • 打赏
  • 举报
回复
insert into c表
select a.组名,a.型号,b.属性
from B表 a,(select distinct 组名,属性 from B表) b
where a.组名=b.组名
and not exists(select 1 from A表 where prac1=a.型号)
AcHerat 元老 2011-08-05
  • 打赏
  • 举报
回复

select a.prac1,a.型号,a.flag,b.属性,b.组名
into #tb
from a left join b on a.型号 = b.型号

update t
set t.属性 = (select max(属性) from #tb where flag = t.flag),
t.组名 = (select max(组名) from #tb where flag = t.flag)
from #tb t

insert into c
select 组名,型号,属性
from #tb e
where not exists (select 1 from b where 组名 = e.组名 and 型号 = e.型号)

drop table #tb
  • 打赏
  • 举报
回复
自己顶下, 怎么没人那。我可以追家分的
  • 打赏
  • 举报
回复
中午搞定了。 用的while 写了个循环, 比指针舒服多了。 呵呵,谢谢大家啊
慌慌丨张张 2011-08-05
  • 打赏
  • 举报
回复
学习中。顶。。。
  • 打赏
  • 举报
回复
哎, 我觉得设计这个SQL的也很闹心, 我是后来这个公司。 他们表都见好了, 我就只能用了。 添加数据还来个非。 取所有不在组里的型号, not in就行, 但是基础表里。 乱78遭数据一带堆,
fancy0109 2011-08-05
  • 打赏
  • 举报
回复
你还是雇一个稍微严谨点的人吧,再二的人也不应该弄出这么两个表的内容
fancy0109 2011-08-05
  • 打赏
  • 举报
回复
declare @t1 table(prac1 varchar(10), 型号 varchar(10), flag int)
declare @t2 table(组名 varchar(10), 型号 varchar(10), 属性 varchar(10))

insert into @t1(prac1, 型号, flag)
select 'W-1','AAA',3 union all
select 'W-2','BBB',3 union all
select 'W-3','CCC',3 union all
select 'W-4','DDD',3 union all
select 'T-5','EEE',4 union all
select 'T-6','FFF',4 union all
select 'T-7','GGG',4 union all
select 'C1','QC',5 union all
select 'C2','EV',5 union all
select 'C3','QX',5 union all
select 'C4','BBE',5

insert into @t2(组名, 型号, 属性)
select '型号A','W-1','门套' union all
select '型号A','W-2','门套' union all
select '型号A','W-3','门套' union all
select '型号B','EEE','木门' union all
select '型号B','FFF','木门' union all
select '型号C','QC','装饰材料' union all
select '型号C','EV','装饰材料' union all
select '型号C','QX','装饰材料'

select '型号' + case when left(prac1, 1) = 'W' then 'A'
when left(prac1, 1) = 'T' then 'B'
when left(prac1, 1) = 'C' then 'C' end 组名,
case when left(prac1, 1) = 'W' then a.prac1 else a.型号 end 型号,
case when left(prac1, 1) = 'W' then '门套'
when left(prac1, 1) = 'T' then '木门'
when left(prac1, 1) = 'C' then '装饰材料' end 属性
from @T1 a left join @T2 b on case when left(prac1, 1) = 'W' then a.prac1 else a.型号 end = b.型号
where b.型号 is null

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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