Add Heading to a group of columns in Oracle SQL - oracle-sqldeveloper

Oracle 11g. Oracle Apex 5.1
I need to Merge the columns without merging the data and Add a column heading in Oracle Apex Interactive Report.
For Example
I have a table like this:
I want the table output like this:
How can I achieve the output in Report Select Statement?
If I am using below query in Oracle Apex Interactive Report:
TITLE LEFT ' amount_column Quantity_column'
SELECT Date, Amount1, Amount2, Amount3, Quantity1, Quantity2
FROM table_name;
I am getting error as: ORA-20001: Query must begin with SELECT or WITH.

In oracle Apex 5.1 we can create such groups in Interactive Grid(IG).
To create a group, steps are:
Go to Attribute of IG -> create Group -> add a name to group.
To assign a group on column, steps are:
go to particular column(s) name -> under layout property -> select group name
Save and run the page and it will work.

If you are running the query to get a SQL/Plus style textual output (using Crtl-F5 / run-as-script within SQL Developer) then you can use the commands for formatting SQL*Plus reports such as COLUMN and TTITLE to make it appear like your desired result:
Something like (untested):
COLUMN "Date" FORMAT A9
COLUMN Amount1 FORMAT 9999.99
COLUMN Amount2 FORMAT 9999.99
COLUMN Amount3 FORMAT 9999.99
COLUMN Quantity1 FORMAT 999999999
COLUMN Quantity2 FORMAT 999999999
TTITLE LEFT ' Amount column Quantity Column'
SELECT "Date", Amount1, Amount2, Amount3, Quantity1, Quantity2
FROM table_name;
If you want to do it in the grid (using F5 to run the query within SQL Developer) then you are out of luck and it is not possible.

Related

How to save a query result into a column using postgresql

This may be a simple fix but Im somehow drawing a blank. I have this code below, and I want the results that I got from it to be added into their own column in an existing table. How would i go about doing this.
Select full_name, SUM(total) as sum_sales
FROM loyalty
where invoiceyear = 2013
GROUP BY full_name
order by sum_sales DESC;
This leaves me with one column with the name of employee and the second with their sales from that year.
How can i just take these results and add them into a column in addition to the table
Is it as simple as...
Alter table loyalty
Add column "2013 sales"
and then add in some sort of condition?
Any help would be greatly appreciated!
If i got your question right, you need to first alter the table allowing the new field to be null (you can change it later on) then you could use an insert clause to store the value permanently.

postgres crosstab with unknown number of columns

I have a table test with looks like the following
That should be transformed to the following table
I can achieve that with the following crosstab statement
SELECT *
FROM crosstab( 'select category, month, sum from test) AS ct(category text, r202208 float, r202209 float);
However this only works when I know the columns beforehand, but the table contains how much money I spend in each category and each month, so I don't know the months upfront. Is it possible to autogenerate the the columns based on the content in the month column of the original table, maybe by using a postgres function?
Of course I could generate the sql query dynamically as string and execute it with java, node or something else. However I get the data as csv from my financial institute and the original table is just a view, so I would like to create that table without the help of external programming

DB2 : Need to get the list of columns and distinct value counts for a given db2 table

For data profiling purpose , I just need to get the idea if a columns in a given table has values populated or not. For that, I need to get the list of columns and distinct value counts for a given db2 table.
If you are using Db2 for Linux, Unix or Windows you could try
SELECT TABSCHEMA, TABNAME, COLNAME, COLCARD FROM SYSCAT.COLUMNS

How to return a comma separated string using Crystal SQL Expression

I want to display a string on each row (Details section) in my Crystal Report. The contents of this string will be retrieved with the help of a SQL Expression.
The SQL I have is follows: However if multiple rows are returned, I am not sure how to convert that into a Comma Separated String. I have an Oracle 11g database.
(select distinct NAME from TEST
where SAMPLE_NUMBER = "TEST"."SAMPLE_NUMBER"
and X_BENCH <> '"TEST"."X_BENCH"')
The TEST Table looks like this:
My report will be filtered for all samples with a specific test (e.g. Calcium). For those samples on the report, My SQL Expression should retrieve all "Other" Tests on the sample. See output example.
You can accomplish this with a wm_concat. WM_CONCAT takes a bunch of rows in a group and outputs a comma separated varchar.
Using the substr function you can separate the first result with the last.
Please note that I am dirty coding this (without a compiler to check my syntax) so things may not be 100% correct.
select sample_number
, substr(wm_concat(name),1,instr(wm_concat(name),",")-1) as NAME
, substr(wm_concat(name),instr(wm_concat(name),","),length(wm_concat(name)-instr(wm_concat(name),",")+1) as OTHER_TEST_NAMES
from TEST
where SAMPLE_NUMBER = "TEST"."SAMPLE_NUMBER"
and X_BENCH <> '"TEST"."X_BENCH"'
and rownum < 2
group by sample_number
However, if it is not necessary to separate the name and the other test names, it actually is much simpler.
select sample_number
, wm_concat(name) as NAMES
from TEST
where SAMPLE_NUMBER = "TEST"."SAMPLE_NUMBER"
and X_BENCH <> '"TEST"."X_BENCH"'
and rownum < 2
group by sample_number
Also please try to organize your lines to make it easier to read.
You can use LISTAGG for Converting Rows to Comma-Separated String in Oracle.
Example:
SELECT user_id
, LISTAGG(expertise, ',')
WITHIN GROUP (ORDER BY expertise)
AS expertise
FROM TEMP_TABLE
GROUP BY user_id;

Copy selected query fields name in Mysql Workbench

I am using mysql workbench (SQL Editor). I need copy the list of columns in each query as was existed in Mysql Query Browser.
For example
Select * From tb
I want have the list of fields like as:
id,title,keyno,......
You mean you want to be able to get one or more columns for a specified table?
1st way
Do SHOW COLUMNS FROM your_table_name and from there on depending on what you want have some basic filtering added by specifying you want only columns that data type is int, default value is null etc e.g. SHOW COLUMNS FROM your_table_name WHERE type='mediumint(8)' ANDnull='yes'
2nd way
This way is a bit more flexible and powerful as you can combine many tables and other properties kept in MySQL's INFORMATION_SCHEMA internal db that has records of all db columns, tables etc. Using the query below as it is and setting TABLE_NAME to the table you want to find the columns for
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='your_table_name';
To limit the number of matched columns down to a specific database add AND TABLE_SCHEMA='your_db_name' at the end of the query
Also, to have the column names appear not in multiple rows but in a single row as a comma separated list you can use GROUP_CONCAT(COLUMN_NAME,',') instead of only COLUMN_NAME
To select all columns in select statement, please go to SCHEMAS menu and right click ok table which you want to select column names, then select "Copy to Clipboard > Select All statement".
The solution accepted is fine, but it is limited to field names in tables. To handle arbitrary queries would be to standardize your select clause to be able to use regex to strip out only the column aliases. I format my select clause as "1 row per element" so
Select 1 + 1 as Col1, 1 + 2 Col2 From Table
becomes
Select 1 + 1 as Col1
, 1 + 2 Col2
From Table
Then I use simple regex on the "1 row per select element" version to replace "^.* " (excluding quotes) with nothing. The regex finds everything before the final space in the line, so it assumes your column aliases doesn't contain spaces (so replace spaces with underscore). Or if you don't like "1 row per element" then always use "as" keyword to give you a handle that regex can grasp.