需要对表格进行透视或交叉制表,但不是以传统方式进行。请

发布于 2024-12-25 10:21:17 字数 666 浏览 3 评论 0原文

我这里有一个小情况..希望你们能帮助我。

我应该查询一个有 4 列的表
帐户编号、资源编号、产品编号、客户编号
一些 accountNo 有 2 个 ResourceNo(115 和 134),

我必须以这样的方式查询它:我必须显示 resourceNo 值的两个动态列,并在 上放置一个“X” accountNo其中包含那些ResourceNo的..以便AccountNo不会重复..在这种情况下旋转没有帮助。请调查此事并帮助我。

参见

穷人的 SQL 枢轴。

另请参阅

带有动态列的 Sql 透视查询

I have a small situation here.. hope you guys can help me out.

I'm supposed to query a table wich has 4 columns
AccountNo, ResourceNo, ProductNo, CustomerNo.
A few accountNo's have 2 ResourceNo's (115 and 134)

I have to Query it in such a way that I have to show two dynamic columns for the resourceNo values and put an 'X' against the accountNowhich has those ResourceNo's.. So that the AccountNo is not repeated.. Pivoting doesn't help in this situation. Please look into this and help me.

See also

Poor Man's SQL Pivot.

See also

Sql Pivot Query with Dynamic Columns

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2025-01-01 10:21:17

你需要穷人的枢轴:

静态列

例如:

select 
    AccountNo,
    case when sum(case when ResourceNo = 134 then 1 else 0 end) = 0 then '' else 'X' end as Resource_134,
    case when sum(case when ResourceNo = 115 then 1 else 0 end) = 0 then '' else 'X' end as Resource_115
from 
    AccountResource 
group by
    AccountNo

示例数据:

AccountNo ResourceNo ProductNo CustomerNo
A1        134        P1         C1
A1        134        P2         C1
A1        134        P3         C2
A2        134        P1         C1
A2        115        P1         C4
A2        115        P2         C1
A3        115        P5         C2

示例输出:

AccountNo Resource_134 Resource_115
A1        X
A2        X            X
A3                     X

动态列

对于动态列,您可以执行以下操作:

declare @sql nvarchar(max)
set @sql = ''
select @sql = @sql + ',' + char(13)+char(10)+
'case when sum(case when ResourceNo = ''' + replace(cast(ResourceNo as nvarchar(10)), '''', '''''') + ''' then 1 else 0 end) = 0 then '''' else ''X'' end as "Resource_' + replace(cast(ResourceNo as nvarchar(10)), '"', '""') + '"'
from AccountResource 
group by ResourceNo
order by Resourceno

set @sql = 'select AccountNo' + @sql
+ char(13)+char(10)
+ 'From AccountResource '
+ char(13)+char(10)
+ 'group by AccountNo'
+ char(13)+char(10)
+ 'order by AccountNo'
select * from datacheck.dbo.splitmax(@sql, null,null)

exec sp_executesql @sql

You need poor man's pivot:

Static Columns

For example:

select 
    AccountNo,
    case when sum(case when ResourceNo = 134 then 1 else 0 end) = 0 then '' else 'X' end as Resource_134,
    case when sum(case when ResourceNo = 115 then 1 else 0 end) = 0 then '' else 'X' end as Resource_115
from 
    AccountResource 
group by
    AccountNo

Example Data:

AccountNo ResourceNo ProductNo CustomerNo
A1        134        P1         C1
A1        134        P2         C1
A1        134        P3         C2
A2        134        P1         C1
A2        115        P1         C4
A2        115        P2         C1
A3        115        P5         C2

Example output:

AccountNo Resource_134 Resource_115
A1        X
A2        X            X
A3                     X

Dynamic Columns

For dynamic columns you can do this:

declare @sql nvarchar(max)
set @sql = ''
select @sql = @sql + ',' + char(13)+char(10)+
'case when sum(case when ResourceNo = ''' + replace(cast(ResourceNo as nvarchar(10)), '''', '''''') + ''' then 1 else 0 end) = 0 then '''' else ''X'' end as "Resource_' + replace(cast(ResourceNo as nvarchar(10)), '"', '""') + '"'
from AccountResource 
group by ResourceNo
order by Resourceno

set @sql = 'select AccountNo' + @sql
+ char(13)+char(10)
+ 'From AccountResource '
+ char(13)+char(10)
+ 'group by AccountNo'
+ char(13)+char(10)
+ 'order by AccountNo'
select * from datacheck.dbo.splitmax(@sql, null,null)

exec sp_executesql @sql
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文