Is there a way to rotate column's to row in snowflake? - multiple-columns

I have table with single row like below which comes from snowflake query like below
show warehouses like 'COMMON_WH';
select "name","state","type","size" FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
name|state|type|size
COMMON_WH|SUSPENDED|STANDARD|Small
I Want to rotate it like below.
name| COMMON_WH
state|SUSPENDED
type|STANDARD
size|Small
Thanks,

You can do this with a unpivot. Here is an example of how to use it. Note that it is a bit verbose because you need to cast all of the original table's datatypes to the same datatype before the unpivot (varchar in this case).
show warehouses like 'COMMON_WH';
select *
from (
select
"name"::varchar as name,
"state"::varchar as state,
"type"::varchar as type,
"size"::varchar as size
FROM TABLE (RESULT_SCAN(LAST_QUERY_ID()))
) unpivot (col_val for col_name in (name, state, type, size))
This produces:
COL_NAME
COL_VAL
NAME
COMMON_WH
STATE
STARTED
TYPE
STANDARD
SIZE
X-Small

Related

SQL query - I don't know all identifiers

Is there a way, where I can see every parameter or identifier I can query from my database? Not the contents but the "column names"
Something like
SELECT * FROM myDb AS String
To simply get the column names and types of a table.
You could SHOW them.
SHOW COLUMNS FROM myTable;
But if you want to know the column names of your table, and only a bit of data from it (to see what it looks like).
Then use LIMIT to get only a few records.
SELECT *
FROM myTable
LIMIT 3
It's fast and easy.
But you can also just see the columns without data if you use a criteria that's false.
SELECT *
FROM myTable
WHERE 0=1
You can also use:
show create table table_name;
but as "LukStorms" mentioned, the below statement shows you the data in table format and in a pretier way
show columns from table_name;
You can use INFORMATION_SCHEMA.COLUMNS to retrieve all columns name
select column_name from INFORMATION_SCHEMA.COLUMNS where Table_Name='Your_Table'

Retrive decimal values from table using mysql Like query

I want to retrieve records from table, all the records are decimal values in (width x Height) format
using like clause I want to to get data but query didn't work for it
Table:
id widthheight
1 17.14X12.41
2 23.4X39.8
select * from test where widthHeight like '%17X12%'
As said above, create two columns 'width' and 'heigth'
select * from (
select
id,
SUBSTRING_INDEX(widthHeight, 'X', 1) width,
SUBSTRING_INDEX(widthHeight, 'X', -1) height
from mytable) x
where floor(x.width)=17 and floor(x.height)=12
Your original query was not working because you should have done this:
select *
from mytable
where widthHeight like '%17%' and widthHeight like '%12%'
Better solution is to use a regex query:
SELECT * FROM test WHERE widthHeight REGEXP '17(\.\d+)?X12(\.\d+)?'
This regexp will match with following values:
17X12
17.14X12.41
17.00X12.00
17.9999999X12.999999
You can playground with this regex on the awesome Regex101. Go to this link.
Note: This query won't be efficient. Consider splitting your data into two columns named height and width that uses DECIMAL column type so you can do more efficient queries.
You should ideally just store the width and height as two separate decimal columns. That being said, you might be able to work with what you have now using some string tricks:
SELECT *
FROM test
WHERE
SUBSTRING_INDEX(widthHeight, 'X', 1) REGEXP '17[[:>:]]' AND
SUBSTRING_INDEX(widthHeight, 'X', -1) REGEXP '12[[:>:]]';

MySQL Cannot Compare TEXT

I have a MySQL table called table_1 that has column named col_1 of type TEXT. I am trying query a row that has a certain value using this statement, but no data is returned.
SELECT * FROM table _1 WHERE col_1 = '1325'
However, when I used this statement, I do get data.
SELECT * FROM table_1 WHERE col_1 LIKE '%1325%'
How do I get the first statement to work?
If the select with the WHERE col_1 LIKE '%1325%' brings back a value that looks like 1325, but then WHERE col_1 = '1325' doesn't?
Then it's probably because of invisible characters.
For example if the value of col_1 is '1325 '.
Because of the extra space, that value isn't equal to '1325'.
But it does contain '1325', so the LIKE would find it.
It's easy not to notice what's not visible.
What you could do is TRIM the value, so that spaces are removed before comparing it with =.
SELECT * FROM table_1 WHERE TRIM(col_1) = '1325';
Note that a default TRIM or RTRIM removes only the spaces.
To make that work for other whitespace characters the SQL becomes be a bit longer.
SELECT * FROM table_1
WHERE TRIM(Replace(Replace(Replace(col_1,'\t',''),'\n',''),'\r','')) = '1325';
And if you want to have visual evidence wether this is caused by invisible whitespaces?
Then use the LIKE, and CONCAT something to the start and end of the column. Then the spaces will be easy to notice in the result.
select CONCAT('[',col_1,']') AS Test, t.* from table_1 t where col_1 like '%1325%';
You can test it here
Because you do not test with:
- **SELECT * FROM table _1 WHERE col_1="1325" **
the second statment means the data contains 1325 but you don't have a row containning 13
Hope you got iT thanks

How do I search for a string in a cell substring containing a string from another cell in SQL

I am looking to compare the results of 2 cells in the same row. the way the data is structured is essentially this:
Col_A: table,row,cell
Col_B: row
What I want to do is compare when Col_A 'row' is the same as Col_B 'row'
SELECT COUNT(*) FROM MyTable WHERE Col_A CONTAINS Col_B;
sample data:
Col_A: a=befd-47a8021a6522,b=7750195008,c=prof
Col_B: b=7750195008
Col_A: a=bokl-e5ac10085202,b=4478542348,c=pedf
Col_B: b=7750195008
I am looking to return the number of times the comparison between Col_A 'b' and Col_B 'b' is true.
This does what I was looking for:
SELECT COUNT(*) FROM MyTable WHERE Col_A LIKE CONCAT('%',Col_B,'%');
I see You answered Your own question.
SELECT COUNT(*) FROM MyTable WHERE Col_A LIKE CONCAT('%',Col_B,'%');
is good from performance perspective. While normalization is very good idea, it would not improve speed much in this particular case. We must simply scan all strings from table. Question is, if the query is always correct. It accepts for example
Col_A: a=befd-47a8021a6522,ab=7750195008,c=prof
Col_B: b=7750195008
or
Col_A: a=befd-47a8021a6522,b=775019500877777777,c=prof
Col_B: b=7750195008
this may be a problem depending on the data format. Solution is quite simple
SELECT COUNT(*) FROM MyTable WHERE CONCAT(',',Col_A,',') LIKE CONCAT('%,',Col_B,',%');
But this is not the end. String in LIKE is interpreted and if You can have things like % in You data You have a problem. This should work on mysql:
SELECT COUNT(*) FROM MyTable WHERE LOCATE(CONCAT(',',Col_B,','), CONCAT(',',Col_A,','))>0;
SELECT * FROM MyTable WHERE Col_A = Col_B (AND Col_A = 'cell')
^^ Maybe you are looking for this statement. The part in brackets is optional.
If this is not the solution, please supply us with further information.
The easiest way would be to use the IN operator.
SELECT COUNT(*) FROM MyTable WHERE Col_A IN (Col_B);
More info on the IN operator: http://www.w3schools.com/sql/sql_in.asp
There's also the SUBSTRING() or MID() (depending on what you're using) function if you know that the substring will be in the same position everytime.
MID()/SUBSTRING() function: http://www.w3schools.com/sql/sql_func_mid.asp
You can use SUBSTRING_INDEX to extract a delimited field from a column.
SELECT COUNT(*)
FROM MyTable
WHERE Col_B = SUBSTRING_INDEX(SUBSTRING_INDEX(Col_A, ',', 2), ',', -1)
You need to call it twice to get a single field. The inner call gets the first two fields, the outer call gets the last field of that.
Note that this will be very slow if the table is large, because it's not possible to index substrings in MySQL. It would be much better if you normalized your schema so each field is in a separate column.
If column Col_a has data with format table,row,cell then search expression will be next:
SELECT COUNT(*) FROM MyTable AS MT
WHERE SUBSTRING(Col_A,
INSTR(Col_A, ',b=') + 3,
INSTR(Col_A, ',c=') - INSTR(Col_A, ',b=') + 3) = Col_B

MYSQL 5.6 : Select where value is in CSV colum

I have a database (Mysql 5.6) like this :
id | value
--------------------------
1 | "value1;value2;value3"
2 | "value4;value5;value6"
I'd like to make a request like :
SELECT id FROM table WHERE 'value5' IS IN value
return --> 2
SQLFiddle : http://sqlfiddle.com/#!9/b5c347
I cannot change the database schema
Use LIKE syntax, but use it like this:
SELECT id
FROM table
WHERE
value LIKE 'value5;%' OR
value LIKE '%;value5' OR
value LIKE '%;value5;%' OR
value LIKE 'value5';
This is a dirty solution, and not possible to index, but should fit your needs (On a smallish table in a reasonable amount of time)
Another solution (If you're sure your values don't contain commas):
SELECT id
FROM table
WHERE
FIND_IN_SET('value5', REPLACE(value, ';', ',')
If all the values are different, you might be able to use LIKE:
SELECT id FROM table WHERE value LIKE '%value5%';
Just keep in mind that it won't work in case you have some value that contains the other, like
value2;value3;value56
Or use a RegExp, like this:
SELECT id FROM table WHERE value REGEXP '(^|[^a-z0-9])value5([^a-z0-9]|$)'