Basic sql query and cricket match fixture Creation: Informatica DWH training Material
1. I have a table with data like
HR department decided to arrange inter department cricket match for employees fun and play.
Your HR manager told you to create a match fixture table which should show matches between different team as follows Marketing Sales Marketing IT Marketing HR Marketing Finance Sales IT Sales HR Sales Finance HR IT HR Finance Finance IT
Now how do you write a sql query to create the above max fixture?
Lets try to solve this problem step by step basis.
I have single table only to do so. DEPTNO DNAME 1 Marketing 4 Finance 2 Sales 3 HR 5 IT
DEPTNO DNAME 1 Marketing 2 Sales 3 HR 4 Finance 5 IT
2. If I need to get create a data set with every department associated with other department then I have to create a join. Let’s show it.
If we join Marketing with (finance,Sales,HR,IT) then I will get the match fixture for Marketing.
This row should not be included in the result set.
This way we should derive for all other department like Finance with (Sales,HR,IT) and so on.
At this point let me form a query.
3. Cross join is required to generate different combination. 1=1 is the condition used to associate every department with other department. where d1.dname<>d2.dname Its used to avoid same department association with same department. Since we don’t need any cricket match in between Marketing with Marketing Or Finance with Finance etc
Let’s now see the o/p the query
4. I have given only few rows as o/p. A cricket match between Marketing and Finance is same as Finance and Marketing
So I have to modify the query to remove this redundant data.
If I arrange the columns in such a manner that left column will always have
less value than its right counterpart , and on that result set if
I apply the group by functionality then obviously redundant will be removed.
Left column logic=
if(Col_1>Col2)
then Col_2
else Col_1 as Left_Col
Right Column Logic=
if(Col_1>Col2)
then Col_1
else Col_2 as right_Col
5. Let me give you an example
Marketing,Finance=> If I apply above logic then result will be Finance,Marketing
Finance,Marketing=> If I apply above logic then result will be Finance,Marketing
So let’s re write the query
And here is the o/p DEPARTMENT_1 DEPARTMENT_2 Marketing Sales Finance HR HR Sales Finance Marketing Finance IT HR IT
6. Finance Sales IT Sales HR Marketing IT Marketing
Please contact tele.sarkar@gmail.com for any clarification related to this document or for training on Informatica and Data Warehouse