Convert Oracle SQL to OBIEE Evaluate/TO_CHAR Function - sql

Im trying to convert Oracle SQL Developer Select Statement into an Oracle BI function.
Seems like Evaluate is the way to go but the syntax is a little confusing so any help is appreciated. The SQL Dev Code is below for 2 select Statements:
TRIM(TO_CHAR(COUNT(*), '999,999,999,999,999')) AS Row_Count
TRIM(TO_CHAR(SUM(N_MODEL_ISS_AGE), '999,999,999,999,999')) AS Age

Those bits of SQL are there for formatting the resulting number into a more "nice to read" format. OBIEE is an Analytical platform and formatting has nothing to do with querying data. You build your analysis, select the attributes and measures you want and on each column you will be able to set the format you want for the values. Formatting is separated from retrieving data as formatting is only something which matter for rendering.
On each column in the criteria tab of the analysis you have "Column Properties" > "Data Format". You either pick a predefined format or set to 'custom' and enter your own format mask.

Related

Use Redshift query result as to_char format

Inspired by the slected answer to Declare a variable in RedShift I am trying to use a query result as the format value in a to_char function call:
WITH tmp_variables as (
select 'YYYY-MM-DD' as date_format
)
SELECT to_char(OrderDate, (SELECT date_format FROM tmp_variables)) FROM Orders
But I am getting an error
TO_CHAR parameter: Second input must be a string literal
How can the tmp_variables's date_format value be used as a to_char format without getting an error or is there an alternative to using to_char where this would work?
SELECT is a SQL operator that work upon data. SQL is compiled before it can operate on data. The basic answer is that this won't work as written.
What you are trying to achieve isn't clear in the question - change date output format for some reason for some set of queries but not others? In the general case you will need to modify the SQL that goes to the compiler which will mean reading some configuration and merging this into the SQL text. If the use case is more limited there may be another way to achieve the desired result but only within some set of limitations.
Some possibilities - You could set a SQL variable with the format literal. Your client can read info and modify the query itself if it is capabile. A stored procedure could be used. A SQL modifier (pg_bouncer?) could live between the client and the cluster and substitute the string based on some other factors. Each of these has limitations and costs.
If you can describe the use case it could generate different / better ways.

How to read the value from EXCEL/CSV and pass into SQL query and the SQL query output also need to print in EXCEL/CSV

Input Excel/CSV:
SQL Query: -
select 'WWW' as COLUMN_NAME,
(case when to_char (max(WWW)) = to_char(min(WWW)) and to_char(count(WWW)) = count(*) or to_char(max(WWW)) is null then 'same' else 'diff'end)as COMPARISION_VALUE, to_char(max(WWW))as TRANSACTION1, to_char(min(WWW))as TRANSACTION2
from ABC
where Book ='123' and UPDATE_DATE='01-JUN-18';
Note: - I am looking to put this SQL query in loop where first row will pass into the SQL query, then it will check the 2nd row, if the cell is blank it will consider the top most value and COLUMN_NAME will iterate as much we have specified. All the above 4 columns should be parameter.
***Output Console: -***
COLUMN_NAME COMPARISION_VALUE TRANSACTION1 TRANSACTION2
WWW same test test
Expected Output: - I want to save all the transaction in excel/CSV one by one
Please find the attached doc for complete Details
Your question concatenates Excel/CSV as through they're the same thing. They are not. Excel is a very different thing from CSV. Consequently they are two different requirements.
Excel is a binary format which makes it hard to integrate with SQL (although there are third party libraries which can help). CSV is open and text-based, so it's a lot simpler. With an external table you can query a CSV using SQL. Find out more.
As for output, you've tagged your question [plsqldeveloper]. That is the tag for the Allround Automations IDE, which already has excellent capabilities for exporting to Excel or CSV. If you are using that tool you should not reinvent its features. Oracle SQL Developer also already does this (and I suspect TOAD and most of the other IDEs out there do too).

right to left string in SQL server

Consider below query containing both Persian(a right to left language) and English(a left to right language):
SELECT 'نرم افزار SQL سرور'
the required result is this string :
سرور SQL نرم افزار
Is there any function or any other way to converting string from ltr to rtl??
It is required to add N before string literal: SELECT N'نرم افزار SQL سرور'. This is needed to correctly interpret contained Unicode characters. (Source)
Important: In some cases, please avoid using standard copy-paste in order to put SELECT into SSMS command window. This could affect the RTL/LTR order. Instead, try to open correctly composed file using File > Open.
And regarding your comment:
the result should be : سرور SQL نرم افزار`
I admit I understand RTL writing system only partially, but from what I can see, Persian words are put to the output exactly in order as you entered them (even if reading right to left). Could you show me based on Unicode Bidirectional Algorithm or similar standards document why the word order should be changed by SQL Server? Shouldn't be change you expect made by preprocessing on another place, sending expected string form SELECT N'سرور SQL نرم افزار'? I see no point why just SQL SELECT should perform the change. If it did, what would happen if you feed result of such a SELECT into another SELECT? Another transformation? I have reasons to think that SQL server is interpreting your input technically correctly.
Hint: maybe you can try to surround your RTL text by different Directional formatting characters.
Please try the same SELECT with MySQL server at SQL Fiddle. Different server and technology, but the same result as Microsoft SQL Server gave.
Result from SSMS with MS SQL Server:
Conclusion: in order to get expected result, please form the input accordingly.
Related: Transformation of word order you expected can be done by appropriate settings in user interface.
When we add digit with english this will again not work following solution will work
SELECT nchar(8234)+ N' 33-M ' + N'کلینک کمرہ نمبر' +nchar(8236) + N'میں تشریف لائیں'

Convert column oriented table to row oriented one

I have a table structure as below on Greenplum database:
Wish to change it to the following structure so as to support pie charts on Tableau.
Could some one help me out ? Thanks!
Export the table to a CSV file
Install the Tableau Excel add-in
Open CSV file in Excel and use the add-in to reshape the data
SQL Server 2008 : Convert column value to row
http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html
Just to make sure you know about this Tableau feature:
Once you have devised the SQL select statement that will unpivot the data the way you'd like, then you can tell Tableau to use that instead of a select * by editing the data connection and selecting the Custom SQL option.
The generic way to unpivot in your situation is to union together several select statements, unless your database offers a more efficient alternative as described in the blog entry that Revanayya cited.
The following would work for a static, known beforehand, set of metrics:
SELECT
t.Date,
x.Metric,
CASE x.Metric
WHEN 'metric1' THEN metric1_week
WHEN 'metric2' THEN metric2_week
END AS week_val,
CASE x.Metric
WHEN 'metric1' THEN metric1_13week
WHEN 'metric2' THEN metric2_13week
END AS "13week_val"
FROM
atable AS t
CROSS JOIN
(VALUES ('metric1'), ('metric2')) AS x (Metric)
;
You could build a dynamic query off the above to account for an unknown number of metrics. For that, you would need to read the metadata (probably the INFORMATION_SCHEMA.COLUMNS system view) to build the dynamic bits, which are the VALUES list and the two CASE expressions, before embedding them into the query.

How to select all data from oracle database table in human readable format?

Sorry if my questions sounds silly, but I never worked with Oracle databases.
If I'm executing select * from table in MS Management Studio I have 3 options to select that data. I'm using grid and sometimes text. Using grid you're able to easily copy this into Excel spreadsheet.
When I'm selecting data from Oracle database table I have to format each column, but when I'm executing select * from table, I'm getting something very unreadable.
I'm using SQL Developer. Is there any way to retrieve data in more friendly view?
Thanks
Use Run Statement command (green arrow or Ctrl+Enter).
If you want export data, you can right click on table and select Export..
First, SQL Developer allows you to export selected data into many format, including the "new" XLSX spreadsheet format. So you do not really need to worry about formatting.
But if you want to do it yourself, you can try to select the data in CSV format that is very easy to convert to Excel spreadsheet. Something like this:
SELECT '"' || column1 || '","' || column2 || '"'
FROM table
WHERE ...
You might want to convert dates to some universal format (like YYYY-MM-DD) and you need to enclose text with double quotes if they contain double quotes.
You might want to consider using Toad for Oracle, The free version gives you the option to view your data in Grid Format and you will also be able to export it out into Excel or csv.