in reply to sql output format

satya128:

While you could write a perl script to do that, why not get your SQL tool to do it for you? That way you could use as few programs as possible.

First, you need to glue your queries together. I'm assuming they're currently something like this:

select XMOU from Table1 where ...stuff...; select YBLLD_MOU from Table2 where ...morestuff...; select ZCNT_CALL from Table3 where ...otherstuffr...;

You can glue these queries together even though they have different selection criteria and use different tables. In addition, you can tell it to output the field name beside the value like this:

select 'XMOU' as the_name, XMOU as the_value from Table1 where ...stuff... union all select 'YBLLD_MOU', YBLLD_MOU from Table2 where ...morestuff... union all select 'ZCNT_CALL', ZCNT_CALL from Table3 where ...otherstuffr...

The union word tells SQL that you want the results of the query below it to be appended to the same result set as the query before it. Two things of note: First, both queries need to return the same number of columns, and those columns need to have the same types in the same order. Second, you can name the columns of the output results with the 'as' word, as I did in the first query.

Once you've done that, then take a look at the manual for your SQL tool. Usually, there's an option to suppress column headers and row counts. If you set those, then you should be able to get the results you want without even involving perl.

Perl isn't always the answer--if you learn all your tools well, you can save yourself a lot of headaches. Of course, this all supposes that you're not wanting to do some additional processing in perl that you haven't mentioned. In that case, I'd either:

Update: Added 'ALL' per chacham's note.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: sql output format
by chacham (Prior) on Oct 20, 2014 at 16:08 UTC

    A simple comment on your excellent post. As a general rule, it is better to use UNION ALL. UNION applies a DISTINCT which forces a(n expensive) sort operation.

      chacham:

      Thanks for that. I wasn't aware of the difference between UNION and UNION ALL. Hopefully, I'll remember it next time I need it.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        lmgtfy, for

        PostgreSQL, Oracle, and SQLite.

        As chacham said, UNION ALL leaves the rowsets from all query-parts intact; UNION is slightly more expensive, as it makes the final rows DISTINCT (i.e., removes duplicates).

        With regard to these compound query operators, note that Oracle stubbornly uses MINUS instead of the Standard-prescribed EXCEPT. Ah well, I guess it's too late to teach them manners ;-)