I am looking to monitor the credit usage for my snowflake account.
For warehouse I can get the credit info from metering history where I can see which warehouse had how many credits .
However I am not able to get the info for storage, For example I can see there is 12 tables whose size is huge like 100 plus terrabytes. I am getting that info from TABLE_STORAGE_METRICS.
But I need to know how much credits are getting used by those table.
Is there any view or table in information schema which can shows how many credits are getting used for these table.
To look at the average daily bytes used in the last days, you can use stage_storage_usage_history:
select *
from table(information_schema.stage_storage_usage_history(dateadd('days',-10,current_date()), current_date()));
https://docs.snowflake.com/en/sql-reference/functions/stage_storage_usage_history.html
If you want to look at the detail from stages and databases:
select convert_timezone('UTC', usage_date) as usage_date
, database_name as object_name
, 'database' as object_type
, max(AVERAGE_DATABASE_BYTES) as database_bytes
, max(AVERAGE_FAILSAFE_BYTES) as failsafe_bytes
, 0 as stage_bytes
from snowflake.account_usage.database_storage_usage_history
where usage_date >= date_trunc('day', ('2021-12-01')::timestamp_ntz)
and usage_date < date_trunc('day', ('2021-12-05')::timestamp_ntz)
group by 1, 2, 3
union all select convert_timezone('UTC', usage_date) as usage_date
, 'Stages' as object_name
, 'stage' as object_type
, 0 as database_bytes
, 0 as failsafe_bytes
, max(AVERAGE_STAGE_BYTES) as stage_bytes
from snowflake.account_usage.stage_storage_usage_history
where usage_date >= date_trunc('day', ('2021-12-01')::timestamp_ntz)
and usage_date < date_trunc('day', ('2021-12-05')::timestamp_ntz)
group by 1, 2, 3;
https://docs.snowflake.com/en/sql-reference/account-usage/database_storage_usage_history.html
https://docs.snowflake.com/en/sql-reference/account-usage/stage_storage_usage_history.html
As Greg says in a comment, you can transform these monthly average bytes into credits with a formula that will depend on the specifics of your account contract.
Related
I am trying to run some reports on the database (SQL Sever 2008) for our Incident Management System.
There are 2 tables, one containing Incident data and one containing Task data
The Incident table might look like this
Ref# OpenDate Category Group State
111 1/4/15 IncidentRequest A Open
112 1/4/15 ServiceRequest A Open
113 2/4/15 SalesRequest A Closed
114 3/4/15 IncidentRequest B Open
The Task table might look like this
TaskRef# OpenDate Group State
211 1/4/15 A Open
212 1/4/15 A Open
213 4/4/15 B Closed
214 5/4/15 A Closed
I would like to run a query that would return
OpenDate, SUM(IncidentRequest), SUM(ServiceRequest), SUM(SalesRequest), COUNT(Tasks) for Group A
so basically the amount of all Incidents, Service Requests, Sales Requests and Tasks for each OpenDate where the Group is A
Ideally I would also like to minimise load on the SQL server, speed is not a huge priority
I have looked at JOINS, UNIONS etc but I am very new to SQL and find myself suffering from a bit of information overload.
Any help would be appreciated.
Thanks,
DT
You can do this with Full Join and conditional sums:
Select * From
(
Select Date,
Sum(Case When Category = 'Incident Request' Then 1 End) As ir,
Sum(Case When Category = 'Service Request' Then 1 End) As ser,
Sum(Case When Category = 'Sales Request' Then 1 End) As sar
From Incidents
Where Group = 'A'
Group By Date
)i
Full Join
(
Select Date,
Count(*) As rc
From Tasks
Where Group = 'A'
Group By Date
)t On i.Date = t.Date
I have a MSSQL server 2012 express DB that logs user activities. I need some help creating a query to compare timestamps on the user activities based on the text in the notes. I am interested in figuring out how long it takes my users to perform certain activities. The activities they are performing are stored in text in a NOTES column. I want to build a query that will tell me the time difference for each [INVOICEID] from the ‘START NOTE’ to the next note for that invoice by that user. The note that is entered is always the same for the start of the timer (for the purposes of this I used ‘START NOTE’ to indicate the start of the timer, but I have multiple activites I will need to do this for so I plan on simply changing that text in the query), but the end of the timer the text of the note will vary because it will be user entered data. I want to find the time difference between ‘START NOTE’ and the note that immediately follows ‘START NOTE’ entered by the same USERID for the same INVOICEID. Please see the SQLfiddle for an example of my data:
http://sqlfiddle.com/#!3/a00d7/1
With the data in the sql fiddle I would want the results of this query to be:
INVOICE ID USERID TIME_Difference
100 5 1 day
101 5 3 days
102 5 9 days
(time_difference does not need to be formatted like that, standard SQL formatting is fine)
I don’t really know where to start with this. Please let me know if you can help.
Thanks
select a.userid,a.invoiceid,min(a.added),min(b.added),datediff(DAY,min(a.added),min(b.added)) from om_note a
left join om_note b on a.userid=b.userid and a.invoiceid = b.invoiceid and a.added < b.added
where a.notes = 'START NOTE' group by a.userid,a.invoiceid
;with x as (
select
o.*, sum(case when notes='START NOTE' then 1 else 0 end)
over(partition by o.invoiceid, o.userid order by o.added) as grp
from om_note o
),
y as (
select *,
row_number() over(partition by x.invoiceid, x.userid, x.grp order by x.added) as rn
from x
where grp > 0
)
select y1.invoiceid, y1.userid, datediff(hour, y1.added, y2.added)
from y y1
inner join y y2
on y1.invoiceid=y2.invoiceid and y1.userid=y2.userid and y1.grp=y2.grp
where y1.rn=1 and y2.rn=2
I want to graph the % of users over time that have their Twitter account connected. The number of users changes constantly, and so does the % of them that connect their Twitter account.
The table has a user account specific createDateTime column as well as a tw_connectDateTime column.
Let's say I'm interested in the trend of % connected over the last 7 days. Is there a way I can have MSSQL calculate the percentage for every day in the specified range, or do I need to do it myself using multiple queries?
Doing it in app logic would look something like (pseudocode):
for day in days:
query:
select
count(userId) as totalUsers
,c.connected
,cast(c.connected as float)/count(userId) as percentage
from
Users
outer apply (
select
count(userId) as connected
from
Users
where
tw_connectDateTime <= $day
) as c
where
createDateTime <= $day
group by
c.connected
What I'm unsure of is how, if it's possible, to expand this to run for each day, so that the results include the date as a column and the same values that I would get from running the above query for each date in the range.
Is it possible? If so, how?
actually you can use your query joined with days, like this:
with cte_days as (
select #DateStart as day
union all
select dateadd(dd, 1, c.[day]) as [day]
from cte_days as c
where c.[day] < #DateEnd
)
select
d.[day],
count(u.userId) as totalUsers,
c.connected,
cast(c.connected as float)/count(u.userId) as percentage
from cte_days as d
inner join Users as u on u.createDateTime <= d.[day]
outer apply (
select
count(T.userId) as connected
from Users as T
where T.tw_connectDateTime <= d.[day]
) as c
group by d.[day], c.connected
I have two views that i want tho merge them into one view so that their records are not merge into one record! i mean suppose I have these tables :
Table one(suppose this is a sell table were our customer sold something!)
Date Description Fee Number Money
12/2/2012 something 10$ 20 200$
10/3/2012 somethingelse 20$ 30 600$
Table Two (suppose this is the table where our customer got money!)
Date Description Money
02/8/2012 someinfo 5000$
12/1/2012 stuff 3100$
And the resulting Table or view would be(based on the descending order on date) :
Date Description Fee Number Money
02/8/2012 someinfo 0 0 5000$
10/3/2012 somethingelse 20$ 30 600$
12/2/2012 something 10$ 20 200$
12/1/2012 stuff 0 0 3100$
How can I achieve this form? These two tables are separate ,but each has a unique personal ID which represents the salesmen account. ( so basically this means that these information belong to one person only.and our customer wants a report that gives him this specific view only!)
I tried using UNION on these two tables , but the rows where merged!!
If i use Joins there would only be a row where the two tables row are merged together .So I am stuck here and dont know what to do now .
I think you need UNION ALL not just UNION.
select Date, Description, Fee, Number, Money
from table1
UNION ALL
select Date, Description, 0 Fee, 0 Number, Money
from table2
order by Date
Try somthing like
CREATE VIEW vMyView
AS
SELECT [Date], [Description], [Fee], [Number], [Money]
FROM v1
UNION ALL
SELECT [Date], [Description], 0 AS [Fee], 0 AS [Number], [Money]
FROM v2
I think this should do it.
CREATE VIEW new_view AS
SELECT * FROM table_one
UNION ALL
SELECT *, 0 as Fee, 0 as Number FROM table_two;
I've been asked to look at a database that records user login and logout activity - there's a column for login time and then another column to record logout, both in OLE format. I need to pull together some information about user concurrency - i.e. how many users were logged in at the same time each day.
Do anyone know how to do this in SQL? I don't really need to know the detail, just the count per day.
Thanks in advance.
Easiest way is to make a times_table from an auxiliary numbers table (by adding from 0 to 24 * 60 minutes to the base time) to get every time in a certain 24-hour period:
SELECT MAX(simul) FROM (
SELECT test_time
,COUNT(*) AS simul
FROM your_login_table
INNER JOIN times_table -- a table/view/subquery of all times during the day
ON your_login_table.login_time <= times_table.test_time AND times_table.test_time <= your_login_table.logout_time
GROUP BY test_time
) AS simul_users (test_time, simul)
I think this will work.
Select C.Day, Max(C.Concurrency) as MostConcurrentUsersByDay
FROM
(
SELECT convert(varchar(10),L1.StartTime,101) as day, count(*) as Concurrency
FROM login_table L1
INNER JOIN login_table L2
ON (L2.StartTime>=L1.StartTime AND L2.StartTime<=L1.EndTime) OR
(L2.EndTime>=L1.StartTime AND L2.EndTime<=L1.EndTime)
WHERE (L1.EndTime is not null) and L2.EndTime Is not null) AND (L1.ID<>L2.ID)
GROUP BY convert(varchar(10),L1.StartTime,101)
) as C
Group BY C.Day
Unchecked... but lose date values, count time between, use "end of day" for still logged in.
This assumes "logintime" is a date and a time. If not, the derived table can be removed (Still need ISNULL though). of course, SQL Server 2008 has "time" to make this easier too.
SELECT
COUNT(*)
FROM
(
SELECT
DATEADD(day, DATEDIFF(day, logintime, 0), logintime) AS inTimeOnly,
ISNULL(DATEADD(day, DATEDIFF(day, logouttime, 0), logintime), '1900-01-01 23:59:59.997') AS outTimeOnly
FROM
mytable
) foo
WHERE
inTimeOnly >= #TheTimeOnly AND outTimeOnly <= #TheTimeOnly