Blog

Recursive SQL query to find the parent record or first and last or latest records

When we have a scenario to pick up first and  last record/hierarchy from a table which maintains history of changes(EmployeeHistory_Table).

 

Hierarchy_Table

role hierarchies
Trainee_System_Engineer
System_Engineer
Senior_Software_Eng
Team_Leader
Manager
Senior_Exec

 

EmployeeHistory_Table

emp_name current_role new_role
Dan Trainee_System_Engineer System_Engineer
Dan System_Engineer Senior_Software_Eng
Dan Senior_Software_Eng Manager
Tom System_Engineer Senior_Software_Eng
Tom Senior_Software_Eng Team_Leader
Jack System_Engineer Team_Leader
Jack Team_Leader Manager
Jack Manager Senior_Exec

 

expected output:

emp_name started_role current_role
Jack System_Engineer Trainee_System_Engineer
Tom System_Engineer Team_Leader
Dan Trainee_System_Engineer Manager

 

below is the sql for this kind of scenario:

CREATE TABLE #emp_RoleChanges (
emp_name VARCHAR(30),
current_role VARCHAR(30) ,
new_role VARCHAR(30)
);

INSERT INTO #emp_RoleChanges (emp_name , current_role , new_role )
VALUES
(‘Dan’, ‘Trainee_System_Engineer’, ‘System_Engineer’),
(‘Dan’, ‘System_Engineer’, ‘Senior_Software_Eng’),
(‘Dan’, ‘Senior_Software_Eng’, ‘Manager’),
(‘Tom’, ‘System_Engineer’, ‘Senior_Software_Eng’),
(‘Tom’, ‘Senior_Software_Eng’, ‘Team_Leader’),
(‘Jack’, ‘System_Engineer’, ‘Team_Leader’),
(‘Jack’, ‘Team_Leader’, ‘Manager’),
(‘Jack’, ‘Manager’, ‘Trainee_System_Engineer’)
;

select * from #emp_RoleChanges
;
WITH q AS (
SELECT 1 AS LEVEL, emp_name , current_role , new_role
FROM #emp_RoleChanges
UNION ALL
SELECT LEVEL + 1, q.emp_name, q.current_role, u.new_role
FROM q
INNER JOIN #emp_RoleChanges u ON u.current_role = q.new_role and u.emp_name = q.emp_name
)
–select * from q
–order by 2,1

SELECT –q.LEVEL,
q.emp_name , q.current_role started_role, q.new_role current_role
FROM q
where q.level = (select max(p.level) from q p where p.emp_name = q.emp_name)

Cognos – pass prompt value to SQL query

To pass a prompt value say – “p_reportdate” to sql query then syntax is as below
select * from table1 where table1.date =  #prompt(‘p_reportdate’)#

if the prompt- “p_name” is multi select then
select * from table2 where table2.name in (#promptmany(‘p_name’)# )
 

 

few other related articles
http://www-01.ibm.com/support/docview.wss?uid=swg21341148

How to dynamically name tabs in excel 2007

When we use page set, report output in excel will have tab names as page1, pag1_2, page1_3, so on..

If you want to display this tab names by report section names then follow below configuration methods.

In cognos configuration settings at Report service for each dispatcher add below advanced settings

Parameter column, type-  RSVP.EXCEL.PAGEGROUP_WSNAME_ITEMVALUE

Value column, type-  TRUE

Source: https://www.ibm.com/support/knowledgecenter/SSEP7J_10.2.2/com.ibm.swg.ba.cognos.ug_cra.10.2.2.doc/t_ug_cra_adv_prop_name_worksht_tabs_excel2007.html

 

Tableau- how to disable auto grouping in Tableau

There is no option as such in Tableau to disable auto grouping.
But there is work around.
two ways you could achieve this
1. Create index() and drag this as your first row item. Now you shouldn’t be seeing any grouping. To hide this field at index() drop down uncheck “show header”
2. Drag “Number of Records” measure as first row. Do a quick table calculation as — running total, sum, table down. This would give similar data like index(). And data is not grouped anymore. To hide this field uncheck “show header”

Tableau disable auto-grouping

.