【问题标题】:pivoting the results of unioned temporary tables旋转联合临时表的结果
【发布时间】:2017-01-22 00:14:39
【问题描述】:

我有 6 个联合选择语句,其中 3 个显示美国、印度和英国的汽车总销量,然后将它们放在一个临时表中。其他 3 条语句显示美国、印度和英国的船舶总销量,也请在临时表中。

然后通过另一个 select 语句访问这些​​临时表

每个国家/地区的信息按行显示,汽车和船只作为列。

我知道如何旋转单个 select 语句。我的问题是这个pivot的结果可以吗,如果可以,我应该在哪里包含pivot操作符

【问题讨论】:

  • 我用“sql”替换了不兼容的数据库标签,但是如果没有样本数据和期望的结果,你的问题就没有任何意义。
  • 我没有代码,但我可以提供类似场景的东西。谢谢
  • 嗨,Linoff 先生,请看下面的代码
  • 我正在尝试输入代码,但它说它太长了。我可以发邮件给你吗?

标签: sql pivot-table


【解决方案1】:

将 6 个联合选择放入 CTE 并进行旋转。您还需要为枢轴提供数据(例如金额或日期),因此我们可以为您构建枢轴结构

【讨论】:

  • 它提示代码太长,我可以通过电子邮件发送给您吗?
  • 如果你知道如何在sql中使用pivot,那么在CTE内部单select语句和6个union select没有区别。我想你可以自己处理
  • 我尝试失败。
  • with pivotdata as (select categoryname, s.shipcountry, sum(od.unitprice * od.qty) as totalseafoodsales from sales.orders as s inner join sales.orderdetails as od on s.orderid = od.orderid inner join production.products as p on p.productid = od.productid inner join production.categories as pc on pc.categoryid = p.categoryid where s.shipcountry = 'France' and pc.categoryname = 'seafood' group by categoryname, s.shipcountry ) select totalseafoodsales, [Brazil], [USA], [France] from pivotdata pivot(sum( totalseafoodsales) for shipcountry in ([Brazil], [USA], [France])) as p;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多