So we have PIVOT keyword in Oracle /SQL Serverwhen you convert rows to columns.
Not in DB2, I want to convert row results and concatenate them into a column, dynamically, as in, I do not know the number of rows I might get, they might vary.
Eg
table x
COL1 COL2
ABC 10
ABC 20
ABC 30
I want to display this as
COL1 COL2
ABC 10,20,30
But the count of records might vary so I cannot use case.
Want to do this for queries of many tables not a particular query; a function or most preferably in the select query itself.
If you are using Db2 LUW 11.1.4.4 release, you might be able to workaround using sysibm.json_array function:
VALUES JSON_ARRAY(SELECT DEPTNO FROM DEPT);
1
-------------------------------
["F22","G22","H22","I22","J22"]
you can then stored the json_array in a character based column, varchar, clob.
For longer strings.
select substr(xmlserialize(
xmlquery('$L/text()' passing XMLAGG(xmlelement(name "a", ','||colname)) as "L")
as clob(1m)
), 2)
from syscat.columns;
Related
To find for one column, I can use this.
SELECT column1
FROM `dataset.table`
WHERE column1 IS NULL OR column1 = '';
But what if i have 100 columns? Instead of going through column by column, changing column 1 to 2,3,etc.,I'm looking for one for all solution. I'm kinda new to SQL and Data Cleaning.
Consider below approach
select *
from your_table t
where regexp_contains(to_json_string(t), r':(?:null|"")[,}]')
above will return you all rows where any column either null or empty string
I have a table with 260 columns, I just want to see only columns with nulls in it.
I know there are a few longer versions to see that information but is there an quicker way ?
Thanks Gurus
SELECT t.column_name
FROM user_tab_columns t
WHERE t.nullable = 'Y' AND t.table_name = 'mytable' AND t.num_distinct = 0
Also,before running it update your statistics:
BEGIN
DBMS_STATS.gather_database_stats();
END
If your question is about displaying Nullable table columns, look #Mihai...
You can definitely write dynamic Pl/Sql to build and execute a statement containing only columns that contain null. You can use series of loops, Ref_cursor, Execute Immediate, oracle data dictionaries, etc.
But if you would be able to, you would know already.
Regularly, you can select some data where certain values are null. E.g.
Select * From myTable where Col1 is null or col2 is null... -- 258 more columns
This will return all 260 columns, N rows where at least one column is Null
how to write a select query with one or two column excluded instead of list down required columns.
for example we have a table Table1 with 10 columns (col_1,col_2,col_2 ..... col_10)
and we just want select col_1 to col_8.
instead of writing like
Select col_1, col_2, col_3 .... col_8 from Table1
should we select like this
Select -(col_9, col_10) * from Table1
This is possible but you would have to use dynamic SQL or a stored procedure. It would still mean having to specify the start and end column using fixed column names and indexes and if the columns weren't in running order (colN, colN+1, etc.) then it would become messy.
So I suppose your answer it, just type it out. The benefit you will gain from doing something cleaver is small.
I hope this helps.
Any ideas?
I think we can select column name first then write into query.
1 Select column with excluded.
SHOW COLUMNS FROM _table WHERE FIELD NOT IN ('column1','column2')
2 Then, Use the column result we've got from above query to write to select query column.
SELECT {result_aboved} FROM _table WHERE 1
but I don't think it can be only once query for this case. you have to query twice times at least.
I have a basic database table. I would like to implement a functionallity that would allow to insert a select query within a random cell in the table. The result of this query would then be used as as any other cell of elementary type - in my case to compare it to an another value.
The problem is that I do not know in advance how those queries look like.
Here is an example. Say I have a an incoming parameter "score", which assumes some random integer values. I would like to see if the parameter "score" falls within the range defined between the values in Col1 and Col2, and if so happens, then to return the value in Col3.
Table1:
Col1 Col2 Col3
5 10 first row
10 15 second row
20 30 third row
* 50 forth row
* -> select avg(some_number) from Table2;
This random query can occur in any cell and is certain to return a single value. That is why I cannot use a simple JOIN statement.
Edit: Thanks Tim for suggesting to give an example.
You should look at CASE statements in SQL, and also at virtual or symbolic columns whose value is the result of an expression or function.
imagine there are 50 columns. I dont wan't any row that includes a null value. Are there any tricky way?
SQL 2005 server
Sorry, not really. All 50 columns have to be checked in one form or another.
Column1 IS NOT NULL AND ... AND Column50 IS NOT NULL
Of course, under these conditions why not disallow NULLs in the first place by having NOT NULL in the table definition
If it's SQL Server 2005+ you can do something like:
SELECT fields
FROM MyTable
WHERE stuff
EXCEPT -- This excludes the below results
SELECT fields
FROM MyTable
WHERE (Col1 + Col2 + Col3....) IS NULL
Adding a null to a value results in a null, so the sum of all your columns will be NULL.
This may need to change based on your data types, but adding NULL to either a char/varchar or a number will result in another NULL.
If you are looking at the values not being null, you can do this in the select statement.
SELECT ISNULL(firstname,''), ISNULL(lastname,'') FROM TABLE WHERE SOMETHING=1
This will replace nulls with string blanks. If you want another value use: ISNULL(firstname,'empty') for example. You can use anything where the word empty is.
I prefer this query
select *
from table
where column1>''
and column2>''
and (column3>'' or column3<'')
Allows sql server to use an index seek if the proper index/es exist. you would have to do the syntext for column 3 for any numeric values that could be negative.