Export data from db2 with column names - db2

I want to export data from db2 tables to csv format.I also need that first row should be all the column names.
I have little success by using the following comand
EXPORT TO "TEST.csv"
OF DEL
MODIFIED BY NOCHARDEL coldel: ,
SELECT col1,'COL1',x'0A',col2,'COL2',x'0A'
FROM TEST_TABLE;
But with this i get data like
Row1 Value:COL1:
Row1 Value:COL2:
Row2 Value:COL1:
Row2 Value:COL2:
etc.
I also tried the following query
EXPORT TO "TEST.csv"
OF DEL
MODIFIED BY NOCHARDEL
SELECT 'COL1',col1,'COL2',col2
FROM ADMIN_EXPORT;
But this lists column name with each row data when opened with excel.
Is there a way i can get data in the format below
COL1 COL2
value value
value value
when opened in excel.
Thanks

After days of searching I solved this problem that way:
EXPORT TO ...
SELECT 1 as id, 'COL1', 'COL2', 'COL3' FROM sysibm.sysdummy1
UNION ALL
(SELECT 2 as id, COL1, COL2, COL3 FROM myTable)
ORDER BY id
You can't select a constant string in db2 from nothing, so you have to select from sysibm.sysdummy1.
To have the manually added columns in first row you have to add a pseudo-id and sort the UNION result by that id. Otherwise the header can be at the bottom of the resulting file.

Quite old question, but I've encountered recently the a similar one realized this can be achieved much easier in 11.5 release with EXTERNAL TABLE feature, see the answer here:
https://stackoverflow.com/a/57584730/11946299
Example:
$ db2 "create external table '/home/db2v115/staff.csv'
using (delimiter ',' includeheader on) as select * from staff"
DB20000I The SQL command completed successfully.
$ head /home/db2v115/staff.csv | column -t -s ','
ID NAME DEPT JOB YEARS SALARY COMM
10 Sanders 20 Mgr 7 98357.50
20 Pernal 20 Sales 8 78171.25 612.45
30 Marenghi 38 Mgr 5 77506.75
40 O'Brien 38 Sales 6 78006.00 846.55
50 Hanes 15 Mgr 10 80659.80
60 Quigley 38 Sales 66808.30 650.25
70 Rothman 15 Sales 7 76502.83 1152.00
80 James 20 Clerk 43504.60 128.20
90 Koonitz 42 Sales 6 38001.75 1386.70

Insert the column names as the first row in your table.
Use order by to make sure that the row with the column names comes out first.

Related

PostgreSQL how do I COUNT with a condition?

Can someone please assist with a query I am working on for school using a sample database from PostgreSQL tutorial? Here is my query in PostgreSQL that gets me the raw data that I can export to excel and then put in a pivot table to get the needed counts. The goal is to make a query that counts so I don't have to do the manual extraction to excel and subsequent pivot table:
SELECT
i.film_id,
r.rental_id
FROM
rental as r
INNER JOIN inventory as i ON i.inventory_id = r.inventory_id
ORDER BY film_id, rental_id
;
From the database this gives me a list of films (by film_id) showing each time the film was rented (by rental_id). That query works fine if just exporting to excel. Since we don't want to do that manual process what I need is to add into my query how to count how many times a given film (by film_id) was rented. The results should be something like this (just showing the first five here, the query need not do that):
film_id | COUNT of rental_id
1 | 23
2 | 7
3 | 12
4 | 23
5 | 12
Database setup instructions can be found here: LINK
I have tried using COUNTIF and CASE (following other posts here) and I can't get either to work, please help.
Did you try this?:
SELECT
i.film_id,
COUNT(1)
FROM
rental as r
INNER JOIN inventory as i ON i.inventory_id = r.inventory_id
GROUP BY i.film_id
ORDER BY film_id;
If there can be >1 rental_id in your data you may want to use COUNT(DISTINCT r.rental_id)

Is there a SQL select query with following where clause?

Table with Records
ID Column
1 Java 8 Update 131 (64-bit)
2 Java 8 Update 161
3 Java Auto Updater
4 Java SE Development Kit 8 Update 102
5 Java(TM) 6 Update 12
6 shared.tp.aurora.ooc.java.bundle-4.0-core-nu
7 olap.oda.api.java-4.0-en-nu
8 repoaccess.container.java-4.0-core-nu
Select Records having "Java Update" only with ID's 1,2,3,4,5
Tried
Select ID, Column
where column like '%Java%'
Select ID, Column
where column like '%Java Update%'
Select ID, Column
where column like '%Java*Update%'
Using an AND:
SELECT ID, Column
FROM YourTable
WHERE column like '%Java%' AND column like '%Update%'
SELECT ID, Column
FROM YourTable
WHERE column like 'Java%Update%'
Assuming (not a good thing) you want all rows with Column value that starts with 'Java' and contains 'Update' somewhere after 'Java' - the above will help.

Select value from stored procedure which outputs a table

I have a procedure which returns a select statement output after a set of calculation. The output on executing the procedure will be as below:
exec proc1 empID
Output is as below
col1 col2 col3 col4 col5
2014 2 33 330 29
2014 3 10 34 12
2015 1 25 60 55
Now I have a main select statement which gets many columns by joining different tables.
I need to retrieve the above columns (output of stored procedure proc1) to the main select statement, for the empID which is available.
Something like below:
select empID, empName, empSal,
(select col3 from [exec proc1 empID] where col2=1),
empDept
from tblemployee
Is it possible to do? I am expecting 25 in the 4th column of the above query.
You can use either User-defined function or a view instead of a stored procedure.
sp doesn't allow select to use in it.
or you can
create a table variable to store the result returned from the stored procedure
Declare #TempTable Table (...)--declare all columns
insert the output of the stored proc into the table variable and then
Insert #TempTable Exec storedProcname params
Join the Temp table variable exactly as per your need
Note
But the above method has a limitation
The problem with INSERT #Temptable is that an
INSERT EXEC statement cannot be nested
. it will break,if your stored procedure already has an INSERT EXEC in it.

How to divide percentage distribution of rows in two different tables in sql server?

Hi i am using SQL Server 2012, i have a situation where have to divide table records in percent distribution let says i have 199 records in a table and i have to split this in two different tables 1 table having 43% of records and another table have remaining like 57% rows. Somebody please help me in these.
select top 43 percent from table order by field asc
select top 57 percent from table order by field desc
or
SELECT top 43 percent *
INTO newtable43
FROM table
SELECT col1, col2, coln
INTO newtable57
FROM table
except
select col1, col2, coln from newTable43

distinct key word only for one column

I'm using postgresql as my database, I'm stuck with getting desired results with a query,
what I have in my table is something like following,
nid date_start date_end
1 20 25
1 20 25
2 23 26
2 23 26
what I want is following
nid date_start date_end
1 20 25
2 23 26
for that I used SELECT DISTINCT nid,date_start,date_end from table_1 but this result duplicate entries, how can I get distinct nid s with corresponding date_start and date_end?
can anyone help me with this?
Thanks a lot!
Based on your sample data and sample output, your query should work fine. I'll assume your sample input/output is not accurate.
If you want to get distinct values of a certain column, along with values from other corresponding columns, then you need to determine WHICH value from the corresponding columns to display (your question and query would otherwise not make sense). For this you need to use aggregates and group by. For example:
SELECT
nid,
MAX(date_start),
MAX(date_end)
FROM
table_1
GROUP BY
nid
That query should work unless you are selecting more columns.
Or maybe you are getting the same nid with a different start and/or end date
Try distinct on:
select distinct on (col1) col1, col2 from table;
DISTINCT can't result in duplicate entries - that's what it does... removed duplicates.
Is your posted data is incorrect? Exactly what are your data and output?