A、B
有两个表
A:
colunm1 colunm2 colunm3
a1 2 1
b1 4 2
a1 5 3
f1 6 1
b1 2 8 B:
colunm1 colunm2
a1 8
b1 11
e2 9
能用查到这样一个结果吗
colunm1 colunm2 colunm3 colunm4
a1 2 1 8
b1 4 2 11
a1 5 3 null
f1 6 1 null
b1 9 8 nullA,B表,只有colunm1有关联,c其他字段都没有一定的规律和大小关系,请教大家如何解决。前天写过可能是表数据引起大家的误解,不好意思,现在急需大家帮忙。谢谢
有两个表
A:
colunm1 colunm2 colunm3
a1 2 1
b1 4 2
a1 5 3
f1 6 1
b1 2 8 B:
colunm1 colunm2
a1 8
b1 11
e2 9
能用查到这样一个结果吗
colunm1 colunm2 colunm3 colunm4
a1 2 1 8
b1 4 2 11
a1 5 3 null
f1 6 1 null
b1 9 8 nullA,B表,只有colunm1有关联,c其他字段都没有一定的规律和大小关系,请教大家如何解决。前天写过可能是表数据引起大家的误解,不好意思,现在急需大家帮忙。谢谢
解决方案 »
- Sqlserver 如何对一个字段的不同值进行统计
- SQL Server2000之最
- 请问为何我的"全文索引表"是灰色的不可用,另外服务管理器下拉框内也没有Microsoft Search service
- 请问如何查看某个未提交的SQL进程ID下尚未提交的SQL语句?
- 一条SQL语句是不是默认为一个事务
- 急!!sql存储过程中遇到个错误 实在找不到原因请大家 在线等
- 数据库表设计的一个问题
- 请问哪里有最新的msde下载?
- 请问MS-SQL7.0中如何把本机当服务器并在本机登陆?
- 在线等待,紧急求救:SQL server 7中数据处理停顿问题
- 关于ORALCE中的空格
- nvarchar变成varchar,中文乱码!是不是因为sql server服务器是英文版?
colunm1 colunm2 colunm3 colunm4
a1 2 1 8
b1 4 2 11
a1 5 3 8
f1 6 1 null
b1 9 8 11
from A left join B on a.column1=b.column1
column4=case when exists(select 1 from a where column1=t.column1 and column3<t.column3 ) then null else b.colunm2 end
from A t left join B on t.column1=b.column1
select a.colunm1,a.colunm2,a.colunm3 ,d.colunm4 from a left outer join (
select a.colunm1,a.colunm2,a.colunm3,b.colunm2 as colunm4 from a inner join b on a.colunm1=b.colunm1
where exists (select 1 from a c where c.colunm1=a.colunm1 and c.colunm2>a.colunm2))d
on a.colunm1=d.colunm1 and a.colunm2=d.colunm2 and a.colunm3=d.colunm3--结果
a1 2 1 8
a1 5 3 NULL
b1 9 8 NULL
b1 4 2 11
f1 6 1 NULL
CREATE TABLE #T1(colunm1 nvarchar(20),colunm2 int,colunm3 int)
INSERT INTO #T1
SELECT 'a1',2,1 UNION ALL
SELECT 'b1',4,2 UNION ALL
SELECT 'a1',5,3 UNION ALL
SELECT 'f1',6,1 UNION ALL
SELECT 'b1',2,8CREATE TABLE #T2(colunm1 nvarchar(20),colunm2 int)
INSERT INTO #T2
SELECT 'a1',8 UNION ALL
SELECT 'b1',11 UNION ALL
SELECT 'e2',9 SELECT IDENTITY(int,1,1) AS [id],* INTO #T FROM #T1
SELECT A.*,B.colunm2 AS colunm4 FROM #T AS A
LEFT OUTER JOIN #T2 AS B ON B.colunm1=A.colunm1
AND NOT EXISTS(SELECT 1 FROM #T AS C WHERE C.colunm1=A.colunm1 AND C.[id]<A.[id])
DROP TABLE #T1,#T2,#T
/*
id colunm1 colunm2 colunm3 colunm4
---------------------------------------
1 a1 2 1 8
2 b1 4 2 11
3 a1 5 3 NULL
4 f1 6 1 NULL
5 b1 2 8 NULL
*/
--只要A表有一个唯一标志的自增ID就好办了,不然得使用临时表新建一个唯一标志的自增ID。
declare @A table(colunm1 varchar(20),colunm2 varchar(20),colunm3 int)
insert @A select
'a1' ,'2', 1 union all select
'b1' ,'4', 2 union all select
'a1' ,'5', 3 union all select
'f1' ,'6', 1 union all select
'b1' ,'2', 8
declare @b TABLE(colunm1 VARCHAR(20),colunm2 VARCHAR(20))
INSERT @b SELECT
'a1', 8 union all select
'b1', 11 union all select
'e2', 9 --查詢
SELECT a.colunm1,a.colunm2,a.colunm3,case when RowNo=1 then b.colunm2 else null end as colunm4
from (select *,RANK() OVER(partition by colunm1 order by colunm1,colunm2,colunm3) as RowNo from @A) a
left join @B b on a.colunm1=b.colunm1 /*
結果:
colunm1 colunm2 colunm3 RowNo colunm4
-------------------- -------------------- ----------- -------------------- --------------------
a1 2 1 1 8
a1 5 3 2 NULL
b1 2 8 1 11
b1 4 2 2 NULL
f1 6 1 1 NULL
*/--應該沒有此記錄
--b1 9 8 null
結果:
colunm1 colunm2 colunm3 colunm4
-------------------- -------------------- ----------- --------------------
a1 2 1 8
a1 5 3 NULL
b1 2 8 11
b1 4 2 NULL
f1 6 1 NULL
*/
不过没有说具体SQL语言
(select top 1 column2 from A where column1=b.column1 order by column2,column3) as column3,b.column2 as column4
from b where exists(select * from A where a.column1=b.column1)
用A表与以上查询结果进行左连接,on的条件为column1,column2,column3即可得到结果