Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在SQL中命名行_Mysql_Sql_Count_Case_Union - Fatal编程技术网

Mysql 如何在SQL中命名行

Mysql 如何在SQL中命名行,mysql,sql,count,case,union,Mysql,Sql,Count,Case,Union,我有以下疑问: (SELECT count(ID)/2 FROM people where Date between '2000' and '2001') union (SELECT count(ID)/3 FROM people where Date between '2002' and '2004') union (SELECT count(ID)/6 FROM people where Date between '2005' and '2010') 数字是正确的。现在我想给这些行起

我有以下疑问:

(SELECT count(ID)/2 FROM people  where Date between '2000' and '2001') 
union
(SELECT count(ID)/3 FROM people where Date between '2002' and '2004') 
union
(SELECT count(ID)/6 FROM people where Date between '2005' and '2010')
数字是正确的。现在我想给这些行起个名字,看看它们是指哪些人。 到目前为止,我只知道如何给栏目命名


编辑:加上如何命名新名称所在的列?

只需在子查询中添加一列即可:

select dt_2000_2001 as dt_range, count(id) cnt from people  where date between 2000 and 2001
union all
select dt_2002_2004, count(id) / 2 from people where date between 2002 and 2004 
union all
select dt_2005_2010, count(id) / 5 from people where date between 2005 and 2010
我不相信你真的需要这里的
union
。如果每年都有行,可以使用
case
表达式来构建组,并使用
count(distinct)
来计算Denumerator,如下所示:

select
    case when date between 2000 and 2001 then dt_2000_2001
         when date between 2002 and 2004 then dt_2002_2004
         when date between 2005 and 2010 then dt_2005_2010
    end as dt_range,
    count(*) / count(distinct date) cnt
from people
group by dt_range
我建议:

select (case when date between 2000 and 2001 then '2000-2001'
             when date between 2002 and 2004 then '2002-2004'
             when date between 2005 and 2010 then '2005-2010'
        end) as dt_range,
       (count(*) * 1.0 / 
        (case when date between 2000 and 2001 then 1
              when date between 2002 and 2004 then 2
              when date between 2005 and 2010 then 5
         end) 
       ) as value
from people
group by dt_range;
我对你选择分母有点惊讶。这比价格低一倍。我希望该值是完整的范围

假设您拥有所有年份的数据,您可以将其表示为:

select (case when date between 2000 and 2001 then '2000-2001'
             when date between 2002 and 2004 then '2002-2004'
             when date between 2005 and 2010 then '2005-2010'
        end) as dt_range,
       (count(*) * 1.0 / 
        (max(date) - min(date) + 1)
       ) as value
from people
group by dt_range;

如果date列只包含没有完整时间戳的日期,您确定第二个查询计数正确吗?@AsifMehmood:从OP显示的原始查询来看,
date
实际上是一个整数值,而不是
date
datetime
-否则,
where
条件无论如何都没有意义。关于日期列数据类型,您是对的。谢谢我还没有弄清楚原因,但是
案例语句
没有显示正确的答案results@SAPNONEXPERT:如果确实有时间戳,则应将所有出现的
date
替换为
year(date)
。第一个将
周围的错误1064作为值
,第二个可用,但只给出一行。你知道ifrst有什么问题吗?我用分母得到了你的观点,并对其进行了调整。
as value
前面的最后一个括号太多了。它可以工作并给出正确的结果。谢谢