*************暴难的问题(续)******************

einsteincao 2004-09-21 03:27:01
如何写出如下条件的Query

FS_VATCustomerCode:

id VATCustomerCode
101 John
102 Mike
103 Jully



FS_VATProductCode:

id VATProductCode
101 Bike
102 Car
103 Tractor




FS_VATProductCustomerCombination:

VATCustomerCode VATProductCode Tax
John Bike 2
John Car 500
Mike Tractor 149
Jully Bike 2
Jully Car 388
Jully Tractor 149




希望用一个表变量,实现如下结果集:


ColumnA | ColumnB
------------------------------|-------------------------------------------
ProductCode | John Mike Jully
Bike | 2 2
Car | 500 388
Tractor | 149 149





也就是说,表变量中第二个字段ColumnB是拼串拼出来的..... 中间由六个空格格开
...全文
185 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-09-22
  • 打赏
  • 举报
回复
CREATE TABLE [FS_VATCustomerCode] (
[VATCustomerCode] [char] (5) COLLATE Latin1_General_BIN NOT NULL ,
[VATCustomerCodeKey] [int] NOT NULL
) ON [PRIMARY]
insert FS_VATCustomerCode select 'John' ,1
union all select 'Mike' ,2
union all select 'Jully',3
GO


CREATE TABLE [FS_VATProductCode] (
[VATProductCode] [varchar] (12) COLLATE Latin1_General_BIN NOT NULL ,
[VATProductCodeKey] [int] NOT NULL
) ON [PRIMARY]
insert FS_VATProductCode select 'Bike' ,1
union all select 'Car' ,2
union all select 'Tractor',3
GO


CREATE TABLE [FS_VATProductCustomerCombination ] (
[VATCustomerCode] [char] (5) COLLATE Latin1_General_BIN NOT NULL ,
[VATProductCode] [varchar] (12) COLLATE Latin1_General_BIN NOT NULL ,
[PrimaryTaxCode] [char] (2) COLLATE Latin1_General_BIN NOT NULL

) ON [PRIMARY]
insert FS_VATProductCustomerCombination select 'John' ,'Bike' ,'2'
union all select 'John' ,'Car' ,'3'
union all select 'Mike' ,'Tractor','4'
union all select 'Jully','Bike' ,'5'
union all select 'Jully','Car' ,'6'
union all select 'Jully','Tractor','7'
GO

declare @s2 varchar(8000),@s3 varchar(8000)
select @s2='',@s3=''
select @s2=@s2+'+cast('''' as char(6))+cast('''+rtrim(VATCustomerCode)+''' as char(10))'
,@s3=@s3+'+cast('''' as char(6))+cast(isnull(max(case b.VATCustomerCode when '''
+rtrim(VATCustomerCode)+''' then PrimaryTaxCode end),''FD'') as char(10))'
from FS_VATCustomerCode order by VATCustomerCodeKey
select @s2=stuff(@s2,1,21,''),@s3=stuff(@s3,1,21,'')
exec('
declare @tb table(ColumnA varchar(20),ColumnB varchar(8000))
insert @tb
select ''ProductCode'','+@s2+'
insert @tb
select b.VATProductCode,'+@s3+'
from FS_VATProductCode a,FS_VATProductCustomerCombination b
where a.VATProductCode=b.VATProductCode
group by b.VATProductCode,a.VATProductCodeKey
order by a.VATProductCodeKey
select * from @tb')
go
drop table FS_VATCustomerCode,FS_VATProductCode,FS_VATProductCustomerCombination

/*--测试结果

ColumnA ColumnB
-------------------- -----------------------------------------
ProductCode John Mike Jully
Bike 2 FD 5
Car 3 FD 6
Tractor FD 4 7
--*/
Rewiah 2004-09-22
  • 打赏
  • 举报
回复
太懒
einsteincao 2004-09-22
  • 打赏
  • 举报
回复
CREATE TABLE [FS_VATCustomerCode] (
[VATCustomerCode] [char] (5) COLLATE Latin1_General_BIN NOT NULL ,
[VATCustomerCodeKey] [int] NOT NULL
) ON [PRIMARY]
GO


CREATE TABLE [FS_VATProductCode] (
[VATProductCode] [varchar] (12) COLLATE Latin1_General_BIN NOT NULL ,
[VATProductCodeKey] [int] NOT NULL
) ON [PRIMARY]
GO



CREATE TABLE [FS_VATProductCustomerCombination ] (
[PrimaryTaxCode] [char] (2) COLLATE Latin1_General_BIN NOT NULL ,
[VATCustomerCode] [char] (5) COLLATE Latin1_General_BIN NOT NULL ,
[VATProductCode] [varchar] (12) COLLATE Latin1_General_BIN NOT NULL
) ON [PRIMARY]
GO
zjcxc 元老 2004-09-22
  • 打赏
  • 举报
回复
表结构也给出来吧,方便测试
einsteincao 2004-09-22
  • 打赏
  • 举报
回复
没错 我的VATCustomerCode 是Char类型
但是无论怎么 ltrim(rtrim(VATCustomerCode))
都没有得到希望得到的结果...
einsteincao 2004-09-22
  • 打赏
  • 举报
回复
declare @s2 varchar(8000),@s3 varchar(8000)
select @s2='',@s3=''
select @s2=@s2+'+space(6)+cast('''+VATCustomerCode+''' as char(10))'
,@s3=@s3+'+space(6)+cast(max(case b.VATCustomerCode when '''
+VATCustomerCode+''' then PrimaryTaxCode else ''FD'' end) as char(10))'
from FS_VATCustomerCode order by VATCustomerCodeKey
select @s2=stuff(@s2,1,10,''),@s3=stuff(@s3,1,10,'')
exec('
declare @tb table(ColumnA varchar(20),ColumnB varchar(8000))
insert @tb
select ''ProductCode'','+@s2+'
insert @tb
select b.VATProductCode,'+@s3+'
from FS_VATProductCode a,FS_VATProductCustomerCombination b
where a.VATProductCode=b.VATProductCode
group by b.VATProductCode,a.VATProductCodeKey
order by a.VATProductCodeKey
select * from @tb')



有一点小出入 第五行 PrimaryTaxCode --Tax
第六行 VATCustomerCodeKey - id

倒数第二行 a.VATProductCodeKey --a.id
最末行 a.VATProductCodeKey --a.id

zjcxc 元老 2004-09-22
  • 打赏
  • 举报
回复
把你的代码写出来.
Rewiah 2004-09-22
  • 打赏
  • 举报
回复
数据类型问题
邹键的VATCustomerCode是varchar的,你的是char的,你这么改:

select @s2=@s2+'+space(6)+cast('''+VATCustomerCode+''' as char(10))'
,@s3=@s3+'+space(6)+cast(max(case b.VATCustomerCode when '''
+VATCustomerCode+''' then Tax else ''*'' end) as char(10))'

--〉
select @s2=@s2+'+space(6)+cast('''+rtrim(VATCustomerCode)+''' as char(10))'
,@s3=@s3+'+space(6)+cast(max(case b.VATCustomerCode when '''
+rtrim(VATCustomerCode)+''' then Tax else ''*'' end) as char(10))'

呵呵
einsteincao 2004-09-22
  • 打赏
  • 举报
回复
* 是一个数字类型??
试了一下,转换为1 也能成功

但是实际情况是我必须转换为字符类型的啊
Rewiah 2004-09-22
  • 打赏
  • 举报
回复
退步了

这个贴的结果要求ColumnB 乱七八糟这么多数据,不如前面的贴的要求

einsteincao 2004-09-22
  • 打赏
  • 举报
回复
是这样的... 如果我替代为 '*' 结果没有问题
但是实际情况是我需要替代为'FD' 结果就变成

ColumnA ColumnB
-------------------- -----------------------------------------
ProductCode FD FD FD
Bike FD FD FD
Car FD FD FD
Tractor FD FD FD

的错误结果了...
为什么呢?
einsteincao 2004-09-22
  • 打赏
  • 举报
回复
奇怪 我的为什么不成功呢...

结果:


ColumnA ColumnB
-------------------- -----------------------------------------
ProductCode * * *
Bike * * *
Car * * *
Tractor * * *
zjcxc 元老 2004-09-22
  • 打赏
  • 举报
回复
--示例

--示例数据
create table FS_VATCustomerCode(id int,VATCustomerCode varchar(20))
insert FS_VATCustomerCode select 1,'John'
union all select 2,'Mike'
union all select 3,'Jully'

create table FS_VATProductCode(id int,VATProductCode varchar(20))
insert FS_VATProductCode select 1,'Bike'
union all select 2,'Car'
union all select 3,'Tractor'

create table FS_VATProductCustomerCombination(VATCustomerCode varchar(20),VATProductCode varchar(20),Tax varchar(20))
insert FS_VATProductCustomerCombination select 'John' ,'Bike' ,'2'
union all select 'John' ,'Car' ,' 500'
union all select 'Mike' ,'Tractor','149'
union all select 'Jully','Bike' ,'2'
union all select 'Jully','Car' ,'388'
union all select 'Jully','Tractor','149'
go

--查询
declare @s2 varchar(8000),@s3 varchar(8000)
select @s2='',@s3=''
select @s2=@s2+'+space(6)+cast('''+VATCustomerCode+''' as char(10))'
,@s3=@s3+'+space(6)+cast(max(case b.VATCustomerCode when '''
+VATCustomerCode+''' then Tax else ''*'' end) as char(10))'
from FS_VATCustomerCode order by id
select @s2=stuff(@s2,1,10,''),@s3=stuff(@s3,1,10,'')
exec('
declare @tb table(ColumnA varchar(20),ColumnB varchar(8000))
insert @tb
select ''ProductCode'','+@s2+'
insert @tb
select b.VATProductCode,'+@s3+'
from FS_VATProductCode a,FS_VATProductCustomerCombination b
where a.VATProductCode=b.VATProductCode
group by b.VATProductCode,a.id
order by a.id
select * from @tb')
go

drop table FS_VATCustomerCode,FS_VATProductCode,FS_VATProductCustomerCombination

/*--测试结果

ColumnA ColumnB
-------------------- -----------------------------------------
ProductCode John Mike Jully
Bike 2 * 2
Car * * 388
Tractor * 149 149
--*/

einsteincao 2004-09-22
  • 打赏
  • 举报
回复
我想把Query出来的结果集中的空白,替换为"*"

该如何考虑呢?
chinaandys 2004-09-21
  • 打赏
  • 举报
回复
搜一下大把的例子 case
zjcxc 元老 2004-09-21
  • 打赏
  • 举报
回复
--示例

--示例数据
create table FS_VATCustomerCode(id int,VATCustomerCode varchar(20))
insert FS_VATCustomerCode select 1,'John'
union all select 2,'Mike'
union all select 3,'Jully'

create table FS_VATProductCode(id int,VATProductCode varchar(20))
insert FS_VATProductCode select 1,'Bike'
union all select 2,'Car'
union all select 3,'Tractor'

create table FS_VATProductCustomerCombination(VATCustomerCode varchar(20),VATProductCode varchar(20),Tax varchar(20))
insert FS_VATProductCustomerCombination select 'John' ,'Bike' ,'2'
union all select 'John' ,'Car' ,' 500'
union all select 'Mike' ,'Tractor','149'
union all select 'Jully','Bike' ,'2'
union all select 'Jully','Car' ,'388'
union all select 'Jully','Tractor','149'
go

--查询
declare @s2 varchar(8000),@s3 varchar(8000)
select @s2='',@s3=''
select @s2=@s2+'+space(6)+cast('''+VATCustomerCode+''' as char(10))'
,@s3=@s3+'+space(6)+cast(max(case b.VATCustomerCode when '''
+VATCustomerCode+''' then Tax else '''' end) as char(10))'
from FS_VATCustomerCode order by id
select @s2=stuff(@s2,1,10,''),@s3=stuff(@s3,1,10,'')
exec('
declare @tb table(ColumnA varchar(20),ColumnB varchar(8000))
insert @tb
select ''ProductCode'','+@s2+'
insert @tb
select b.VATProductCode,'+@s3+'
from FS_VATProductCode a,FS_VATProductCustomerCombination b
where a.VATProductCode=b.VATProductCode
group by b.VATProductCode,a.id
order by a.id
select * from @tb')
go

drop table FS_VATCustomerCode,FS_VATProductCode,FS_VATProductCustomerCombination

/*--测试结果

ColumnA ColumnB
-------------------- -----------------------------------------
ProductCode John Mike Jully
Bike 2 2
Car 500 388
Tractor 149 149
--*/
pbsql 2004-09-21
  • 打赏
  • 举报
回复
搜一下大把的例子
andzen 2004-09-21
  • 打赏
  • 举报
回复
以为是什么技术问题,仅仅是一些小技巧而已,这种问题稍微想想就出来了
einsteincao 2004-09-21
  • 打赏
  • 举报
回复
前面问过同样的问题,只是现在的ReportingService太变态需要修改....

34,593

社区成员

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

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