run_query using dbt and jinja - jinja2

I need to create a macro on dbt.
I have systematically 2 distinct values (in date format) in the date_export column, the date of the old export and the date of the new export, I have to compare the number of rows in the two date_export and take only the one with several rows, if the number of rows are equal I take the data with date_export = Min(date_export)
Here is my code, but I have an error "'nb_rows_previous' is undefined" when I try to run the model, anyone have an idea where I screwed up with my code?
{% set nb_rows_previous_data_query %}
select COUNT(*) from {{source(s, table)}}
WHERE date_export = (SELECT MIN(date_export)) FROM {{source(s, table)}} )
{% endset %}
{% set nb_rows_new_data_query %}
select COUNT(*) from {{source(s, table)}}
WHERE date_export = (SELECT MAX(date_export) FROM {{source(s, table)}} )
{% endset %}
{% set results_previous_data = run_query(nb_rows_previous_data_query) %}
{% set results_new_data = run_query(nb_rows_new_data_query) %}
{% if execute %}
{% set nb_rows_previous = results_previous_data.columns[0][0] %}
{% set nb_rows_new = results_new_data.columns[0][0] %}
{% endif %}
{% if nb_rows_previous >= nb_rows_new %}
SELECT
{{schema}}
FROM {{source(s, table)}}
WHERE date_export = (SELECT MIN(date_export) FROM {{source(s, table)}} )
{% else %}
SELECT
{{schema}}
FROM {{source(s, table)}}
WHERE date_export = (SELECT MAX(date_export) FROM {{source(s, table)}} )

In order to persist the value in both nb_rows_previous and nb_rows_new, you will need to wrap the whole last part of the macro with the {% if execute %}, so that it looks like this:
(...)
{% if execute %}
{% set nb_rows_previous = run_query(nb_rows_previous_data_query).columns[0][0] %}
{% set nb_rows_new = run_query(nb_rows_previous_data_query).columns[0][0] %}
{% if nb_rows_previous >= nb_rows_new %}
SELECT
{{schema}}
FROM {{source(s, table)}}
WHERE date_export = (SELECT MIN(date_export) FROM {{source(s, table)}} )
{% else %}
SELECT
{{schema}}
FROM {{source(s, table)}}
WHERE date_export = (SELECT MAX(date_export) FROM {{source(s, table)}} )
{% endif %}
{% endif %}
By the way, I also changed the code a little bit by doing the run_query command and the value selection in the same line of code

Related

SQL query to show multiple counts over the last day

How do i formulate a sql query where i can display the number of counts of success and errors and warning for the tables for latest day . So I can create a visualization in dashboard. The ultimate goal is for a user to know on a current day how many healthy pipelines are there and how many unhealthy pipelines are there.
i tried something like this, but i complicated it.
SELECT status, COUNT(*) as Total, cast(cast(time as Timestamp) as date) as time,
SUM(CASE WHEN Status = 'Success' THEN 1 ELSE 0 END) as Success,
SUM(CASE WHEN Status = 'Warning' THEN 1 ELSE 0 END) as Warning,
SUM(CASE WHEN Status = 'Error' THEN 1 ELSE 0 END) as Error
FROM table-name
where (cast(cast(time as Timestamp) as date) in ({{ Time }}) or {{ Time }} = 'All')
GROUP BY 1, cast(cast(time as Timestamp) as date)
order by cast(cast(time as Timestamp) as date) desc
lets say this is the given table
How do i formulate a sql query where i can display the number of counts of success and errors and warning for the tables for latest day .
You can use something like this:
SELECT DATE(time) as date, COUNT(*) as Total, ,
SUM(Status = 'Success') as Success,
SUM(Status = 'Warning') as Warning,
SUM(Status = 'Error') as Error
FROM table-name
WHERE date(time) = (SELECT MAX(date(time)) FROM table-name)
GROUP BY 1;

Sql add condition IF

I have sql like :
SELECT * FROM leads_notes WHERE content <> '' AND lead_id <> ''
I need add rule if type <> close_task then write user_change_task_status IS NULL
My result sql is:
SELECT * FROM leads_notes WHERE content <> '' AND lead_id <> '' IF(task_type <> 'close_task', 'AND user_change_task_status IS NULL',)
But i get many errors.
Cant understand how can i solve this. Please help, thanks!
Don't use if. Boolean logic is sufficient:
WHERE content <> '' AND
lead_id <> '' AND
( type = 'close_task' or user_change_task is null)
Or:
WHERE content <> '' AND
lead_id <> '' AND
NOT ( type = 'close_task' and user_change_task is not null )

Octave-'endfunction' command matched by 'endif'

Can someone tell me what's wrong with my code?
sinlaw('?',150,30,39.8)
parse error near line 30 of file
'endfunction' command matched by 'endif'
function [phi] = sinlaw (A,a,B,b)
If A==('?')
a=deg2rad(a)
b=deg2rad(b)
A=(B/sin(b))*sin(a)
endif
if(a=='?')
a=deg2rad(asin((A/B)*sin(b)))
endif
endfunction
While the syntax-check whines about matched by endif
The problem is actually in the If... typo
Feel free to test it here.
function [phi] = sinlaw ( A, a, B, b )
if A == ( '?' ) %% this works, not the "If"
a = deg2rad( a )
b = deg2rad( b )
A = ( B / sin( b ) ) * sin( a )
endif
if ( a == '?' )
a = deg2rad( asin( ( A / B ) * sin( b ) ) )
endif
endfunction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sinlaw( 150, '?', 30, 39.8 )
sinlaw( '?', 150, 30, 39.8 )

MySQL group_concat() ordering by case statement values

In a MySQL group_concat() clause, I'm trying to order the resulting values of a case statement. The following query configuration properly orders things.name but does not order the 'Non-US' or 'Unknown' values within the same context.
SELECT
things.id
,group_concat(DISTINCT
CASE
WHEN things.name <> 'United States' THEN 'Non-US'
WHEN things.name IS NULL THEN 'Unknown'
ELSE things.name
END
ORDER BY name SEPARATOR ', ')
FROM things
GROUP BY things.id
I want to do something like this, but it's not working:
SELECT
things.id
,group_concat(DISTINCT
(CASE
WHEN things.name <> 'United States' THEN 'Non-US'
WHEN things.name IS NULL THEN 'Unknown'
ELSE things.name
END) AS new_name
ORDER BY new_name SEPARATOR ', ')
FROM things
GROUP BY things.id
Is there a way to sort by "new_name" without using sub-queries/ nested queries?
You can accomplish this by ordering by column position instead of column name.
For your case ORDER BY 1 should work.
SELECT
things.id
,group_concat(DISTINCT
CASE
WHEN things.name <> 'United States' THEN 'Non-US'
WHEN things.name IS NULL THEN 'Unknown'
ELSE things.name
END
ORDER BY 1 SEPARATOR ', ')
FROM things
GROUP BY things.id
A session variable might work, but I am not sure the order of evaluation within a GROUP_CONCAT():
SELECT
things.id
, group_concat(DISTINCT
#new_name := (CASE
WHEN things.name <> 'United States' THEN 'Non-US'
WHEN things.name IS NULL THEN 'Unknown'
ELSE things.name
END
) ORDER BY #new_name SEPARATOR ', ')
FROM things
GROUP BY things.id
;
If that doesn't work, you could try doing the assignment in the ORDER BY section instead, and just using #new_name in the pre-ORDER BY.

Case condition is not working

What I need:
I need to check if eventvisitor.published is 1 then increment by zero else eventvisitor is equal to 0.
mysql query
SUM(
CASE
WHEN eventvisitor.published =1
THEN eventvisitor.published=eventvisitor.published+0
ELSE eventvisitor.published=0
END
) AS eventvsisitorpublished
sql query works
$qb->select('
eve.id,
SUBSTRING(ed.value,1,150) des,
eve.membership,
eve.name,
eve.abbrName abbr_name,
eve.membership paid,
eve.wrapper event_wrapper,
(case when attach.cdnUrl is null then attach.value else attach.cdnUrl end) event_samll_wrapper,
eve.url event_url,
eve.website,
eve.eventType,
venue.name venue_name,
e.startDate,
e.endDate,
ct.name city,
c.name country,
c.url country_url,
c.shortname country_shortname,
category.id industry_id,
category.name industry_name,
category.url industry_url,
eve.status event_status,
count(distinct eventvisitor.user) PeopleAttending
')
->add('from', $from);
->innerJoin('Entities\City','ct','with','eve.city = ct.id')
->innerJoin('Entities\Country','c','with','eve.country = c.id')
->innerJoin('Entities\EventEdition','e','with','eve.eventEdition = e.id')
->innerjoin('Entities\EventCategory','cat','with','e.event = cat.event')
->innerjoin('Entities\Category','category','with','cat.category = category.id')
->leftJoin('Entities\Venue','venue','with','e.venue=venue.id')
->leftJoin('Entities\EventVisitor','eventvisitor','with','eve.id=eventvisitor.event')
//->leftJoin('Entities\Venue','v','with','v.id = e.venue')
->leftJoin('Entities\EventData','ed','with','eve.eventEdition = ed.eventEdition')
->leftJoin('Entities\Attachment','attach','with','eve.wrapperSmall = attach.id')
->Where("ed.title ='desc' or ed.title is null")
->andWhere("eve.id in (".$res.")")
->andWhere("eventvisitor.published =1")
works check in where condition.
problem I'm facing: I'm getting 500 internal error
where I have done wrong any suggestion are most welcome.
use this
SUM(
IF(eventvisitor.published ==1, eventvisitor.published , 0)
) AS eventvsisitorpublished