Convert column oriented table to row oriented one - sql

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.

Related

SQL to add only new data

I'm using Pentaho Data Integration, and in it, I make a selection in database X, where I get some information with filters, then I need to update this information in database Y. However, I need to ignore the data already recorded and just add the new data, as can i do this with a sql?
you could use a merge statment for that
if your fields names are same in tables you can use this query:
insert into Y.dbo.Y_1(fieldName)
select fieldName from X.dbo.X_1

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).

SQL Server - Syntax around UNION and USE functions

have a series of databases on the same server which i am wishing to query. I am using the same code to query the database and would like the results to appear in a single list.
I am using 'USE' to specify which database to query, followed by creating some temporary tables to group my data, before using a final SELECT statement to bring together all the data from the database.
I am then using UNION, followed by a second USE command for the next database and so on.
SQL Server is showing a syntax error on the word 'UNION' but does not give any assistance as to the source of the problem.
Is it possible that I am missing a character. At present I am not using ( or ) anywhere.
The USE statement just redirects your session to connect to a different database on the same instance, you don't actually need to switch from database to database in this matter (there are a few rare exceptions tho).
Use the 3 part notation to join your result sets. You can do this while being connected to any database.
SELECT
SomeColumn = T.SomeColumn
FROM
FirstDatabase.Schema.TableName AS T
UNION ALL
SELECT
SomeColumn = T.SomeColumn
FROM
SecondDatabase.Schema.YetAnotherTable AS T
The engine will automatically check for your login's users on each database and validate your permissions on the underlying tables or views.
UNION adds result sets together, you can't issue another operation (like USE) other than SELECT between UNION.
You should use the database names before the table name:
SELECT valueFromBase1
FROM `database1`.`table1`
WHERE ...
UNION
SELECT valueFromBase2
FROM `database2`.`table2`
WHERE ...

How to run a select sql statement within a field in the Pentaho?

I have a table with a 'query' field containing a select sql and another 'parameters' field containing the sql parameters. I have merged these two fields into a new field containing a correct select sql statement. Now I need to execute this new field containing select sql, get the return from select (the output fields) and generate an excel file.
Use Table-Input if you are interested in a query result set. Table-Input supports SQL parameters, so no need to build the statement yourself using e.g. Replace-In-String, and tripping over escapes on your way. Also, there's variable substitution, just in case you can't live with a single template.
Update 21:14 GMT
I'm not very fond of the way you try to prepare the SELECT statement, but here we go, assuming it's a single statement we have:
Create a job with a Start entry and 2 Transformation entries (T1, T2). Let T1 produce the field containing your SELECT statement and use a Set-Variables step to make the statement available to T2 as variable SELECT. In T2 use a Table-Input step referencing ${SELECT} in the SQL statement text area. Don't forget to enable option "Replace variables in script".
From now on it's a matter of taste. I would prefer to create a CSV file using Text-File-Output. Using the right field separator Excel will open the file after double-clicking it. The advantage of Text-File-Output is that you don't have to specify the fields you don't know at design-time anyway. An empty field list will just handle all fields coming in. Comparable to the total projection in a Table-Input which will create the necessary fields from the retrieved columns downstream.
If you must produce an Excel workbook, you'll have to learn about metadata injection. That would be a separate project for a beginner, though. There are samples in your Kettle installation folder. And there is a very active community if you find yourself in trouble.

sql or trick to search through whole database

is there a way to actually query the database in a such a way to search for a particular value in every table across the whole database ?
Something like a file search in Eclipse, it searches accross the whole worspace and project ?
Sorry about that .. its MS SQL 2005
SQL Workbench/J has a built in tool and command to do that.
It's JDBC based and should also work with SQL Server.
You will need to use the LIKE operator, and search through each field separately. i.e.
SELECT * FROM <table name>
WHERE (<field name1> LIKE '%<search value>%') OR
(<field name2> LIKE '%<search value>%') OR
... etc.
This isn't a quick way though.
I think the best way would be to
1) programatically generate the query and run it
2) use a GUI tool for the SQL server you are using which provides this functionality.
In mysql you can use union operator like
(SELECT * from table A where name = 'abc') UNION (SELECT * from
table B where middlename = 'pqr')
and so on
use full text search for efficency
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
Well, your best bet is to write a procedure to do this. But to give you some pointers you can use the INFORMATION_SCHEMA.Tables to get a list of all the tables in a given database and INFORMATION_SCHEMA.Columns to get a list of all columns. These tables also give you the datatype of columns. So you will need a few loops on these tables to do the magic.
It should be mentioned most RDBMSs nowadays support these schemas.
In phpmyadmin, go to your database, reach the search tab.
Here you will be able to select all of your tables and search through your entire db in one time.