Null return from a vertica query

Moderator: NorbertKrupa

Post Reply
pk132007
Newbie
Newbie
Posts: 7
Joined: Fri Sep 20, 2013 9:05 am

Null return from a vertica query

Post by pk132007 » Thu Oct 10, 2013 10:00 am

I am trying to run a vertica sql query
Select SUM(column1) from table;

If column1 has null value for all the records, then the query will return a single character.
I have to capture it in a varaible in unix so that i can compare it.

var=$(/opt/vertica/bin/vsql -c "Select SUM(column1) from table;")

If [[ $var = " " ]] ; then
echo "print"
fi

i am not able to get the "print" output.
Can anyone please help.

PS: In db2 the output of above query is "-" so we can easily compare.

User avatar
nnani
Master
Master
Posts: 302
Joined: Fri Apr 13, 2012 6:28 am
Contact:

Re: Null return from a vertica query

Post by nnani » Thu Oct 10, 2013 1:24 pm

#pk132007

I could not get your requirement. I am projection a solution according to my understanding

Scenario : If column1 has null value for all the records, then the query will return a single character.

Code: Select all

nnani=> \d+ sum_test
                                         List of Fields by Tables
       Schema       |  Table   |  Column   | Type | Size | Default | Not Null | Primary Key | Foreign Key
--------------------+----------+-----------+------+------+---------+----------+-------------+-------------
public| sum_test | digit_col | int  |    8 |         | f        | f           |
(1 row)



nnani=> select sum(digit_col) from sum_test;
 sum
-----
  11
(1 row)

Script getting the sum

Code: Select all

#!/bin/bash

add_var=`vsql -At -c " select sum(digit_col) from sum_test;"`

if [ -z  "$add_var" ];
then
add_var="-"
echo "$add_var"
echo "print"
else
echo "$add_var"
echo "records found"
fi
Let me if, this is what you were looking for.
Hope this helps.. :)
nnani........
Long way to go

You can check out my blogs at vertica-howto

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: Null return from a vertica query

Post by JimKnicely » Thu Oct 10, 2013 1:27 pm

Perhaps this is another option? :lol:

Code: Select all

#!/bin/bash

var=$(/opt/vertica/bin/vsql -At -c "SELECT NVL(SUM(column1)::VARCHAR, '-') FROM table;")

if [ "$var" == "-" ]; then
  echo "print";
fi
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: Null return from a vertica query

Post by JimKnicely » Fri Oct 11, 2013 12:00 pm

Yet another example:

Code: Select all

daniel@synapse:~$ var=`/opt/vertica/bin/vsql -Atqnc "select sum(null::int)"`
daniel@synapse:~$ if [[ -z ${var} ]]; then echo 'EMPTY'; else echo ${var}; fi
EMPTY
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

User avatar
nnani
Master
Master
Posts: 302
Joined: Fri Apr 13, 2012 6:28 am
Contact:

Re: Null return from a vertica query

Post by nnani » Fri Oct 11, 2013 12:48 pm

Another Possibility :D

Code: Select all

nnani=> select sum(digit_col) from sum_test;
 sum
-----
  11
(1 row)

nnani=> select CASE when sum(digit_col) < 1 then '-' ELSE 'EMPTY' END from sum_test;
 case
-------
 EMPTY
(1 row)

You can use this in script like this

Code: Select all


#!/bin/bash

add_var=`vsql -At -c " select CASE when sum(digit_col) < 1 then '-' ELSE 'EMPTY' END from sum_test;"`
echo "$add_var"


Hope this helps.. :)
nnani........
Long way to go

You can check out my blogs at vertica-howto

Post Reply

Return to “Vertica SQL”