SQL只是case语句的一部分是打印

时间:2015-01-07 08:59:24

标签: sql shell sqlplus

cd /home/XXXXX
USER_NAME=1234
USER_PWD=1234
DEBUG=0


clear

echo -e "\n Enter The From Date in specified format like DD-MON-YY \n "
read From_Date

echo -e "\n Enter The To Date in specified format like DD-MON-YY \n "
read To_Date


res=`sqlplus -S -L $USER_NAME/$USER_PWD<<EOF | tail -n +2
set pages 0
set linesize 800
set heading off
set feedback off

select distinct CASE WHEN s.s_name in('A','B','AB','C') THEN 'PASSED' ELSE 'FAILED' END as      RESULT,count(*) from student s,Result r where r.REF_NO=s.REF_NO and trunc(r.TIME)>='$From_Date' and trunc(r.TIME)<='$To_Date' and s.response='S' group by rollup((CASE WHEN s.s_name in('A','B','AB','C') THEN 'PASSED' ELSE 'FAILED' END)) order by count(*);

EOF`

echo $res;

此脚本的输出是

FAILED 64

但是OUTPUT想要

PASSED 34
FAILED 64

只有一个案件部分工作,否则只有部分工作

1 个答案:

答案 0 :(得分:0)

如果SELECT语句返回PASSED的行,上面的代码应该会产生预期的结果。

尝试不使用tail -n +2查看所有输出;也许sqlplus会在最后添加另一个空行。

或尝试使用egrep,它允许您说&#34;仅打印以......开头的行&#34;:

res=$(sqlplus -S -L $USER_NAME/$USER_PWD<<EOF | egrep '^(FAILED|PASSED)'
... your SQL here ...
EOF