数据查询 行转列 问题求助

daview 2007-02-08 09:04:58
下面是我同事弄的一个脚本,不知道他从哪里弄的(反正他在这方面比我还垃圾,哈哈,而且他已经离职了),这个脚本目的是把行转为列,中间的“else”部分代码是我后来添加的,无论前后,该代码只有第一个数据成功转换了,其余的数据都没有转换。

Select ([Year]),
max(case Number when 1 then (SmallImageUrl) else ('defaultMag.jpg') end) as SmallImageUrl1,
max(case Number when 2 then (SmallImageUrl) else ('defaultMag.jpg') end) as SmallImageUrl2,
max(case Number when 3 then (SmallImageUrl) else ('defaultMag.jpg') end) as SmallImageUrl3,
max(case Number when 4 then (SmallImageUrl) else ('defaultMag.jpg') end) as SmallImageUrl4,
max(case Number when 1 then ('?ISBN=' + ISBN) else ('#') end) as ISBN1,
max(case Number when 2 then ('?ISBN=' + ISBN) else ('#') end) as ISBN2,
max(case Number when 3 then ('?ISBN=' + ISBN) else ('#') end) as ISBN3,
max(case Number when 4 then ('?ISBN=' + ISBN) else ('#') end) as ISBN4,
max(case Number when 1 then (IssueName) else ('(无杂志)') end) as IssueName1,
max(case Number when 2 then (IssueName) else ('(无杂志)') end) as IssueName2,
max(case Number when 3 then (IssueName) else ('(无杂志)') end) as IssueName3,
max(case Number when 4 then (IssueName) else ('(无杂志)') end) as IssueName4
From
MagazineIssue
Where OwnerEMNumber = @ISBN
group by [Year]
...全文
345 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jacobsan 2007-02-08
  • 打赏
  • 举报
回复
倒分?
dawugui 2007-02-08
  • 打赏
  • 举报
回复
呵呵,不客气.
daview 2007-02-08
  • 打赏
  • 举报
回复
谢谢各位哈。

非常感谢 dawugui !!!

是你的需求提醒了我,我试着创建一个表,提供模拟数据给你帮忙的,结果没有问题,仔细分析,原来是原来的数据表中NUMBER是一样的。(呵呵,这个数据的YEAR本身是我手工修改的,而且数据量不大)。

非常感谢。
dawugui 2007-02-08
  • 打赏
  • 举报
回复
我建议你给出表结构,数据和想要的结果.
daview 2007-02-08
  • 打赏
  • 举报
回复
问题没有解决。

我的数据中有IMAGEURL(varchar)、ISSUENAME(varchar)、NUMBER(int,值1-4)、ISBN(varchar)、YEAR(char)等字段,希望通过按YEAR进行统计,根据NUMBER的不同生成对应的IMAGEURL、ISSUENAME、ISBN字段。

我的代码本身没有问题,能正确得到NUMBER为最大的结果,但运行错误。

根据我的分析,问题应该在MAX这个聚合函数上,该函数取最大的,第一次取了最大的后,后面的不再运行了,把MAX更换为MIN后,得到另外一个结果。

问题:

是否有其他的聚合函数,支持以VARCHAR为参数的?或者自己如何编写一个聚合函数?
wxh163000 2007-02-08
  • 打赏
  • 举报
回复
HRHR
jacobsan 2007-02-08
  • 打赏
  • 举报
回复
是不是因为你的year字段都相同?改为group by [Year],[Month],类似这样试试
hhhdyj 2007-02-08
  • 打赏
  • 举报
回复
这样改试试看,不过要确保,SmallImageUrl和IssueName字段的第一个字符不为空格。
Select ([Year]),
ltrim(max(case Number when 1 then (SmallImageUrl) else (' defaultMag.jpg') end)) as SmallImageUrl1,
ltrim(max(case Number when 2 then (SmallImageUrl) else (' defaultMag.jpg') end)) as SmallImageUrl2,
ltrim(max(case Number when 3 then (SmallImageUrl) else (' defaultMag.jpg') end)) as SmallImageUrl3,
ltrim(max(case Number when 4 then (SmallImageUrl) else (' defaultMag.jpg') end)) as SmallImageUrl4,
ltrim(max(case Number when 1 then ('?ISBN=' + ISBN) else (' #') end)) as ISBN1,
ltrim(max(case Number when 2 then ('?ISBN=' + ISBN) else (' #') end)) as ISBN2,
ltrim(max(case Number when 3 then ('?ISBN=' + ISBN) else (' #') end)) as ISBN3,
ltrim(max(case Number when 4 then ('?ISBN=' + ISBN) else (' #') end)) as ISBN4,
ltrim(max(case Number when 1 then (IssueName) else (' (无杂志)') end)) as IssueName1,
ltrim(max(case Number when 2 then (IssueName) else (' (无杂志)') end)) as IssueName2,
ltrim(max(case Number when 3 then (IssueName) else (' (无杂志)') end)) as IssueName3,
ltrim(max(case Number when 4 then (IssueName) else (' (无杂志)') end)) as IssueName4
From
MagazineIssue
Where OwnerEMNumber = @ISBN
group by [Year]
云中客 2007-02-08
  • 打赏
  • 举报
回复
是哪个结果显示出来的
w75251455 2007-02-08
  • 打赏
  • 举报
回复
8嘎
dawugui 2007-02-08
  • 打赏
  • 举报
回复
确实错了.为何有3次等于1,2,3,4?

34,838

社区成员

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

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