Is there an equivalent SQL query to SHOW WAREHOUSES; - snowflake-cloud-data-platform

I'm looking for SQL code equivalent to SHOW but as a Select statement.
Available: SELECT DATABASE_NAME FROM "SNOWFLAKE"."ACCOUNT_USAGE"."DATABASES"
Not Available???: SELECT WAREHOUSE_NAME FROM "SNOWFLAKE"."ACCOUNT_USAGE"."WAREHOUSES"

You could potentially make use of the RESULT_SCAN function.
So you could first run:
SHOW WAREHOUSES;
And then run:
SELECT * FROM table(result_scan(last_query_id()));
If you're doing this programmatically, then keep track of the query ID and pass it into the function like so:
SELECT * FROM table(result_scan('ce6687a4-331b-4a57-a061-02b2b0f0c17c'));

You could use:
SELECT DISTINCT WAREHOUSE_NAME FROM "SNOWFLAKE"."ACCOUNT_USAGE"."WAREHOUSE_METERING_HISTORY" ORDER BY 1;

Related

Is there a query to use in Snowflake to retrieve the original SELECT statement behind the creation of a table?

I am trying to find the original code behind the creation of a table in Snowflake.
I am using this query:
SELECT GET_DDL('table', 'table1');
This is only giving me the original DDL behind the table. I would need the full code (as in the original SQL SELECT statement).
Anyone know what query could get me that?
You can query QUERY_HISTORY and get the SQL statement (and other data) using the following:
// Be sure to use a role with permission to perform the following
SELECT
*
FROM
SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
WHERE
QUERY_TEXT ILIKE '%create%table%table1%'
ORDER BY END_TIME DESC
LIMIT 20;

Snowflake get query_id for running query?

I want to get the query_id for the running query in Snowflake programatically. It seems like account_usage.query_history doesn't have running query ids stored. Is there are any other way to get the query_id of running query?
If I'm not mistaken, you will need two sessions on your client application to get the query ID. You would only want to do this for long-running queries (for example to allow checking on status or aborting it after so many seconds), because you can get the query ID of a short query with last_query_id().
-- In first session, get the session ID before executing the long-running query.
select current_session() as SESSION_ID;
-- In second session, get the query ID of the running query in the first session
select QUERY_ID
from table(information_schema.query_history())
where execution_status ='RUNNING'
and session_id = 172747880058954;
You can use information schema.
select *
from table(information_schema.query_history()) where execution_status ='RUNNING';
You can do either which return the ID for the most recently executed query
select last_query_id();
Or select last_query_id(1);
which Return the ID for the first query executed in the session
Read more here
There are other options provided but if you want to check for queries running for specific time (long running) you can use this query.
SET long_running_threshold_seconds = 10;
SELECT * FROM TABLE(information_schema.query_history()) WHERE (execution_status = 'RUNNING'
AND datediff('seconds',start_time,current_timestamp()) > $long_running_threshold_seconds )

Can I look up the query id from a SnowSql session in the Snowflake Interface?

Is it possible to do the same thing using the SnowSql command line interface (CLI)? I'd like to view the SQL code for a particular query, as specified by its query ID, using the CLI.
When using the web console, one may go on the History tab and filter by "Query ID" e.g. "xxx-xxxxx" to view the SQL code and error messages (if any) for that particular query.
You can use LAST_QUERY_ID to retrieve the query IDs for queries in your session.
select last_query_id(); Gets the most Recent Query ID
select last_query_id(1); Gets the first Query ID of the session
select last_query_id(-2); Gets the Query ID from two queries ago.
etc.
Then you can use a query like this to get your actual Query Text if you need it.
SELECT QUERY_TEXT
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY_BY_SESSION(RESULT_LIMIT => 10))
WHERE QUERY_ID = '018cde0c-0077-09ee-0000-001812d26346'
;
If you need to retrieve query information outside of your Session, I believe you can use ACCOUNT_USAGE if that works for you.
SELECT * FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY WHERE QUERY_ID = '018cde0c-0077-09ee-0000-001812d26346';
Please try this using QUERY_HISTORY* Tables:
SELECT query_id, query_text,user_name,session_id,role_name,warehouse_name,
execution_status,error_code,error_message,start_time,end_time
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY(DATEADD('HOURS',-1,CURRENT_TIMESTAMP()),CURRENT_TIMESTAMP()))
ORDER BY start_time;
Reference : https://docs.snowflake.net/manuals/sql-reference/functions/query_history.html

From sql to qlik view declare variable

I am new to QV and am having a problem transferring this query to QV. What am I doing wrong?
ODBC CONNECT TO XXX);
abc:
SQL DECLARE #www INT
SET #www= ( SELECT max(Id) FROM [].[].[] where)
SQL select *
FROM [].[].[]
JOIN [].[.[] on .[] = .[]
where [qqq] = #www
order by rrr desc;
Did you manage to solve your problem?
From what I know it's not possible to use a SQL SET command, because in Qlik we already have a SET command which is far different from the SQL one.
I would suggest to do a first query that retrieves the max(Id), then pass it to a variable and then do the second query with that parameter.
i.e.
//execute the first query and retrieve the max Id
FirstQuery:
SQL
SELECT
max(Id) as maxId
FROM
schema.table;
//retrieve the value from the previous query and pass it to a variable
let vMaxId = fieldvalue('maxId', 1);
//execute the second query and use the variable in the where clause
SecondQuery:
SQL
SELECT
*
FROM
schema.table
WHERE
Field = $(vMaxId);
Also, take a look here to know how the FieldValue function works

Try to create a table from Select - SqL Server 2008 throws error

Hi I am trying to create a table using inner select statement...
for example:
CREATE TABLE JmxMonSer AS (SELECT * FROM services WHERE monitoring_enabled = 1);
But keep getting error:
Incorrect Syntax near keyword 'AS', Severity 15
please advice
I'm almost positive that SQL Server doesn't have a CREATE TABLE AS (SELECT... syntax, but you can use SELECT INTO:
SELECT *
INTO JmxMonSer
FROM services
WHERE monitoring_enabled=1
Check the MSDN documentation for proper uses of the CREATE TABLE statement.
How about:
SELECT * into JmxMonSer FROM services WHERE monitoring_enabled=1
If the table already exists (and the columns types and ordering line up), you can use:
INSERT INTO JmxMonSer SELECT * FROM services WHERE monitoring_enabled=1
The below syntax is for using sub-query instead of using static table name...
because sometime the required result set is coming from different queries..
SELECT *
INTO JmxMonSer
From (SELECT * FROM services WHERE monitoring_enabled = 1) as X
Try using SELECT INTO:
SELECT *
INTO newtable [IN externaldb]
FROM table1;
src: http://www.w3schools.com/sql/sql_select_into.asp

Resources