3

I am using VSQL to extract data from a table in CSV format using this command:

vsql -h [host_address] -d [db_name] -u [user_name] -w [password] -A -F , -t -f script_to_extract_data.sql -o output.csv

However, it outputs column headers, data rows AND an extra row that indicates the total number of rows in the table like this:

Geography,Product,Campaign,VariableName,Outlet,Creative,Period,VariableValue
Geo_00000,Product,BABY,web_sales,Total,Total,2016-10-03,24.06
Geo_00000,Product,BABY,web_sales,Total,Total,2016-10-08,67.17
Geo_00000,Product,BABY,web_sales,Total,Total,2016-10-17,404.67
(3 rows)

If I exclude the -t option, it'll output just the data like this:

Geo_00000,Product,BABY,web_sales,Total,Total,2016-10-03,24.06
Geo_00000,Product,BABY,web_sales,Total,Total,2016-10-08,67.17
Geo_00000,Product,BABY,web_sales,Total,Total,2016-10-17,404.67

I would like the column headers AND the data, but not the total row number like this:

Geography,Product,Campaign,VariableName,Outlet,Creative,Period,VariableValue
Geo_00000,Product,BABY,web_sales,Total,Total,2016-10-03,24.06
Geo_00000,Product,BABY,web_sales,Total,Total,2016-10-08,67.17
Geo_00000,Product,BABY,web_sales,Total,Total,2016-10-17,404.67

From reading through VSQL commandline options, I don't think I have a way to restrict the total number of rows not show?? Anyone who is experienced with using VSQL via commandline could help me out, I would greatly appreciate the help. Thank you!

user1330974
  • 2,500
  • 5
  • 32
  • 60

2 Answers2

2

Without -t add -P footer=off...this will give you headers without the footer like you want.

vsql -h [host_address] -p [port] -d [db_name] -u [user_name] -w [password] -AP footer=off -F , -f script_to_extract_data.sql -o output.csv
Philector
  • 91
  • 8
1

There is no documented way to do this.

You could just inject a select into your script though to print out the header while leaving tuples-only on.

\t
\a
\f ,
\o output.csv
select 'Geography', 'Product', 'Campaign', 'VariableName', 'Outlet', 'Creative', 'Period', 'VariableValue';

select Geography, Product, Campaign, VariableName, Outlet, Creative, Period, VariableValue
from mytable;
\o

And I guess if it really, really bothers you to list the fields twice, you could use a variable.

\set fields Geography,Product,Campaign,VariableName,Outlet,Creative,Period,VariableValue

Then reference :fields in both queries (just use ' around it for the header list). In this case, the header list would just need to be a string, and the delimiter would have to be a , since it would also be used in sql. Just a thought.

woot
  • 7,406
  • 2
  • 36
  • 55
  • Thank you! This hack of adding `SELECT 'Geography, ....` before is a great workaround! :) – user1330974 Dec 05 '16 at 15:13
  • 1
    No problem. You can also just list out the fields in strings which might be better in case you decide to change the delimiter in the future. `select 'Geography','Product' ...` In fact I do like that better so I'll change my answer. – woot Dec 06 '16 at 14:06