DateWise Query with sum count in Oracle - oracle

Below is the one is my actual resulset in oracle database
TIMESTAMP SUCESS FAILURE
26-01-2017 1 0
31-01-2017 0 1
If i select from 26-01-2017 to 31-01-2017 .Query has to return like this below
expected resultset
Timestamp 26-01-2017 27-01-2017 28-01-2017 29-01-2017 30-01-2017 31-01-2017
Sucess 1 0 0 0 0 0 |
Failure 0 0 0 0 0 0
Please can anyone give me suggestions to write logic for above expected resultset?

You would need a PIVOT (I made an assumption that you always have either success or failure):
select * from (
select decode(success, 1, 'success', 'failure') as res_name,
success+failure as res,
to_char(time_stamp, 'DD-MM-YYYY') ts
from your_table)
pivot (max(res) for ts in ('26-01-2017', '27-01-2017', '28-01-2017', '29-01-2017', '30-01-2017', '31-012017'))
List of columns is always defined up front, so if you need a variable list of columns you need either generate above query or use PIVOT XML. With PIVOT XML you can use subquery instead of predefined list of variables, but you get XML back.

Related

How do I create a ID based on multiple values in different columns in Power Query?

I am trying to create an ID based in multiple values in different columns in Power query.
The idea is to check the following values:
IF
ID_STORE = 1
ID_PRODUCT = 1
ID_CATEGORY = 1
SALE_DATE = 01/01/2018
ID_COSTUMER = 1
THEN CREATE THE SAME ID FOR THE ROWS THAT HAVE THIS INFO.
The idea is to check the rows that have that info (1 and 01/01/2018) in multiple columns (ID_STORE, ID_PRODUCT, ID_CATEGORY etc..).
Thanks in advance.
Obs: This is my first post, so feel free to correct me in any manner.
You need to add an 'AND' clause to your 'OF' statement;
if [ID_STORE] = 1
and [ID_PRODUCT] = 1
and [ID_CATEGORY] = 1
and [SALE_DATE] = Text.ToDate("01/01/2018")
and [ID_COSTUMER] = 1 then
1 else 0

Number of trailing zeros in a number

I have a column MONTHLY_SPEND in the table with data type of NUMBER. I am trying to write a query which will return number of zeros in the column.
e.g..
1000 will return 3
14322 will return 0
1230 will return 1
1254000.65 will return 0
I tried using mod operator and 10 but without the expected result. Any help is appreciated. Please note that database is Oracle and we can't create procedure/function.
select nvl(length(regexp_substr(column, '0+$')), 0) from table;
Here is one way to find
create table spend
(Monthly_spend NUMBER);
Begin
insert into spend values (1000)
insert into spend values (14322)
insert into spend values (1230)
insert into spend values (1254000.65)
End;
This query will for this data :
select Monthly_spend,REGEXP_COUNT(Monthly_spend,0)
from spend
where Monthly_spend not like '%.%' ;
if have one more data like 102 and if it should be zero , then try below query:
select Monthly_spend,case when substr(Monthly_spend,-1,1)=0 THEN REGEXP_COUNT(Monthly_spend,0) ELSE 0 END from spend;
Here is final query for value like 2300120 or 230012000
select Monthly_spend,
case when substr(Monthly_spend,-1,1)=0 and REGEXP_COUNT(trim (0 from Monthly_spend),0)<=0 THEN REGEXP_COUNT(Monthly_spend,0)
when REGEXP_COUNT(trim (0 from Monthly_spend),0)>0 THEN LENGTH(Monthly_spend) - LENGTH(trim (0 from Monthly_spend))
ELSE 0 END from spend;
Output :
1000 3
1254000.65 0
14322 0
1230 1
102 0
2300120 1
230012000 3
You can try this, a simple solution.
select length(to_char(col1))-length(rtrim(to_char(col1), '0')) no_of_trailing_zeros from dual;
select length(to_char('123.120'))-length(rtrim(to_char('123.120'), '0')) no_of_trailing_zeros from dual;

Using oracle loop to concatanete strings

I have someting like this
id day descrition
1 1 hi
1 1 today
1 1 is a beautifull
1 1 day
1 2 exemplo
1 2 for
1 2 this case
I need to do a funtion that for each day concatenate the descrtiomn colunm and return the result like this
id day descrition
1 1 hi today is a beautifull thay
1 2 exemplo for this case
Anny ideia about how can i do this usisng a loop in a function in oracle
You need a way of determining which order the values should be aggregated. The snippet below will rely on the implicit order in which Oracle reads the rows from the datafiles - if you have row movement enabled then you may get inconsistent results as the rows can be read in different orders as they are relocated in the underlying datafiles.
SELECT LISTAGG( description, ' ' ) WITHIN GROUP ( ORDER BY ROWNUM ) AS description
FROM your_table
GROUP BY id, day
It would be better to have another column that stores the order within each day.

How do you filter a measure in MDX and output only a single row for the measure?

Given the MDX:
select {[Measures].[Effort], [Measures].[Count]} on columns from [Tickets]
.. How can zero (0) values for [Measures].[Effort] be filtered out from the [Measures].[Count] so that the resulting [Measures].[Count] value is reduced by the number of "Tickets" with zero (0) effort?
One would think that it would be easy to filter out values, however that's not the case. The following does not reduce the count of course because the final, single value output is naturally greater than zero (0):
select {[Measures].[Effort], FILTER([Measures].[Count], [Measures].[Effort] > 0 )} on 0
from [Tickets]
.. Also, please assume millions of tickets so placing a ticket ID on axis 1 and then filtering and then summing after the MDX result is returned would not be performant
If the performance is a matter and the following query is too slow:
With
Member [Measures].[RealCount] as
SUM(
IIF(
[Measures].[Effort] > 0,
[Measures].[Count],
Null
)
)
Select
{[Measures].[Effort],[Measures].[Count],[Measures].[RealCount]} on 0
From [Tickets]
You have to filter it out on the DWH to pre-calculate a real count.
I'm not sure of your ticket hierarchy structure so will guess that bit but I would imagine something along these lines:
WITH MEMBER [Measures].[RealCount] AS
SUM(
[Ticket].[Ticket].[Ticket Id],
Iif(
[Measures].[Effort] > 0
,1
,NULL
)
)
SELECT
{
[Measures].[Effort]
,[Measures].[Count]
,[Measures].[RealCount]
} on 0
FROM [Tickets];
If the above gives the correct result then it can be further improved by moving some of the logic to the cube script - this bit:
CREATE HIDDEN SumTicker;
[Measures].[SumTicker] = Iif([Measures].[Effort] > 0,1,NULL);
NON_EMPTY_BEHAVIOR([Measures].[SumTicker]) = [Measures].[Effort];
Then the script becomes:
WITH MEMBER [Measures].[RealCount] AS
SUM(
[Ticket].[Ticket].[Ticket Id],
[Measures].[SumTicker]
)
SELECT
{
[Measures].[Effort]
,[Measures].[Count]
,[Measures].[RealCount]
} on 0
FROM [Tickets];

Passing dynamic values to order records in oracle

I want to sort a record in following way.
Arrange records in group (by ID column)
Sort the step 1 results by ascending order (by NAME column)
2.1. If NAME Column having same values, then order by FLAG column value (ascending order)
Order the Step 2 results by Order Assist column (I will be passing dynamic value to sort using order assist column)
My Query:
SELECT IDENTIFIER, CODE, INC_EXC_FLAG,ORDER_ASSIST FROM DUMMY_SORT
WHERE METHOD_ID = '1'
GROUP BY (IDENTIFIER, CODE, INC_EXC_FLAG,ORDER_ASSIST)
ORDER BY ORDER_ASSIST ASC, CODE ASC, INC_EXC_FLAG ASC
Result of above Query:
ID NAME FLAG ORDER_ASSIST
A_EC AEC 0 EC1
B_EC_DET BEC 1 EC2
A_NIT ANIT 0 NIT1
A_NIT ANIT 1 NIT1
A_NIT BNIT 0 NIT1
B_NIT_DET BNIT 0 NIT2
B_NIT_DET BNIT 1 NIT2
A_SC ASC 0 SC1
A_SC ASC 1 SC1
B_SC_DET BSC 0 SC2
B_SC_DET BSC 1 SC2
C_SC_FUN CSC 0 SC3
D_SC_GRP DSC 0 SC4
But I want to generate the result according to dynamic values of order_assist
For Example:
If I am passing dynamic value as "SC" i want to fisrt order the records SC1,SC2,SC3. Then NIT1,NIT2 . then EC1,EC2.
If I am passing dynamic value as "NITG" i want to fisrt order the records NIT1,NIT2 then SC1,SC2,SC3. then EC1,EC2.
Expected result added when dynamic value is "SC"
ID NAME FLAG ORDER_ ASSIST
A_SC ASC 0 SC1
A_SC ASC 1 SC1
B_SC_DET BSC 0 SC2
B_SC_DET BSC 1 SC2
C_SC_FUN CSC 0 SC3
D_SC_GRP DSC 0 SC4
A_NIT ANIT 0 NIT1
A_NIT ANIT 1 NIT1
A_NIT BNIT 0 NIT1
B_NIT_DET BNIT 0 NIT2
B_NIT_DET BNIT 1 NIT2
A_EC AEC 0 EC1
B_EC_DET BEC 1 EC2
Sounds like maybe you're after something like:
order by case when p_sort_param = 'SC' and order_assist like 'SC%' then 1
when p_sort_param = 'SC' and order_assist like 'NIT%' then 2
when p_sort_param = 'NITG' and order_assist like 'NIT%' then 1
when p_sort_param = 'NITG' and order_assist like 'SC%' then 2
else 3
end,
order_assist
where p_sort_param is the parameter that gets passed in to provide the "dynamic" value. This assumes you're running the query via a stored procedure. If it's a manually run query (eg. in Toad), then add a colon in front of the parameter name to make :p_sort_param.
I cannot understand your specific ordering rules, but you should be able to achieve what you want using CASE expressions:
order by
case order_assist
when 'SC' then <first thing to order by for SC>
when 'NITG' then <first thing to order by for NITG>
...
end,
case order_assist
when 'SC' then <second thing to order by for SC>
when 'NITG' then <second thing to order by for NITG>
...
end,
... etc.

Resources