COGNOS - How to sum the last 12 row values - cognos

I need to sum the last 12 of a value for EVERY ROW. On SQL this looks like it:
sum(value) over (partition by field1, field2
order by field3 asc rows BETWEEN 11 preceding AND CURRENT row
) AS total_acum
I can't do that on the database and need to do that on the Cognos... Any tip?
Obs.:
I'm using the version 11.1 R7
I'm using the Cognos in Portuguese. Because of that, i'm using ; and not , on the formulas.
Edit2:
The answer given by #C'est Moi, is pretty much what i want. But i'm keep having errors and i have no idea why.
#C'est Moi tells to use the code:
running-total ( QUANTITY
for
Period ( _add_months ( order_day_date, -12) , order_day_date)
)
But when i do that, i got this error messages:
Try 1 My code (with ;):
running-total ( [total] for period( _add_months ( [date]; -12) ; [date]) )
Try 1 Error:
XQE-V5-0017 Erro de sintaxe V5 localizado para o item de dados ''query_field_name'' da consulta ''cognos_query'', token inválido ";" localizado após "running-total ( [total] for period( _add_months ( [date]; -12) ". CAF-WRN-2082 ... O CAF ... log com o SecureErrorID:2021-01-18-12:36:58.114-#874
Try 2 My code (with ,):
running-total ( [total] for period( _add_months ( [date]; -12) , [date]) )
Try 2 Error:
XQE-V5-0017 Erro de sintaxe V5 localizado para o item de dados ''query_field_name'' da consulta ''cognos_query'', token inválido " " localizado após "running-total ( [total] for period( _add_months( [date]; -12) ,". CAF-WRN-2082 Ocorreu um erro. Entre em contato com o administrador. O CAF registrou o erro completo no log com o SecureErrorID:2021-01-18-12:53:54.959-#891
Edit 3:
I'm starting to think that this is some kind of configuration that is'nt right and dont let me use dimentional functions... Does anyone know something about that?

moving-total would be what you would want. It would require that your report is ordered.
Running total for period might be what you want.
running-total ( QUANTITY
for
Period ( _add_months ( order_day_date, -12) , order_day_date)
)

Related

How to Automate SQL code using Python in efficient way

I have a sql code which looks like this:
Database: RedShift
WITH
X as
(
SELECT distinct pn , pg , ic, sr , cm, fq , m1 , m2 , m3 , m4
FROM table1 ORDER BY 1,2,3
),
table2 AS
(
Select g,p,t , avg(ss) as ss , avg(ce) as ce , sum(av) as ps
from
(
select distinct ic AS g , pn AS p , cm AS t , ss , cast((sum_m1/nullif(sum_m2,0)) as decimal(3,2)) as
ce , av
from
(
select *
, cast((sum(m3) over (partition by ic, pn,cm)) as decimal) as ss
, sum(m1) over (partition by ic, pn,cm) as sum_m1
, sum(m2) over (partition by ic, pn,cm) as sum_m2
, cast((avg(m2) over (partition by ic, pn,cm)) as decimal) as av
from X
ORDER BY 1,2,3
)
order by 1,2,3
)
where ss is not null
group by 1,2,3
order by 1,2,3
)
The group by value g,p,t changes every time and so it creates table for every new combination of g,p,t values.
One way to automate is dump this sql code in Python which might be inefficient:
Here is my approach-> I replace all values in list by {} braces
Example say:
I store all possible group by values in a list.
G=[g1,g2,g3]
P=[p1,p2,p3]
T=[t1,t2,t3]
Connection to databse:
c= psycopg2.connect(database=db,host=host,port=port,user=user,password=password,sslmode='require')
data2={}
for g in G:
for p in P:
for t in T:
sqlstr=( """ WITH
X as
(
SELECT distinct pn , pg , ic, sr , cm, fq , m1 , m2 , m3 , m4
FROM table1 ORDER BY 1,2,3
),
table2 AS
Select {},{},{} , avg(ss) as ss , avg(ce) as ce , sum(av) as ps
from
(
select distinct ic AS g , pn AS p , cm AS t , ss , cast((sum_m1/nullif(sum_m2,0)) as decimal(3,2)) as
ce , av
from
(
select *
, cast((sum(m3) over (partition by ic, pn,cm)) as decimal) as ss
, sum(m1) over (partition by ic, pn,cm) as sum_m1
, sum(m2) over (partition by ic, pn,cm) as sum_m2
, cast((avg(m2) over (partition by ic, pn,cm)) as decimal) as av
from X
ORDER BY 1,2,3
)
order by 1,2,3
)
where ss is not null
group by 1,2,3
order by 1,2,3
), select * from table2 """).format(g,p,t)
data2[g+"_"+p+"_"+t] = pd.read_sql_query(sqlstr, c)
Is there any better way to pass parameters as in above code sequence of {} should be maintained to pass parameters in order ??
Can we use some other approach other than SQL? In pythonic way?

Datevalue calculation in MDX query

I want to create a dynamic query which updates each day.
So to filter on todays report I use
[Report Date].[Report Date].&[4226]
The 4226 is coming from:
=DATEVALUE("28-07-2017")-38718 or =TODAY()-38718 (convert to number)
38718 is just an arbitrary number to get the correct date from the cube.
EDIT:
Here is my current query:
SELECT NON EMPTY { [Measures].[Price FC] } ON COLUMNS
FROM ( SELECT ( -{ [Agency].[Nationality - Consortium - Agency].[Nationality].&[111],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[116],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[242],
[Agency].[Nationality - Consortium - Agency].[Nationality].&[134] } ) ON COLUMNS
FROM ( SELECT ( { StrToMember("[Report Date].[Report Date].&[" + Str(DateValue(Format(Now(), "dd-MM-yyyy")) - 38718) + "]") } ) ON COLUMNS
FROM ( SELECT ( { [Market].[Market].[Market].&[103] } ) ON COLUMNS
FROM ( SELECT ( { [Travel Type].[Travel Type].&[101],
[Travel Type].[Travel Type].&[102],
[Travel Type].[Travel Type].&[103] } ) ON COLUMNS
FROM ( SELECT ( { [Departure Date].[Year].&[2017] } ) ON COLUMNS
FROM [Booking])))))
WHERE ( [Departure Date].[Year].&[2017],
[Travel Type].[Travel Type].CurrentMember,
[Market].[Market].[Market].&[103],
StrToMember("[Report Date].[Report Date].&[" + Str(DateValue(Format(Now(), "dd-MM-yyyy")) - 38718) + "]") )
But it says that there is no column detected in the statement. I have also tried different date formats, any ideas?
Following the tips from this thread:
VBA Date as integer
I used CDbl instead of Datevalue, which gave me the desired result!
StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]")

Excel Query - invalid number of parameter and invalid descriptor index errors

I work in financial deparment of a company and want to extract data from our server (SQL) to put together reports for Board.
So I have several excel file where I use excel query to retreive data and make presentations.
I have been upgrading my querys and hit an obstacle with this one.
It was working fine with this bit of code:
SELECT
INTERNAL_REFERENCE as ref,
CMP_CODE AS CMP_CODE,
COUNTERPARTY_CODE AS BANK_CODE,
TRANSACTION_CODE AS TRANS_CODE,
CAST(CONVERT(varchar(10), AMO_END_DATE, 110) AS datetime) AS DATE,
SUM([AMORTIZATION]) AS AMOUNT
FROM
[SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE] inner join [SAGE_MTC_FRP].[dbo]. [LOANS]
on [SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE].LOAN_ID=[SAGE_MTC_FRP].[dbo]. [LOANS].LOAN_ID
WHERE
(AMO_END_DATE>=?) AND (BOOK_DATE<?) AND
(TRANSACTION_CODE<>'CPCA' AND TRANSACTION_CODE<>'CPCF' AND TRANSACTION_CODE<>'RENT') AND
IS_DELETED=0 AND VERSION_NUMBER=1 AND CMP_CODE='MTG'
GROUP BY
INTERNAL_REFERENCE, CMP_CODE, COUNTERPARTY_CODE, TRANSACTION_CODE, AMO_END_DATE
But when I put all code with the union all, it blows up with these 2x errors:
problem:
invalid number of parameter
invalid descriptor index
Code:
SELECT * FROM
(
SELECT
INTERNAL_REFERENCE as ref,
CMP_CODE AS CMP_CODE,
COUNTERPARTY_CODE AS BANK_CODE,
TRANSACTION_CODE AS TRANS_CODE,
CAST(CONVERT(varchar(10), AMO_END_DATE, 110) AS datetime) AS DATE,
SUM([AMORTIZATION]) AS AMOUNT
FROM
[SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE] inner join [SAGE_MTC_FRP].[dbo]. [LOANS]
on [SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE].LOAN_ID=[SAGE_MTC_FRP].[dbo]. [LOANS].LOAN_ID
WHERE
(AMO_END_DATE>=?) AND (BOOK_DATE<?) AND
(TRANSACTION_CODE<>'CPCA' AND TRANSACTION_CODE<>'CPCF' AND TRANSACTION_CODE<>'RENT') AND
IS_DELETED=0 AND VERSION_NUMBER=1 AND CMP_CODE='MTG'
GROUP BY
INTERNAL_REFERENCE, CMP_CODE, COUNTERPARTY_CODE, TRANSACTION_CODE, AMO_END_DATE
UNION ALL
SELECT
CL_CODE as ref
,LEFT([ACC_CODE] , 3) AS CMP_CODE
,[COUNTERPARTY_CODE] AS BANK_CODE
,RIGHT([CL_DESCRIPTION] , 3) AS TRANS_CODE
,CAST(CONVERT(varchar(10), [END_DATE], 110) AS datetime) AS DATE
,[CL_AMOUNT] AS AMOUNT
FROM
[SAGE_MTC_FRP].[dbo].[CREDIT_LINES]
WHERE
(END_DATE>?) AND
RIGHT([CL_DESCRIPTION] , 3)='PPC'
) AS DATA
ORDER BY REF
If both queries run by there self, you could use something like:
DECLARE #result TABLE(
[ref] NVARCHAR(50),
[CMP_CODE] NVARCHAR(50),
[BANK_CODE] NVARCHAR(50),
[TRANS_CODE] NVARCHAR(50),
[Date] DATE,
[AMOUNT] DECIMAL(10,2)
)
INSERT INTO #result
( ref ,
CMP_CODE ,
BANK_CODE ,
TRANS_CODE ,
Date ,
AMOUNT
)
SELECT
INTERNAL_REFERENCE as ref,
CMP_CODE AS CMP_CODE,
COUNTERPARTY_CODE AS BANK_CODE,
TRANSACTION_CODE AS TRANS_CODE,
CAST(CONVERT(varchar(10), AMO_END_DATE, 110) AS datetime) AS DATE,
SUM([AMORTIZATION]) AS AMOUNT
FROM
[SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE] inner join [SAGE_MTC_FRP].[dbo]. [LOANS]
on [SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE].LOAN_ID=[SAGE_MTC_FRP].[dbo]. [LOANS].LOAN_ID
WHERE
(AMO_END_DATE>=?) AND (BOOK_DATE<?) AND
(TRANSACTION_CODE<>'CPCA' AND TRANSACTION_CODE<>'CPCF' AND TRANSACTION_CODE<>'RENT') AND
IS_DELETED=0 AND VERSION_NUMBER=1 AND CMP_CODE='MTG'
GROUP BY
INTERNAL_REFERENCE, CMP_CODE, COUNTERPARTY_CODE, TRANSACTION_CODE, AMO_END_DATE
INSERT INTO #result
( ref ,
CMP_CODE ,
BANK_CODE ,
TRANS_CODE ,
Date ,
AMOUNT
)
SELECT
CL_CODE as ref
,LEFT([ACC_CODE] , 3) AS CMP_CODE
,[COUNTERPARTY_CODE] AS BANK_CODE
,RIGHT([CL_DESCRIPTION] , 3) AS TRANS_CODE
,CAST(CONVERT(varchar(10), [END_DATE], 110) AS datetime) AS DATE
,[CL_AMOUNT] AS AMOUNT
FROM
[SAGE_MTC_FRP].[dbo].[CREDIT_LINES]
WHERE
(END_DATE>?) AND
RIGHT([CL_DESCRIPTION] , 3)='PPC'
SELECT * FROM #result ORDER BY ref
I realize this is ancient but ran across the problem today.
To test, I removed the parameter and found that it was a security issue. I granted the user execute permission to the SQL Procedure, and it worked without the parameter. When I re-introduced the parameter, it continued working.
I'm far from a security expert, so I'm sure there's a better way to grant access, but this got me past the problem for now. Feel free to suggest a better solution.

Ingres nested query

I try this:
select fielda from tableA A
left join (select
fieldB
from tableB
) B where A.fielda = B.fieldB
I have this error :
Une erreur s'est produite lors de l'exécution de la requête.
ERROR [42500] [CA][Ingres ODBC Driver][Ingres]Table 'select' does not exist or is not owned by you.
INFORMATIONS SUPPLÉMENTAIRES :
ERROR [42500] [CA][Ingres ODBC Driver][Ingres]Table 'select' does not exist or is not owned by you. (CAIIOD35.DLL)
How can I do?
Thank you !!
The correct syntax would use ON rather than WHERE following the subselect.
SELECT a.fielda from tableA a
LEFT JOIN (
SELECT fieldB
FROM tableB
) b ON b.fielda = b.fieldB

Subquery returns more than 1 row - Mysql with Visual Studio 2010

I have the next code in mysql:
("SELECT id_viaje,
(SELECT nombre
FROM unidades,
viaje
WHERE id_unidad = id_unidades),
(SELECT nombre
FROM empleados,
viaje
WHERE id_empleado = id_conductor),
(SELECT nombre
FROM empleados,
viaje
WHERE id_empleado = id_guarda),
(SELECT nombre
FROM ciudad,
viaje
WHERE id_ciudad = id_salida),
(SELECT nombre
FROM ciudad,
viaje
WHERE id_ciudad = id_llegada),
fecha_salida,
fecha_llegada
FROM viaje; ")
I have tried LIMIT 1 at the end of each one, I have replaced = with IN and i have no idea why is showing me this error:
22:30:30 SELECT ID_Viaje, (select Nombre from unidades, viaje where ID_Unidad IN (ID_Unidades)) , (select Nombre from empleados, viaje where ID_Empleado IN (ID_Conductor)) ,(select Nombre from empleados, viaje where ID_Empleado IN (ID_Guarda)) , (select Nombre from ciudad, viaje where ID_Ciudad IN (ID_Salida)) , (select Nombre from ciudad, viaje where ID_Ciudad IN (ID_Llegada)) , Fecha_Salida, Fecha_Llegada FROM viaje limit 1
Error Code: 1242 Subquery returns more than 1 row
Please I need help very quickly!
you need to use in operator when subQuery returns more than one row.But in your case the sub query is also a problem.Please try to use what suggested by Rachcha
This error is returned when your subquery returns multiple rows when it is supposed to return a single row.
Your subquery logic is completely wrong. When you put a subquery inside a SELECT statement, you must be sure that the subquery will always return zero or one row.
What you can do now is putting a LEFT JOIN and use a few aliases in your query, like this:
SELECT id_viaje,
u.nombre AS unidade_nombre,
e_con.nombre AS conductor_nombre,
e_grd.nombre AS guarda_nombre,
c_salida.nombre AS salida_nombre,
c_llegada.nombre AS llegada_nombre,
fecha_salida,
fecha_llegada
FROM viaje v
LEFT JOIN unidades u ON v.id_unidad = u.id_unidades
LEFT JOIN empleados e_con ON e.id_empleado = v.id_conductor
LEFT JOIN empleados e_grd ON e.id_empleado = v.id_guarda
LEFT JOIN ciudad c_salida ON e.id_salida = c_salida.id_ciudad
LEFT JOIN ciudad c_llegada ON e.id_llegada = c_llegada.id_ciudad
;
Try running this in MySQL Workbench first and only if it runs successfully there then you should put it into your application code.
Also note that my code is not supposed to run in the first attempt. Please make any applicable corrections before running this code.
Read more about MySQL joins and MySQL subquery

Resources