I need to get a query to get the below data:
to display like below:
basically adding a new column Vehicle_size for each value that is listed in the corresponding column. for example.
kay_SM = Vehicle_size(Small)
kay_LG= Vehicle_size(Large)
Lola_SM= Vehicle_size(Small)
lola_LG= Vehicle_size(Large)
at the same time create a new column:competitor, identifying if it is either for "lola" or "kay"
You can use unpivot in this case.
SELECT U.RENTAL_DATE,U.OUTBOUND,U.INBOUND,
REGEXP_SUBSTR(VEHICLE_COMPETITIOR, '[^_ ]+', 1, 2) AS VEHICLE_SIZE,
REGEXP_SUBSTR(VEHICLE_COMPETITIOR, '[^_ ]+', 1, 1) AS COMPETITIOR,
RATE
FROM T
UNPIVOT (
RATE FOR VEHICLE_COMPETITIOR IN (KAY_SM AS 'Kay_Small',
KAY_LG AS 'Kay_Large',LOLA_SM AS 'Lola_Small' ,LOLA_LG AS 'Lola_Large')
) U
Related
I'm hoping to create a measure of distinct count of a customer column, on the condition if customers in this column does not exist in another table's customer column.
I know I can create a calculated column checking if the customer exists, and then use the calculate function filtering out those who do exist. But is it possible to achieve this without creating the calculated column?
Please note this is in Power Pivot, not Power BI so I can't really use 'treatas' or 'in'. Thanks a lot.
Assuming tables named Table1 and Table2:
MyMeasure :=
VAR T2Customer =
VALUES( Table2[Customer] )
RETURN
CALCULATE(
DISTINCTCOUNT( Table1[Customer] ),
NOT (
CONTAINSROW(
T2Customer,
Table1[Customer]
)
)
)
Yes, You can achieve it using EXCEPT()function:
Let's say that we have 2 tables like this:
Customer_Table1:
Customer_Table2:
Now we can use this measure to achieve our result:
CountOfDistinctCusts =
COUNTROWS (
EXCEPT (
VALUES ( Customer_Table1[Customer] ),
VALUES ( Customer_Table2[Customer] )
)
)
If we test the code:
I have a matrix table grouped by WBS and Acc at row level.
Then i have a column group "Table" with Plan & Actuals. What i want to get is (Plan)-(Actuals)
I used the below expression in a outer group column
=SUM(IIF(Fields!Table.Value = "Plan", (Fields!Total.Value),0))-SUM(IIF(Fields!Table.Value = "Actuals", (Fields!Total.Value),0))
The total.value is the total of plan and actuals individually.
I am getting an error in the calculated column (Expression)
The Matrix TableThe Matrix Table
The Dataset and Outputs (expected and current)
If you only ever have two fixed values in your Table column then you should be able to do this without the need for custom code.
Something like this...
=SUM(IIF(Fields!Table.Value = "Plan", Fields!Total.Value,0))
-
SUM(IIF(Fields!Table.Value = "Actuals", Fields!Total.Value,0))
UPDATE for clarity
I have created a report based on your sample data and the above works as expected.
I created a dataset using the following query
DECLARE #t TABLE (WBSNumbers varchar(20), [Table] varchar(20), Account Varchar(20), Total float)
INSERT INTO #t
SELECT 'xxx', 'Plan', 'Capital', 96875 UNION ALL
SELECT 'xxx', 'Plan', 'Expense', 40625 UNION ALL
SELECT 'xxx', 'Actuals', 'Capital', 229949 UNION ALL
SELECT 'xxx', 'Actuals', 'Expense', 2848 UNION ALL
SELECT 'yyy', 'Actuals', 'Expense', 0
SELECT * FROM #t
I added a matrix with row groups for WBSNumbers and Account and a column group for Table.
I added a new column outside the column group with the expression stated above
I added an extra row for the headers but this was not really required - just to make it match the expected output more closely.
The final design looks like this.
The final output looked like this...
Other than the fact that the sample data does not match the supplied expected output for ABS yyyy this works as expected.
I am trying to count all rows that meet a criteria, and then also have it list sequence numbers for each row so we can quickly identify those meeting the criteria. Ex: We know 5 meet criteria of Complete Response and we want to know there are 5 but also what those sequence numbers are. How would i write that in my query?
select
count(sf.protocol_subject_id) as responseCount, sf.BEST_RESPONSE
from oncore.sv_sub_followup sf
where sf.protocol_no = $P{pPclNo} and sf.best_response is not null
group by sf.best_response
order by decode(best_response, 'Complete Response', 1, 'Partial Response', 2, 'Stable', 3, 'Progressive', 4, 'Not Evaluable', 5)
I should also mentioned that this is one of 9 subreports compiling a larger report. Not sure if that makes a difference when trying to do this. I'm very new to pl/sql. Thanks in advance for any help.
If I got this right, you need to count the rows for each BEST_RESPONSE for a given PROTOCOL_NO, and also list the IDs (protocol_subject_id) of those rows.
You could use the analityc function LISTAGG, that concatenates field values over a group. Something like this:
SELECT
best_response,
COUNT(protocol_subject_id) AS responsecount,
LISTAGG(protocol_subject_id, ', ') WITHIN GROUP (
ORDER BY
1
) matching_rows_ids
FROM
sv_sub_followup
WHERE
protocol_no = :protocol_no
AND best_response IS NOT NULL
GROUP BY
best_response
ORDER BY
decode(best_response, 'Complete Response', 1, 'Partial Response', 2,
'Stable', 3, 'Progressive', 4, 'Not Evaluable',
5);
You can use a subquery to obtain your results like the below
SELECT responsecount,
sf1.best_response,
sf1.protocol_subject_id
(
select count(sf.protocol_subject_id) AS responsecount,
sf.best_response
FROM oncore.sv_sub_followup sf
WHERE sf.protocol_no = $p{ppclno}
AND sf.best_response IS NOT NULL
GROUP BY sf.best_response)rec,
oncore.sv_sub_followup sf1
WHERE sf1.protocol_no=$p{ppclno}
AND sf1.best_response=rec.best_response
ORDER BY instr('Complete Response,Partial Response,Stable,Progressive,Not Evaluable',sf1.best_response);
I have a URI column coming in a log. I have to parse it and remove the certain parts from it and store it in a table. For Example if I have /v7/cp/members/~PERF1SP826T90869AN/options, then I have to store it as /v7/cp/members/*/options. Can I do that using REGEXP_REPLACE?
Also I would like to see if I could store that part that I removed from the URI as another column?
For Example from /v7/cp/members/~PERF1SP826T90869AN/options, I should store /v7/cp/members/*/options as a column and PERF1SP826T90869AN in a separate column.
If you are using Oracle, here's a method:
SQL> with tbl(str) as (
select '/v7/cp/members/~PERF1SP826T90869AN/options' from dual
)
select regexp_replace(str, '(.*?)(/|$)', '*/', 1, 5) as replaced,
regexp_substr(str, '(.*?)(/|$)', 1, 5, NULL, 1) as fifth_element
from tbl;
REPLACED FIFTH_ELEMENT
------------------------ -------------------
/v7/cp/members/*/options ~PERF1SP826T90869AN
SQL>
Here's my original question:
merging two data sets
Unfortunately I omitted some intircacies, that I'd like to elaborate here.
So I have two tables events_source_1 and events_source_2 tables. I have to produce the data set from those tables into resultant dataset (that I'd be able to insert into third table, but that's irrelevant).
events_source_1 contain historic event data and I have to do get the most recent event (for such I'm doing the following:
select event_type,b,c,max(event_date),null next_event_date
from events_source_1
group by event_type,b,c,event_date,null
events_source_2 contain the future event data and I have to do the following:
select event_type,b,c,null event_date, next_event_date
from events_source_2
where b>sysdate;
How to put outer join statement to fill the void (i.e. when same event_type,b,c found from event_source_2 then next_event_date will be filled with the first date found
GREATLY APPRECIATE FOR YOUR HELP IN ADVANCE.
Hope I got your question right. This should return the latest event_date of events_source_1 per event_type, b, c and add the lowest event_date of event_source_2.
Select es1.event_type, es1.b, es1.c,
Max(es1.event_date),
Min(es2.event_date) As next_event_date
From events_source_1 es1
Left Join events_source_2 es2 On ( es2.event_type = es1.event_type
And es2.b = es1.b
And es2.c = es1.c
)
Group By c1.event_type, c1.b, c1.c
You could just make the table where you need to select a max using a group by into a virtual table, and then do the full outer join as I provided in the answer to the prior question.
Add something like this to the top of the query:
with past_source as (
select event_type, b, c, max(event_date)
from event_source_1
group by event_type, b, c, event_date
)
Then you can use past_source as if it were an actual table, and continue your select right after the closing parens on the with clause shown.
I end up doing two step process: 1st step populates the data from event table 1, 2nd step MERGES the data between target (the dataset from 1st step) and another source. Please forgive me, but I had to obfuscate table name and omit some columns in the code below for legal reasons. Here's the SQL:
INSERT INTO EVENTS_TARGET (VEHICLE_ID,EVENT_TYPE_ID,CLIENT_ID,EVENT_DATE,CREATED_DATE)
select VEHICLE_ID, EVENT_TYPE_ID, DEALER_ID,
max(EVENT_INITIATED_DATE) EVENT_DATE, sysdate CREATED_DATE
FROM events_source_1
GROUP BY VEHICLE_ID, EVENT_TYPE_ID, DEALER_ID, sysdate;
Here's the second step:
MERGE INTO EVENTS_TARGET tgt
USING (
SELECT ee.VEHICLE_ID VEHICLE_ID, ee.POTENTIAL_EVENT_TYPE_ID POTENTIAL_EVENT_TYPE_ID, ee.CLIENT_ID CLIENT_ID,ee.POTENTIAL_EVENT_DATE POTENTIAL_EVENT_DATE FROM EVENTS_SOURCE_2 ee WHERE ee.POTENTIAL_EVENT_DATE>SYSDATE) src
ON (tgt.vehicle_id = src.VEHICLE_ID AND tgt.client_id=src.client_id AND tgt.EVENT_TYPE_ID=src.POTENTIAL_EVENT_TYPE_ID)
WHEN MATCHED THEN
UPDATE SET tgt.NEXT_EVENT_DATE=src.POTENTIAL_EVENT_DATE
WHEN NOT MATCHED THEN
insert (tgt.VEHICLE_ID,tgt.EVENT_TYPE_ID,tgt.CLIENT_ID,tgt.NEXT_EVENT_DATE,tgt.CREATED_DATE) VALUES (src.VEHICLE_ID, src.POTENTIAL_EVENT_TYPE_ID, src.CLIENT_ID, src.POTENTIAL_EVENT_DATE, SYSDATE)
;