表1 Fno fl tbh rwdbh
1 10 C20 2013-0001
2 12 C30 2013-0002
表2
Fno Fpan clmc clyl
1 1 砂子 100
1 1 石子 80
2 1 砂子 120
2 1 外加剂 10要得到如下结果
C20 10 砂子 100 石子 80 外加剂 0
1 10 C20 2013-0001
2 12 C30 2013-0002
表2
Fno Fpan clmc clyl
1 1 砂子 100
1 1 石子 80
2 1 砂子 120
2 1 外加剂 10要得到如下结果
C20 10 砂子 100 石子 80 外加剂 0
解决方案 »
- 一个多表关联查询的问题。
- 求一条多表关联语句
- 请问有人用过sql server7吗?
- 我动态构造某有40多个字段的表的所有字段的赋值给类型为nvarchar(4000)的字段,但是系统会自动截断!但是40多个字段美元可能超出4000的啊
- 急急急 在 windows server 2003 上 安装 企业版 SQLSERVER 2005 出现以上错误
- 有关字段值包含%的查询问题
- sql语句问题
- 请教数据库的死锁和ACCESS的性能问题
- VB中是怎么对SQL SERVER 数据库进行数据备份和恢复的,最好有程序?
- 这条SQL该如何写????
- mssql 记录集,延迟select出来的问题。
- 如何实现下列的数据
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-04-07 22:23:53
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
-- Jun 17 2011 00:57:23
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[表1]
if object_id('[表1]') is not null drop table [表1]
go
create table [表1]([Fno] int,[fl] int,[tbh] varchar(3),[rwdbh] varchar(9))
insert [表1]
select 1,10,'C20','2013-0001' union all
select 2,12,'C30','2013-0002'
if object_id('[表2]') is not null drop table [表2]
go
create table [表2]([Fno] int,[Fpan] int,[clmc] varchar(6),[clyl] int)
insert [表2]
select 1,1,'砂子',100 union all
select 1,1,'石子',80 union all
select 2,1,'砂子',120 union all
select 2,1,'外加剂',10--------------开始查询--------------------------SELECT tbh,fl,MAX(CASE WHEN clmc='砂子' THEN clmc ELSE NULL END )clmc,
MAX(CASE WHEN clmc='砂子' THEN clyl ELSE 0 END ) clyl,
MAX(CASE WHEN clmc='石子' THEN clmc ELSE NULL END )clmc,
MAX(CASE WHEN clmc='石子' THEN clyl ELSE 0 END ) clyl,
MAX(CASE WHEN clmc='外加剂' THEN clmc ELSE NULL END )clmc,
MAX(CASE WHEN clmc='外加剂' THEN clyl ELSE 0 END ) clyl
FROM [表1] a
INNER JOIN 表2 b ON a.fno = b.fno
GROUP BY tbh,fl
----------------结果----------------------------
/*
tbh fl clmc clyl clmc clyl clmc clyl
---- ----------- ------ ----------- ------ ----------- ------ -----------
C20 10 砂子 100 石子 80 NULL 0
C30 12 砂子 120 NULL 0 外加剂 10
*/
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-04-07 22:23:53
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
-- Jun 17 2011 00:57:23
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[表1]
if object_id('[表1]') is not null drop table [表1]
go
create table [表1]([Fno] int,[fl] int,[tbh] varchar(3),[rwdbh] varchar(9))
insert [表1]
select 1,10,'C20','2013-0001' union all
select 2,12,'C30','2013-0002'
if object_id('[表2]') is not null drop table [表2]
go
create table [表2]([Fno] int,[Fpan] int,[clmc] varchar(6),[clyl] int)
insert [表2]
select 1,1,'砂子',100 union all
select 1,1,'石子',80 union all
select 2,1,'砂子',120 union all
select 2,1,'外加剂',10--------------开始查询--------------------------SELECT tbh,fl,MAX(CASE WHEN clmc='砂子' THEN clmc ELSE '砂子' END )clmc,
MAX(CASE WHEN clmc='砂子' THEN clyl ELSE 0 END ) clyl,
MAX(CASE WHEN clmc='石子' THEN clmc ELSE '石子' END )clmc,
MAX(CASE WHEN clmc='石子' THEN clyl ELSE 0 END ) clyl,
MAX(CASE WHEN clmc='外加剂' THEN clmc ELSE '外加剂' END )clmc,
MAX(CASE WHEN clmc='外加剂' THEN clyl ELSE 0 END ) clyl
FROM [表1] a
INNER JOIN 表2 b ON a.fno = b.fno
GROUP BY tbh,fl
----------------结果----------------------------
/*
tbh fl clmc clyl clmc clyl clmc clyl
---- ----------- ------ ----------- ------ ----------- ------ -----------
C20 10 砂子 100 石子 80 外加剂 0
C30 12 砂子 120 石子 0 外加剂 10
*/
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-04-07 22:23:53
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
-- Jun 17 2011 00:57:23
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[表1]
if object_id('[表1]') is not null drop table [表1]
go
create table [表1]([Fno] int,[fl] int,[tbh] varchar(3),[rwdbh] varchar(9))
insert [表1]
select 1,10,'C20','2013-0001' union all
select 2,12,'C30','2013-0002'
if object_id('[表2]') is not null drop table [表2]
go
create table [表2]([Fno] int,[Fpan] int,[clmc] varchar(6),[clyl] int)
insert [表2]
select 1,1,'砂子',100 union all
select 1,1,'石子',80 union all
select 2,1,'砂子',120 union all
select 2,1,'外加剂',10--------------开始查询--------------------------
DECLARE @s NVARCHAR(MAX)
SET @s = ''
SELECT @s = @s + ',' + QUOTENAME('clmc') + '=max(case when [clmc]='
+ QUOTENAME(clmc, '''') + ' then [clmc] else clmc end)' + ','
+ QUOTENAME('clyl') + '=max(case when [clmc]=' + QUOTENAME(clmc, '''')
+ ' then [clyl] else 0 end)'
FROM [表1] a
INNER JOIN 表2 b ON a.fno = b.fno
GROUP BY clmc,clyl--tbh ,
--fl
EXEC('select tbh,fl'+@s+' from [表1] a
INNER JOIN 表2 b ON a.fno = b.fno group by tbh,fl')
----------------结果----------------------------
/*
tbh fl clmc clyl clmc clyl clmc clyl clmc clyl
---- ----------- ------ ----------- ------ ----------- ------ ----------- ------ -----------
C20 10 石子 100 石子 100 石子 80 石子 0
C30 12 外加剂 120 外加剂 120 外加剂 0 外加剂 10
*/