How to create a view from PIVOT SQL - view

Our Employee benefits have 5 Plan_Types, I need to put all the data into a view that looks like this:
`EMPLID YEAR SK PB VTO BV CT VC
-----------------------------------------------------
0199990 2017 23 22 5 0 169
0000004 2018 22 0 2 5 65
0199990 2017 5 34 34 0 55
0000004 2018 23 0 19 5 0
----------------------------------------------------
`
Here is the SQL for the above pivot table
`SELECT * FROM (SELECT b.emplid,
b.empl_rcd,
EXTRACT (YEAR FROM B.ACCRUAL_PROC_DT)
AS year,
DECODE (b.plan_type,
'50', 'SK',
'52', 'PB',
'5V', 'VTO',
'5Y', 'BV',
'5Z', 'CT',
'51', 'VC')
BANK,
B.HRS_CARRYOVER
+ B.HRS_EARNED_YTD
- B.HRS_TAKEN_YTD
+ B.HRS_ADJUST_YTD
+ B.HRS_BOUGHT_YTD
- B.HRS_SOLD_YTD
- B.HRS_TAKEN_UNPROC
+ B.HRS_ADJUST_UNPROC
+ B.HRS_BOUGHT_UNPROC
- B.HRS_SOLD_UNPROC
BALANCE
FROM ps_leave_accrual b)
PIVOT (SUM (balance) AS bal
FOR (bank)
IN ('SK', 'PB', 'VTO', 'BV', 'CT', 'VC'))
WHERE emplid in ('0199990','0000004');`
How do I turn this into a view I can use in PS Query. If I put this code into the view SQL, It fails at the Pivot point - "SQL command not properly ended "

To use non-standard code in views, we have different options:
We create the record definition and in the SQL we put the SQL, but comment it out, and reference our migration package information. When we build, we build manually as part of the migration process. I've seen folks use DMS to build the views.
We can create a SQL Object and reference it in the view sql using %SQL(M_CUSTOM_VIEW_SQL), that way the code is migrated with the project.

Related

Error executing view: Function count is not executable in denodo

I have a table with few columns as shown below. I would like to get a count of all the records per week level but I am unable to count it. I could also use group by but I do not want to do that because it gives me too many records. I use denodo and oracle 18g.
s_id sub_id week year st_id
24hifew njfhwf 50 2020 ew1eer
939hjefbw newfkhwfe 34 2019 e3eef3
hewfhwe23 67832ghef 44 2018 ewfwf1
Code:
select
xx.s_id,
xx.sub_id,
xx.st_id,
yy.week,
yy.year,
count(*) OVER ( PARTITION BY yy.year, yy.week,xx.s_id,xx.sub_id xx.st_id) as week_l
from xx as xx left join yy as yy
Basically, I am looking for an equivalent query to partition by which will run fine.
Error:
finished with error: Error executing view: Function count is not executable

Calculate last year group by ID with DAX

I have a table in SSAS Tabular Model like this
Table 1
ID END DATE
1 06/24/2016
1 06/24/2017
1 06/24/2018
2 08/08/2017
2 08/08/2016
3 12/12/2015
I would like to create a Mesure in DAX, in another related Table. The output should be this:
Table 2
ID MAXYEAR
1 2018
1 2018
1 2018
2 2017
2 2017
3 2015
PLEASE !!! WITHOUT USING EARLIER. Because my model is very large, and canĀ“t use this function.
Create a relationship between the 2 tables, assuming that Table 2
contains unique values for ID.
Create a year column from end date
Year = year([END DATE])
After that, in Table 2 create a calculated column with the following code:
MaxYear = CALCULATE(max('Table'[Year]))
Table 2 should look like this

How to complete report derived from another query with zeros or nulls

So, I'm really having a hard time with a report.
I need a report grouped by year. For example, we want to show how many cars are sold per year. So, the report has a column Year, the type/model of the car, and the quantity. However, I also want to show a row with null/zero value, so even when no car of a specific type was sold, I want it to show the row, but with 0.
The problem is, this query is based on a lot of views, which shows each transaction. So, my actual query works fine except it doesn't show a type when none of that type was sold in a year.
When I pivot this report using Oracle APEX, it almost works. It shows all the types, but if I filter per year, then they are gone.
I have all the years I need, but I don't have the data for that year. I take all the data from multiple views with the specifics of the sales. Some of the models/types were not sold in some years, so when I query the report, it doesn't show up, which is expected. For example:
What I get is
//YEAR - MODEL - QUANTITY //
2018 - MODEL 1 - 300
2018 - MODEL 2 - 12
2017 - MODEL 1 - 12
2017 - MODEL 2 - 33
2017 - MODEL 3 - 22
What I want
//YEAR - MODEL - QUANTITY //
2018 - MODEL 1 - 300
2018 - MODEL 2 - 12
2018 - MODEL 3 - 0
2017 - MODEL 1 - 12
2017 - MODEL 2 - 33
2017 - MODEL 3 - 22
Any ideas?
You can conjure rows, and outer join to them.
with years as (
select add_months(date '1980-1-1', (rownum-1)*12) dt
from dual
connect by level < 5
)
select y.dt, count(e.hiredate)
from scott.emp e
right outer join years y
on y.dt = trunc(e.hiredate,'yy')
group by y.dt
DT COUNT(E.HIREDATE)
------------------- -----------------
01-01-1982 00:00:00 1
01-01-1983 00:00:00 0
01-01-1981 00:00:00 10
01-01-1980 00:00:00 1

Event Study (Extracting Dates in SAS)

I need to analyse abnormal returns for an event study on mergers and acquisitions.
** I would like to analyse abnormal returns to acquirers by using event windows. Basically I would like to extract the prices for the acquirers using -1 (the day before the announcement date), announcement date, and +1 (the day after the announcement date).**
I have two different datasets to extract information from.
The first is a dataset with all the merger and acquisition information that has the information in the following format:
DealNO AcquirerNO TargetNO AnnouncementDate
123 abcd Cfgg 22/12/2010
222 qwert cddfgf 26/12/1998
In addition, I have a 2nd dataset which has all the prices.
ISINnumber Date Price
abcd 21/12/2010 10
abcd 22/12/2010 11
abcd 23/12/2010 11
abcd 24/12/2010 12
qwert 20/12/1998 20
qwert 21/12/1998 20
qwert 22/12/1998 21
qwert 23/12/1998 21
qwert 24/12/1998 21
qwert 25/12/1998 22
qwert 26/12/1998 21
qwert 27/12/1998 23
ISIN number is the same as acquirer no, and that is the matching code.
In the end I would like to have a database something like this:
DealNO AcquirerNO TargetNO AnnouncementDate Acquirerprice(-1day) Acquireeprice(0day) Acquirerprice(+1day)
123 abcd Cfgg 22/12/2010 10 11 12
222 qwert cddfgf 26/12/1998 22 21 23
Do you know how I can get this?
I'd prefer to use sas to run the code, but if you are familiar with any other programs that can get the data like this, please let me know.
Thank you in advance ^_^.
This can be done quite easily with PROC SQL and joining the PRICE dataset three times. Try this (assuming data set names of ANNOUCE and PRICE):
Warning: untested code
%let day='21DEC2010'd;
proc sql;
create table RESULT as
select a.dealno,
a.acquirerno,
a.targetno,
a.annoucementdate,
p.price as acquirerprice_prev,
c.price as acquirerprice_cur,
n.price as acquirerprice_next
from ANNOUCE a
left join (select * from PRICE where date = &day-1) p on a.acquirerno = p.isinumber
left join (select * from PRICE where date = &day) c on a.acquirerno = c.isinumber
left join (select * from PRICE where date = &day+1) n on a.acquirerno = n.isinumber
;
quit;

Hive: Joining two tables with different keys

I have two tables like below. Basically i want to join both of them and expected the result like below.
First 3 rows of table 2 does not have any activity id just empty.
All fields are tab separated. Category "33" is having three description as per table 2.
We need to make use of "Activity ID" to get the result for "33" category as there are 3 values for that.
could anyone tell me how to achieve this output?
TABLE: 1
Empid Category ActivityID
44126 33 TRAIN
44127 10 UFL
44128 12 TOI
44129 33 UNASSIGNED
44130 15 MICROSOFT
44131 33 BENEFITS
44132 43 BENEFITS
TABLE 2:
Category ActivityID Categdesc
10 billable
12 billable
15 Non-billable
33 TRAIN Training
33 UNASSIGNED Bench
33 BENEFITS Benefits
43 Benefits
Expected Output:
44126 33 Training
44127 10 Billable
44128 12 Billable
44129 33 Bench
44130 15 Non-billable
44131 33 Benefits
44132 43 Benefits
It's little difficult to do this Hive as there are many limitations. This is how I solved it but there could be a better way.
I named your tables as below.
Table1 = EmpActivity
Table2 = ActivityMas
The challenge comes due to the null fields in Table2. I created a view and Used UNION to combine result from two distinct queries.
Create view actView AS Select * from ActivityMas Where Activityid ='';
SELECT * From (
Select EmpActivity.EmpId, EmpActivity.Category, ActivityMas.categdesc
from EmpActivity JOIN ActivityMas
ON EmpActivity.Category = ActivityMas.Category
AND EmpActivity.ActivityId = ActivityMas.ActivityId
UNION ALL
Select EmpActivity.EmpId, EmpActivity.Category, ActView.categdesc from EmpActivity
JOIN ActView ON EmpActivity.Category = ActView.Category
)
You have to use top level SELECT clause as the UNION ALL is not directly supported from top level statements. This will run total 3 MR jobs. ANd below is the result I got.
44127 10 billable
44128 12 billable
44130 15 Non-billable
44132 43 Benefits
44131 33 Benefits
44126 33 Training
44129 33 Bench
I'm not sure if I understand your question or your data, but would this work?
select table1.empid, table1.category, table2.categdesc
from table1 join table2
on table1.activityID = table2.activityID;

Resources