Friday, August 29, 2014

Fact to Fact Join in Cognos Framework Manager Or Stitch Query?

What is a Stitch Query?

A stitch query is the name given to the type of query that IBM Cognos BI produces when a user creates a query that contains two or more fact tables. A stitch query consists of independent sub-queries for each fact that are then joined together on common dimensions. This joining process is done using an outer join to ensure no data from any fact sub-query is lost during the stitch.

Please see the below links:
http://www.ibm.com/developerworks/data/library/cognos/reporting/advanced_report_design/page605.html
http://blogs.perficient.com/ibm/2012/05/17/cognos-concepts-stitch-query-in-cognos-reporting-explained/

Friday, August 22, 2014

How to build multiselect prompt using promptmany for DMR and cubes

When building a report in Report Studio against a Cube or a DMR, if you modify the expression of a data item in 
a LIST or in a CROSSTAB to put macro like : 

#promp('pline', 'memberuniquename', '[RootMember]', '')# You will be able to be prompted to select a SINGLE value and submit it. But, if you wish to select multiple values and submit them all at once, you may create a prompt page, add a value prompt in the prompt page, set the 'Multi Select' property of the prompt to 'Yes' and modify the macro above to use promptmany() like the following : #prompmany('pline', 'memberuniquename', '[RootMember]', '')# 

But, this will fail with the following error message when you select more than one item and submit.

Modify the promptmany macro as follow :
#promptmany('pline', 'memberuniquename', '[RootMember]', 'set(', '', ')')#
That will add the MUNs of the items properly in the promptmany function.

Wednesday, August 6, 2014

Cognos Report Studio


1. What are the differences between a summary filter and a detail filter?
Detail filter:
When you use a detail filter to filter a report only on detail data items, none of the data items in a detail filter are summary data items. Examples of detail data items are product, order number, and customer name. You can filter detail data items that you select from a query or a catalog. A detail filter applies one or more specified conditions to each row of data. If the conditions are true for a row, the row is retrieved and appears in the report; if the conditions are not true, the row is not retrieved and does not appear in the report. 

While creating detail filter in Cognos Report Studio, there are two options as Before auto aggregation and After auto aggregation.
§  Before auto aggregation: 
This will apply filter to individual records in the data source. This will filter records before Aggregation. This is default option selected for all reports
§  After auto aggregation: 
This will filter records after aggregation. This option needs to be selected when we want to apply aggregation and then filter content instead of filtering individual record like Rank Function.

Summary filter:
A summary filter is used to filter summary data items in a report. Before you create a summary filter, you must calculate the summary data items that you want to use in the filter. Summary data items that you include in the filter are calculated before the filter is applied while summary data
items that aren't in the filter are calculated after the summary filter is applied. You can filter summary data items that you select from a report query or the catalog.

Monday, August 4, 2014

SQL Queries


1.  How to find the top 5 Or Topn employee salaries
select a.EmpNo, a.Sal from(
select Empno,sal, ROW_NUMBER() over(Order by sal desc) salord from Emp)a where a.salord <= 5

2.  Find out nth highest salary from emp table?
SELECT DISTINCT (a.sal) FROM EMP A WHERE 5 = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);

3.  Find out nth highest salary DEPT wise from emp table?
SELECT * FROM (SELECT DISTINCT DeptNo,(SAL),DENSE_RANK() OVER(PARTITION BY DEPTNO ORDER BY SAL DESC) AS RNK FROM EMP)a WHERE a.RNK = 1

4.  How do I eliminate the duplicate rows ?
delete from Emp where EmpNo in(select EmpNo from Emp group by EmpNo having count(*) >1)

5.  How to find 2nd max salary from emp ?
select max(sal) from emp  where sal not in(select max(sal) from emp)

6.  Co-related sub query:
Correlated subquery runs once for each row selected by the outer query. It contains a reference to a value from the row selected by the outer query.
Eg:
select e1.EmpNo, e1.sal, e1.deptno from emp e1 where e1.sal = (select max(sal) from emp e2 where e2.deptno = e1.deptno)

Nested Subquery:
Nested subquery runs only once for the entire nesting (outer) query. It does not contain any reference to the outer query row.
Eg:
select EmpNo, sal, deptno from emp where  sal in (select max(sal) from emp group by deptno)

7.  With Clause:
with W_dept as (select Deptno,Dname from Dept where DeptNo =10)
select emp.EmpNo,emp.Ename,W_dept.Dname from emp , W_dept where Emp.DeptNo = W_dept.DeptNo

8.  Write a query to find the employees whose salary is less than the salary of all employees in department of id 10
(Multi row Subquery)
SELECT Empno, SAL FROM EMP
WHERE SAL < ALL(SELECT  SAL  FROM EMP  WHERE DeptNo = 10)

How to use database user defined functions in Framework Manager


example from Microsoft SQL Server:
Steps:
1. Create a user defined function in SQL Server. Open SQL Server Enterprise Manager and right click on User Defined Functions (for example in Northwind database) and select New User Defined Function. Enter the following function text:
CREATE FUNCTION SquareNumber (@numberIn int)
RETURNS int AS
BEGIN
return @numberIn * @numberIn
END
2. Test the function. Select any table in the sample Northwind database, for example, Categories. Right click the table and select Open Table -> Query.
3. Enter the following SQL statement.
SELECT *, dbo.SquareNumber(2) AS Squared
FROM Categories
4. Run the SQL by pressing the "!" toolbar button. You will see a column called "Squared" which contains all 4s (2 squared = 4).
5. Import the function in Framework Manager and test it with one of the query subjects. Create a new query subject using the Categories table and modify the SQL to show as below:
a) If using Cognos SQL
Select Categories.CategoryID as Catid,
dbo.SquareNumber(2) as CognosSquaredfrom [Northwind].Categories
b) if using Native SQL
Select *, dbo.SquareNumber(2) as CognosSquaredfrom Cateogries
6. Test the query subject and publish for use in Report Studio. You will now have a "CognosSquared" query item for use in your reports.
Note: You can also use a prompt macro (ie. #prompt('parameter')#) with the function as an input parameter.