Query in COGNOS (Report Studio) - sql

I have this follow query;
the objective is to filter this query by day.
I will use this same query, but now a month. How to do?
WITH DATAS(DATA_CALC) AS(
SELECT DATE(#prompt('DataIni')#) AS DATA_CALC FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT DATA_CALC + 1 DAY FROM DATAS WHERE DATA_CALC < DATE(#prompt('DataFim')#)
)
/* ABERTOS */
SELECT COUNT(*) AS QUANTIDADE, 'ABERTOS' AS TIPO, 1 AS NUM_LINHA, DATE(TRUNC(SOL.DATA_ABERTURA_SOLICITACAO)) AS DATA
FROM VW_SOLICITACAO_DETALHE SOL
INNER JOIN SOLICITACAO SOL_TAB ON (SOL_TAB.ID_SOLICITACAO = SOL.ID_SOLICITACAO)
WHERE SOL.COD_TIPO_SERVICO IN ('MC','OP','SA')
AND SOL_TAB.ID_AREA_COLABORADOR IN (#promptmany('IDArea')#)
AND TRUNC(SOL.DATA_ABERTURA_SOLICITACAO) >= to_date('2014-11-18','YYYY-MM-DD')
AND TRUNC(SOL.DATA_ABERTURA_SOLICITACAO) <= #prompt('DataFim')#
AND ('TODOS' IN (#promptmany('Sistema')#) OR SOL.COD_PRODUTO IN (#promptmany('Sistema')#))
AND DSC_SITUACAO_SOLICITACAO NOT IN ('Desenvolvimento Cancelado')
AND SOL.COD_SOLICITACAO NOT LIKE 'SA%'
GROUP BY TRUNC(SOL.DATA_ABERTURA_SOLICITACAO)

The best solution is to create a Time dimension that includes days and months. This is not easy to plug into Dynamic SQL like you are using above, so you can instead try a work around.
A report studio work around would be to create year and month columns, and group on them:
For year:
extract(year, [datefieldhere])
For month:
extract(month, [datefieldhere])

Related

How to split lines in SQL table according to rules?

so I have SQL table with many lines. It looks like this (just a couple lines from the top):
I need to disperse revenue according to project duration equally every 2 months starting first month. So desired result would look like this:
What's the baest way to achieve this?
You can use recursive cte :
with cte as (
select code, finish2 as acc_date, dateadd(month, project_duration, finish2) as end_date
from table t
union all
select code, dateadd(month, 2, acc_date), end_date
from cte c
where dateadd(month, 2, acc_date) < end_date
)
select code, acc_date
from cte c;
I would create a table or a query containing the dates ranges for each project. Then you JOIN those 2 tables - which will create as many lines for each project as there are dates ranges and you can divide the total revenue by a count(*) of dates ranges or something similar for each line.

sum last n days quantity using sql window function

I am trying to create following logic in Alteryx and data is coming from Exasol database.
Column “Sum_Qty_28_days“ should sum up the values of “Qty ” column for same article which falls under last 28 days.
My sample data looks like:
and I want following output:
E.g. “Sum_Qty_28_days” value for “article” = ‘A’ and date = ‘’2019-10-8” is 8 because it is summing up the “Qty” values associated with dates (coming within previous 28 days) Which are:
2019-09-15
2019-10-05
2019-10-08
for “article” = ‘A’.
Is this possible using SQL window function?
I tried myself with following code:
SUM("Qty") OVER (PARTITION BY "article", date_trunc('month',"Date")
ORDER BY "Date")
But, it is far from what I need. It is summing up the Qty for dates falling in same month. However, I need to sum of Qty for last 28 days.
Thanks in advance.
Yes, this is possible using standard SQL and in many databases. However, this will not work in all databases:
select t.*,
sum(qty) over (partition by article
order by date
range between interval '27 day' preceding and current row
) as sum_qty_28_days
from t;
If your RDBMS does not support the range frame, an alternative solution is to use an inline subquery:
select
t.*,
(
select sum(t1.qty)
from mytable t1
where
t1.article = t.article
and t1.date between t.date - interval 28 days and t.date
) sum_qty_28_days
from mytable t

Need your assistance with a SQL Server query to get result days between two dates

How can I display the query in such a way to show each day between date?
SELECT job.wo_id
FROM [MES].[MESDB].[dbo].[job] AS job
WHERE job.init_sched_ent_id = 227
AND job.sched_start_time_local >= #paramStartDate
AND job.sched_finish_time_local <= #paramEndDate
I can select each day separately by function from this post but I don't know how to combines these two tables.
https://stackoverflow.com/a/44671808/8853661
You can generate dates in mySql and cross join with your select.
The good description is here : generate days from date range

How to get the third month of a query without using variables?

I have a query in which I need to get the third month of the given reporting date using SQL and then use it as part of the query. I am able to get all the months but I specifically need to get the third month how would I go about doing that? I know this is fairly easy to do in other languages but is it possible in SQL?
SELECT REPORTING_MONTH, COUNT(*)
FROM database1 AS fb
JOIN (
--derrived core set
SELECT service_no, subscription_id
FROM database2
WHERE REPORTING_MONTH = '2015-04-01' <-- this is the reporting month
) AS c
ON fb.SERVICE_NO = c.service_no
AND fb.subscription_id = c.subscription_id
AND fb.REPORTING_MONTH = '2015-07-01' <-- THIS SHOULD BE THE THIRD MONTH
AND fb.ACTIVE_BASE_IND_NEW = 1
GROUP BY 1
ORDER BY 1
For example if the reporting month is '2015-04-01 I need the variable month to then be '2015-07-01' to be used as part of the query
You don't specify the database you are using. A typical approach would be:
SELECT REPORTING_MONTH, COUNT(*)
FROM database1 fb JOIN
database2 c
ON fb.SERVICE_NO = c.service_no AND
c.REPORTING_MONTH = '2015-04-01' AND
fb.subscription_id = c.subscription_id AND
fb.REPORTING_MONTH = c.reporting_month + interval '3 month' AND
fb.ACTIVE_BASE_IND_NEW = 1
GROUP BY 1
ORDER BY 1;
The exact syntax for + interval '3 month' varies by database.
If the field REPORTING_MONTH is text then you might have to use SUBSTRING (SQL Server) or MID (Others).
If it's a proper date field then perhaps DATEPART(month, fb.REPORTING_MONTH) = 3 will work?
My SQL is a bit rusty but try those functions.

generate each minute string for a day within specified time limit

My aim is to generate per minute count of all records existing in a table like this.
SELECT
COUNT(*) as RECORD_COUNT,
to_Char(MY_DATE,'HH24:MI') MINUTE_GAP
FROM
TABLE_A
WHERE
BLAH='Blah! Blah!!'
GROUP BY
to_Char(MY_DATE,'HH24:MI')
However, This query doesn't give me the minutes where there were no results.
To get the desired result it, I'm to using the following query to fill the gaps in the original query by doing a JOIN between these two results.
SELECT
*
FROM
( SELECT
TO_CHAR(TRUNC(SYSDATE)+( (ROWNUM-1) /1440) ,'HH24:MI') as MINUTE_GAP,
0 as COUNT
FROM
SOME_LARGE_TABLE_B
WHERE
rownum<=1440
)
WHERE
minute_gap>'07:00' /*I want only the data starting from 7:00AM*/
This works for me, But
I can't rely on SOME_LARGE_TABLE_B to generate the minutes
because it might have no records at some point in future
The query doesn't look like a professional solution.
Is there any easier way to do this?
NOTE:I don't want any new tables created with static values for all the minutes just for one query.
Just generate your timestamps and left join your grouped data to it:
SELECT MINUTE, ....
FROM (
SELECT TO_CHAR(TO_DATE((LEVEL + 419) * 60, 'SSSSS'), 'HH24:MI') MINUTE /* 07:00 - 23:59 */ FROM DUAL CONNECT BY LEVEL <= 1020)
LEFT JOIN (
<your grouped subquery>
) ON MINUTE = MINUTE_GAP