如何在SQL Server 2008中复制列数据?

波尼玛

这是我的查询:

select 
    t.range as [score range], count(*) as [number of occurences]
from 
    (select 
         case 
            when answer_count between 0 and 5 then ' 0- 9'
            when answer_count between 5 and 10 then '10-19'
            else '20-99' end as range
     from 
         points 
     where 
         type = 'product_quiz') t 
group by 
    t.range

输出为:

/-------------------------------------\
| score range | number of occurrences |
|-------------+-----------------------|
|   10-19     |         121327        |
|    0- 9     |         129195        |
\-------------------------------------/

但是我想要这样的输出:

/-----------------------------------------------------------------------------\
| score_range | number_of_occurrences | score_range1 | number_of_occurrences1 |
|-------------+-----------------------+--------------+------------------------|
|   10-19     |         121327        |    10-19     |         121327         |
|    0- 9     |         129195        |     0- 9     |         129195         |
\-----------------------------------------------------------------------------/

如何使用子查询来实现?

3N1GM4

如果我对您的理解正确,则不需要子查询(除非您引用的是子句中已存在的子查询FROM):

select 
    t.range as [score_range], 
    count(*) as [number_of_occurences],
    t.range as [score_range1], 
    count(*) as [number_of_occurences1]
from 
    (select 
         case 
            when answer_count between 0 and 5 then ' 0- 9'
            when answer_count between 5 and 10 then '10-19'
            else '20-99' end as range
     from 
         points 
     where 
         type = 'product_quiz') t 
group by 
    t.range

为什么要执行此操作(以及为什么特别要使用子查询)是另一个问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在SQL Server 2008 R2中生成没有复制数据的数据库脚本

如何在SQL Server 2008中导入数据

如何在SQL Server 2008中遍历某些XML数据?

如何在SQL Server 2008中读取XML列?

如何将所有数据从Sql Server 2008中的表复制到文件

如何在 SQL Server 2008 R2 复制的架构中包含默认值设置

在SQL Server 2008的同一数据库中复制表,然后更新内容

如何在SQL Server中更改列的数据类型?

如何在SQL Server 2008中的特定列中存储列值?

如何在SQL Server中透视列

如何在SQL Server中透视列?

SQL Server 2008:如何在列中给出字段的值并返回列标识

如何在SQL Server 2008中从Avg中查找Max

如何在SQL Server 2008中比较不同行和不同列中的值

如何在SQL Server 2008中将数据转换为json格式?

如何在SQL Server 2008 R2数据库中存储处方(诊所项目)

如何在SQL Server 2008中解析xml

如何在SQL Server 2008中计算累积积?

如何在SQL Server 2008中动态使用SOUNDEX

如何在SQL Server 2008中实现所需的输出?

如何在SQL Server 2008中搜索特殊字符(%)

如何在SQL Server 2008中编写此查询

如何在SQL Server 2008查询中显示订单履行

如何在MS SQL Server 2008中隐藏消息窗口?

如何在SQL Server 2008中获取时间总和字段

如何在Sql Server 2008中插入日期?

如何在SQL Server 2008中获取特定值的列名

如何在SQL Server 2008中编写此查询?

如何在SQL SERVER 2008中使用滞后功能