Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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
基本Teradata SQL添加列和求和列_Sql_Teradata - Fatal编程技术网

基本Teradata SQL添加列和求和列

基本Teradata SQL添加列和求和列,sql,teradata,Sql,Teradata,SQL不太好,如果我的问题看起来很愚蠢,那么很抱歉。我有一个工作代码,用于提取进入日期和在该日期进入商店1的人数 select entry_date as Enter_Date ,count(entry_date) as Entries from db_entry where entry_date between '2017-03-05' and '2017-03-11' and entry_code like 'STR1%' group by entry

SQL不太好,如果我的问题看起来很愚蠢,那么很抱歉。我有一个工作代码,用于提取进入日期和在该日期进入商店1的人数

select entry_date as Enter_Date
    ,count(entry_date) as Entries
    from db_entry
    where entry_date between '2017-03-05' and '2017-03-11'
    and entry_code like 'STR1%'
    group by entry_date
这就是它所表现出来的

Enter_Date  Entries
3/5/2017    35
3/9/2017    30
3/10/2017   27
3/8/2017    23
3/7/2017    29
3/6/2017    32
3/11/2017   39
我想知道是否有办法为Store2添加另一列,其中条目代码为“STR2%”。我不确定该做什么的原因是因为我没有从db_条目中提取不同的列,所以我不确定如何区分WHERE子句中的两列

此外,我想知道是否有一种快速的方法可以对每一列求和,并将最新日期作为输入日期。理想情况下,我希望我的桌子是这样的:

 Enter_Date  Store 1     Store 2 
 3/11/2017   215         301

使用
case
表达式进行条件计数

select entry_date as Enter_Date,
       count(case when entry_code like 'STR1%' then entry_date end) as Entries1,
       count(case when entry_code like 'STR2%' then entry_date end) as Entries2
from db_entry
where entry_date between '2017-03-05' and '2017-03-11'
  and entry_code like any ('STR1%', 'STR2%')
group by entry_date
注意:
WHERE
子句的
like
str1/str2现在实际上并不需要,但可能会加快查询速度


编辑:如@dududu Markovitz建议的那样,现在像任何代码一样使用

使用
case
表达式进行条件计数

select entry_date as Enter_Date,
       count(case when entry_code like 'STR1%' then entry_date end) as Entries1,
       count(case when entry_code like 'STR2%' then entry_date end) as Entries2
from db_entry
where entry_date between '2017-03-05' and '2017-03-11'
  and entry_code like any ('STR1%', 'STR2%')
group by entry_date
注意:
WHERE
子句的
like
str1/str2现在实际上并不需要,但可能会加快查询速度


编辑:如@dududu Markovitz建议的那样,现在像任何代码一样使用

要回答第二个问题,只需删除
分组方式
,然后切换到:

select MAX(entry_date) as Enter_Date,
       count(case when entry_code like 'STR1%' then entry_date end) as "Store 1",
       count(case when entry_code like 'STR2%' then entry_date end) as "Store 2"
from db_entry
where entry_date between date '2017-03-05' and date '2017-03-11'
  and entry_code like any ('STR1%', 'STR2%')

要回答第二个问题,只需删除
分组方式
,然后切换到:

select MAX(entry_date) as Enter_Date,
       count(case when entry_code like 'STR1%' then entry_date end) as "Store 1",
       count(case when entry_code like 'STR2%' then entry_date end) as "Store 2"
from db_entry
where entry_date between date '2017-03-05' and date '2017-03-11'
  and entry_code like any ('STR1%', 'STR2%')

介于日期“2017-03-05”和日期“2017-03-11”之间
条目(类似于任何代码('STR1%,'STR2%)
(Teradata支持的语法)@dududumarkovitz,感谢您的改进建议!只是一些稀奇的东西,在日期“2017-03-05”和日期“2017-03-11”之间是否也有类似的
条目(STR1%,'STR2%)
(Teradata支持的语法)@dududumarkovitz,谢谢你的改进建议!仅仅是古玩,是否也有类似的