Thruk - templating - Add href when host is member of hostgroup - thruk

In the _block.tt its possible to add href when custom_vars_host is present.
[% IF custom_vars_host.size > 0 %]
[% FOREACH cust = custom_vars_host %]
[% IF cust.key == "ilo" || cust.key == "drac") %]
<td><a href='https://[% cust.value %]/'>...</a></td>
[% END %]
[% END %]
[% END %]
Is there a way to add link when host is member of a hostgroup?

Same way as the macro
[% IF host.groups.size > 0 %]
[% FOREACH group IN host.groups.sort %]
[% IF group == "metrics-win" %]
[% END %]
[% END %]
[% END %]

extending the _block.tt template could lead to maintainance issues because you have to double check your changes after each update. You could use the new rest api together with the js action menus like in this example: https://thruk.org/documentation/action-menu.html#sending-commands

Related

Brand new to SQL, looking to find total sales on a specific day

I have the following:
SELECT [Sales_Line_Item].[Sales Date],
[Sales_Line_Item].[Quantity]*[Sales_Line_Item].[Unit Price] AS [Total Sales]
FROM Sales_Line_Item
WHERE [Sales Date] = #9/1/2020#;
Which displays:
I want to add the total sales together so that the result is:
Sales Date Total Sales
9/1/2020 $6,276.00
Thank you for any help
I wanted to use SUM(Total Sales) someway but I'm missing an operator
You can do:
SELECT [Sales_Line_Item].[Sales Date],
SUM([Sales_Line_Item].[Quantity]*[Sales_Line_Item].[Unit Price]) AS [Total Sales]
FROM Sales_Line_Item
WHERE [Sales Date] = #9/1/2020#
GROUP BY [Sales Date]

How to filter rows by the result of a calculation

The below code creates a new column "ZScore"
SELECT [Cardholder Name], [Debit Amount], ([Debit Amount] - AVG([Debit Amount]) OVER ()) / (STDEV([Debit Amount]) OVER ()) as [ZScore]
FROM ['Card Data']
ORDER BY [ZScore] DESC;
What I am trying to do is only display the rows where ZScore is >= 3. I have tried the following but everything seems to throw an error.
SELECT [Cardholder Name], [Debit Amount], ([Debit Amount] - AVG([Debit Amount]) OVER ()) / (STDEV([Debit Amount]) OVER ()) as [ZScore]
FROM ['PCard Output']
HAVING (([Debit Amount] - AVG([Debit Amount]) OVER ()) / (STDEV([Debit Amount]) OVER ())) > 3
ORDER BY [ZScore] DESC;
What would be the correct way to only display rows where the calculated z score is >= 3?
Just use a subquery:
SELECT cd.*
FROM (SELECT [Cardholder Name], [Debit Amount], ([Debit Amount] - AVG([Debit Amount]) OVER ()) / (STDEV([Debit Amount]) OVER ()) as [ZScore]
FROM ['Card Data']
) cd
WHERE ZScore > 3
ORDER BY [ZScore] DESC;

Error in CTE: The multi part identifier could not be bound

Table Structure:
dbo.PS_Margin
2 Columns
[Project Profit by Person %], [Emp or Vendor ID]
Error Messages:
Invalid object name 'dbo.PS_Margin'
(Solved) The multi part identifier could not be bound "PS_Margin.Emp or Vendor ID
Issues/ Goals:
Error messages above
Possible need to add aliases (or fix current aliases) to column names with spaces and special characters although I don't know how to do this
WITH Profit_Score_CTE ( [Emp or Vendor ID], [Project Profit by Person %], [Profit Score] ) AS (
SELECT ps.[Emp or Vendor ID], [Profit Score],
CASE
WHEN ps.[Project Profit by Person %] > .4 THEN 1
WHEN ps.[Project Profit by Person %] > .2 THEN 3
WHEN ps.[Project Profit by Person %] > .1 THEN 5
WHEN ps.[Project Profit by Person %] > .05 THEN 8
ELSE 13 END
AS [Profit Score]
FROM dbo.PS_Margin ps )
SELECT PS_Emp.[Employee Name], PS_Emp.[USID], [Profit Score]*0.3 AS [Final Score]
FROM dbo.PS_Emp LEFT OUTER JOIN Profit_Score_CTE ps
ON PS_Emp.[USID] = ps.[Emp or Vendor ID]
Perhaps you intend:
SELECT PS_Emp.[Employee Name], PS_Emp.[USID], [Profit Score]*0.3 AS [Final Score]
FROM dbo.PS_Emp LEFT OUTER JOIN
Profit_Score_CTE ps
ON PS_Emp.[USID] = ps.[Emp or Vendor ID]
You have defined the CTE but you are not using it.

Script help - count , total and avg

I have the following script, trying to count how many distinct customers are there , how many distinct orders , what is total of all orders under £15 and its's avg, total of orders above £20 and it's avg.
with consignments as
(select
[Sell-to Customer No_],
[Convert-to Document No_],
ic.[Shipping Agent Service Code],
[Pick Completed DateTime] as [Shipped DateTime],
ROUND((ic.[Amount Including VAT] + ic.Postage + ic.[Gift Wrap Price] +
ic.[Handling Fee] + ic.[Personalisation Fee]),2) as [Document Amount]
from dbo.[Temp$Consignment] ic inner join [dbo].
[Temp$Order] oh
on ic.[Owner Header GuID]=oh.[Order Guid]
where ic.[Shipping Agent Service Code]='secstan' and ic.[Pick Completed
DateTime] >= '2016-11-01T00:00:00.000' AND
ic.[Pick Completed DateTime] <= '2016-11-30T23:59:55.000' ),summary as
(select *,CASE WHEN [Document Amount] > 15 THEN 1 ELSE 0 END as 'Over15'
from consignments )select * from summary
I have working script like below, but as I am new to sql I am bit confused how to convert above script to below.
select amountclass,[Shipping Agent Service Code],
count(distinct [Sell-to Customer No_]) as Total_customers,
count(*) as Total_orders,
sum([Amount]) as total_revenue,
avg([Amount] * 1.0) as AOV
from
(
select [Sell-to Customer No_], oh.[Original Order No_], [Amount],ic.
[Shipping Agent Service Code],
case when [Amount] <= 20 then 'Under_20'
else 'Over_20'
end as amountclass
from [TBW_BI].[dbo].[Temp$Order] oh INNER JOIN [TBW_BI].[dbo].
[Temp$Consignment] IC
ON IC.[Owner Header GuID]=OH.[Order Guid]
where[order date] >= '2016-09-01' AND
[order date] <= '2016-09-30' AND [COUNTRY]='UNITED KINGDOM' and
[document type] like 'ord%' and ic.[Shipping Agent Service
Code]='secstan'
) dt
group by amountclass,[Shipping Agent Service Code]
order by amountclass,[Shipping Agent Service Code]
It looks like you have a query with a 2 Common Table Expressions (CTE) and you want to convert the CTE to a derived table. First, we can eliminate one of the CTEs.
summary as
(select *,CASE WHEN [Document Amount] > 15 THEN 1 ELSE 0 END as 'Over15'
from consignments )
select * from summary
The CTE for summary is unnecessary. We can convert that code to this:
select *,CASE WHEN [Document Amount] > 15 THEN 1 ELSE 0 END as 'Over15'
from consignments
Now, instead of using the consignments CTE; we can copy the sql code within it to create a derived table.
select *,CASE WHEN [Document Amount] > 15 THEN 1 ELSE 0 END as 'Over15'
from (
select [Sell-to Customer No_],
[Convert-to Document No_],
ic.[Shipping Agent Service Code],
[Pick Completed DateTime] as [Shipped DateTime],
ROUND((ic.[Amount Including VAT] + ic.Postage + ic.[Gift Wrap Price] +
ic.[Handling Fee] + ic.[Personalisation Fee]),2) as [Document Amount]
from dbo.[Temp$Consignment] ic
inner join [dbo].[Temp$Order] oh on ic.[Owner Header GuID]=oh.[Order Guid]
where ic.[Shipping Agent Service Code]='secstan'
and ic.[Pick Completed
DateTime] >= '2016-11-01T00:00:00.000'
AND
ic.[Pick Completed DateTime] <= '2016-11-30T23:59:55.000' ) as t

SQL: Creating temporary variables

I'm new to SQL so please consider this noob question. Also, its so embarrassing to admit that I cannot search for the right keyword in Google and I'm running out of time so I decided to ask it here.
Code:
select
*,
price * quantity as [Total price],
case
when [Total price]>100 and [Total price]<= 200 then '2%'
when [Total price]>200 and [Total price]<= 300 then '3%'
when [Total price]>300 and [Total price]<= 400 then '4%'
else '0%'
end as tax
from
grocery
As you can see, what I am trying to do is try to create a temporary variable on execution of SQL statement but, this one gives me error
Error 1: could not prepare statement [1 no such column: Total price]
How could I do this ?
A quick way would be to use a CTE (Common Table Expression). This allows you to pre-calculate some values and then refer the values in the body of the query.
If a statement with a CTE isn't the first thing in a batch, you need to end the preceding stuff with a ;:
;
With Totals as
(
select *,
price * quantity as [Total price],
from grocery
)
select *
, case
when [Total price]>100 and [Total price]<= 200 then '2%'
when [Total price]>200 and [Total price]<= 300 then '3%'
when [Total price]>300 and [Total price]<= 400 then '4%'
else '0%'
end as tax
from
Totals
The root of your problem, by the way, is that you can't define an expression and then use the expression by name in a query. You can re-use the expression, but not by name:
select x + 1 as Expr1
, (x + 1) * 2 as Expr2
from Table1
That will work, but the following won't work:
select x + 1 as Expr1
, Expr1 * 2 as Expr2 -- This won't work
from Table1
SELECT *,
case
when [Total price]>100 and [Total price]<= 200 then '2%'
when [Total price]>200 and [Total price]<= 300 then '3%'
when [Total price]>300 and [Total price]<= 400 then '4%'
else '0%'
end as tax
FROM (SELECT *, price * quantity as [Total price]
FROM grocery) A
OR
select
*,
price * quantity as [Total price],
case
when price * quantity >100 and price * quantity<= 200 then '2%'
when price * quantity>200 and price * quantity<= 300 then '3%'
when price * quantity>300 and price * quantity<= 400 then '4%'
else '0%'
end as tax
from
grocery