Need to know if SQL Oracle can output a certain hierarchical style - sql

Needing to know if this is possible.
I'm trying to get a hierarchical output looking like this:
TITLE|CHART|FUND|ORGN|PROGRAM
S |S |null|null|
S |S |1 |null|
S |S |1 |10 |
S |S |1 |10 |1
S |S |2 |null|
Is this possible to accomplish in SQL Oracle?
I can't figure out the proper verbiage for this and that's why I'm asking here.

Your sample result doesn't look hierarchical,
hierarchical queries usually have a tree structure and a CONNECT BY clause.
You can probably get the required result by using the ORDER BY clause as follows:
SELECT TITLE, CHART, FUND, ORGN, PROGRAM
FROM employers_data_table
ORDER BY TITLE
,CHART
,FUND ASC NULLS FIRST
,ORGN ASC NULLS FIRST
,PROGRAM ASC NULLS FIRST

In case anyone comes across this, the answer I found was to use unions.
Thanks Crowne for your answer, it definitely helped.

Related

Using ranking functions without order by clause

I have got a PostgreSQL database. There is a query that orders rows by some_date and minutes. The result looks like this:
id|is_smth|some_date |minutes
35|true |2021-11-10|985
36|true |2021-11-19|684
35|true |2021-11-25|605
34|false |null |null
Then, I have tried applying DENSE_RANK() to it, to implement pagination in my application, but it does not work as I need it to, as it has to order the rows by given columns.
What I am trying to achieve is just giving the rank to each row by the criteria specified in the original ORDER BY clause plus the id of the result set without changing their order. So, it would look like this:
id|is_smth|some_date |minutes|rank
35|true |2021-11-10|985 |1
36|true |2021-11-19|684 |2
35|true |2021-11-25|605 |1
34|false |null |null |3
I have to retrieve the first two rows AND all of the associated data with them (in this case it would be all of the other rows with ids 35 and 36). Is it possible?

Ignore NULLs in GROUP BY? Or a better way to combine row that fill gaps in data?

Due to partial duplicates in some of my database, after some LEFT JOINs I wind up with several (but not all) rows where I have partial data, along with NULLs. For a unique user, one row may have a ZIP code, and another row may have the STATE of that same user.
Let me show you an example:
|email |state |zip |
|-----------------|------|------|
|unique#email.com |NULL |40502 |
|unique#email.com |KY |NULL |
|other#email.com |FL |34744 |
|other#email.com |FL |34744 |
|third#email.com |OH |NULL |
Rows with full duplicates (such as other#email.com in my example) are easy enough to cleanup with a GROUP BY clause, and some people like third#email.com in my example have NULLs and that's ok, but for unique#email.com I have the state in one row and zip in another, what is the best way to combine those two into one row?
A desired result would be:
|email |state |zip |
|-----------------|------|------|
|unique#email.com |KY |40502 |
|other#email.com |FL |34744 |
|third#email.com |OH |NULL |
For the data you have provided, you can use aggregation:
select email, max(state) as state, max(zip) as zip
from t
group by email;
That said, you can probably fix this in the query used to generate the data. Also, if you want multiple rows for a given email in the result set, then you should ask a new question with a clearer example of data.

Bigquery select column only if not null

I am an absolute beginner in Bigquery and SQL so apologies if this is a dumb question. I have a bigquery table like this
|Name|Value1|Value2|Value3|Value4|Value5|Value6|
|Ben |19 |45 |null |19 |13 |null |
|Bob |34 |null |12 |null |45 |43 |
My query only selects one row that matches the name in Name column. I want the result to only display columns that have non null values. For example if I do
SELECT * FROM mytable WHERE Name = "Bob"
I want the result to look like
|Name|Value1|Value3|Value5|Value6|
|Bob |34 |12 |45 |43 |
Similarly, if I select for Ben I want the result to look like
|Name|Value1|Value2|Value4|Value5|
|Ben |19 |45 |19 |13 |
I have tried SELECT IF but don't seem to get the syntax right.
You cannot select a variable amount of columns, but you may be able to create a SQL, with a combination of aggregate/pivot functions. You may be spending more time than it's worth trying to do it. I spend about two hours on the documentation, and I still feel almost clueless (If doesn't help that I don't have an account there, and my own database does not have the same exact functions).
See Google's BigQuery Documentation for examples.
I think you may be able to do it with UNNEST() and ARRAY(), but you'll lose the original column header information in the process.
I doubt if it can be achieved, because any SQL statement will act on record(s),i.e various columns, so if a column is null, it will affect all columns in the record that are to be retrieved. SQL STATEMENTS RETRIEVE ROWS(COLUMNS REFERENCED)
You can not do that dynamically in SQL. If you need a query like that you could create it manually but it depends on the results you want to achieve.
In the case you showed for example, the query below would work but you would lose the table's header reference.
SELECT value1,value2,value4,value5 FROM mytable WHERE value3 IS NULL AND value6 is NULL
UNION ALL
SELECT value1,value3,value5,value6 FROM mytable WHERE value2 IS NULL AND value4 is NULL
In this example it's possible to see that this kind of query is complicated to build if you have many conditions. Besides that, UNION ALL will always need the same number of columns in each separate query to work. If you need to create a generic query to do that, it's not gonna be possible.
I hope it helps

SQL Function to separate commas in column into new rows

I have a table that contains a column with comma separated values. I need to separate those values into new rows. Table looks like this :
ID| DATE|Value|Feed|
1|10-10-2014|5.00 |1,3,4
2|10-11-2014|21.00|1
54|01-15-2015|8.24 |2,15
1|02-22-2015|5.14 |1,3,4
And I need to break it out to :
ID| DATE|Value|Feed|
1|10-10-2014|5.00 |1
1|10-10-2014|5.00 |3
1|10-10-2014|5.00 |4
2|10-11-2014|21.00|1
54|01-15-2015|8.24 |2
54|01-15-2015|8.24 |15
1|02-22-2015|5.14 |1
1|02-22-2015|5.14 |3
1|02-22-2015|5.14 |4
So I believe I need a to write a table valued function but I'm having a difficult time figuring out where to start.
Any guidance would be great.
Thanks.
You are going to need the ID so that you can join back to your table, but here is some help for the Split function https://stackoverflow.com/a/10914602/3854195
EDIT: (How to use a cursor to combine the results from the Split function with your source table)
I setup this SQL Fiddle

QSqlField real type name

i'm looking for a way to retrieve real database type name from qtsql model.
Unfortunately
QVariant::typeToName(field.type())
Where field is QSqlField type
gives me already mapped type to some Qt type. Is it possible to get real names using Qt?
i found a way to retrieve the real database type with following query:
QSqlQuery query("PRAGMA table_info(tableName)");
As a result you will get:
cid|name |type |notnull |dflt_value |pk
0 |id |integer |0 | |1
1 |name |varchar(45) |1 | |0
thanks to this post:
Getting the type of a column in SQLite
Hope this helps