Question

![Cognost reports studio Query Explorer]

Below is the snapshot of a table.

**Acctno     ClientNo     ClientName     PrimaryOffId         SecondaryOffID**
  101      11111        ABC corp           3                        Not Defined
  102      11116        XYZ Inc            5                        Not Defined
  103      11113        PQRS Corp          2                        9
  104      55555        Food LLC           4                        11
  105      99999        Kwlg Co            1                        Not Defined
  106      99999        Kwlg Co            1                        Not Defined
  107      11112        LMN Corp           Not Defined              6
  108      11112        LMN Corp           Not Defined              6
  109      11115        Sleep Co           4                        10
  110      44444        Cool Co            Not Defined              8
  111      11114        Sail LLC           3                        Not Defined
  112      66666        Fun Inc            1                        Not Defined
  113      88888        Job LLC            5                        12
  114      22222        Acc Co             Not Defined              Not Defined
  115      77777        Good Corp          2                        Not Defined
  116      33333        City  LLC          Not Defined              7
  117      33333        City  LLC          Not Defined              7
  118      33333        City  LLC          Not Defined              7
  119      11111        ABC corp           3                        Not Defined

I want to replace PrimaryOffID and SecondaryOffID with their Names coming from this table

EmpID     Names
  1      Cathy
  2      Chris
  3      John
  4      Kevin
  5      Mark
  6      Celine
  7      Jane
  8      Phil
  9      Jess
 10      Jose
 11      Nick
 12      Rosy

The Result should look like this: Notice that, If Cathy is the PrimaryOfficer, she can't be the Secondary Officer and vice versa. This logic is applicable for all the Names

Acctno  ClientNo    Client Name PrimOffName SecondaryOffName
 101    11111   ABC corp          John           Not Defined
 102    11116   XYZ Inc           Mark           Not Defined
 103    11113   PQRS Corp         Chris          Jess
 104    55555   Food LLC          Kevin          Nick
 105    99999   Kwlg Co           Cathy          Not Defined
 106    99999   Kwlg Co           Cathy          Not Defined
 107    11112   LMN Corp          Not Defined        Celine
 108    11112   LMN Corp          Not Defined        Celine
 109    11115   Sleep Co          Kevin          Jose
 110    44444   Cool Co           Not Defined    Phil
 111    11114   Sail LLC          John           Not Defined
 112    66666   Fun Inc           Cathy          Not Defined
 113    88888   Job LLC           Mark           Rosy
 114    22222   Acc Co            Not Defined   Not Defined
 115    77777   Good Corp         Chris          Not Defined
 116    33333   City  LLC         Not Defined   Jane
 117    33333   City  LLC         Not Defined   Jane
 118    33333   City  LLC         Not Defined   Jane
 119    11111   ABC corp          John           Not Defined

But Instead it looks like this:

Acctno  ClientNo    ClientName  PrimOffName SecondaryOffName
   101  11111   ABC corp         John             Not Defined
   102  11116   XYZ Inc          Mark             Not Defined
   103  11113   PQRS Corp        Chris            Not Defined
   103  11113   PQRS Corp        Not Defined           Jess
   104  55555   Food LLC         Kevin            Not Defined
   104  55555   Food LLC         Not Defined     Nick
   105  99999   Kwlg Co          Cathy            Not Defined
   106  99999   Kwlg Co          Cathy            Not Defined
   107  11112   LMN Corp         Not Defined         Celine
   108  11112   LMN Corp         Not Defined         Celine
   109  11115   Sleep Co         Kevin            Not Defined
   109  11115   Sleep Co         Not Defined              Jose
   110  44444   Cool Co          Not Defined              Phil
   111  11114   Sail LLC         John             Not Defined
   112  66666   Fun Inc          Cathy            Not Defined
   113  88888   Job LLC          Mark             Not Defined
   113  88888   Job LLC          Not Defined              Rosy
   114  22222   Acc Co           Not Defined              Not Defined
   115  77777   Good Corp        Chris            Not Defined
   116  33333   City  LLC        Not Defined    jane
   117  33333   City  LLC        Not Defined    jane
   118  33333   City  LLC        Not Defined    jane
   119  11111   ABC corp         John            Not Defined

Notice that, now the Acctno is no more unique, Where ever the Names should have been in both the fields together, it separates and gives the output in the next row creating multiple records. i tried various options but it didn't work. Please be aware, that I am creating this report in Cognos Studio. Please suggest the possible query to get the desired result. Thanks in Advance. Appreciate your help.

Was it helpful?

Solution

You don't state which version of Cognos you're using. "Cognos Studio" is ambiguous. I'm most familiar with 8.4.1, but even then you don't say if you're trying to define this in the Cognos model, Query Studio, Event Studio or Report Studio.

Second, you should always show what you've got so far when asking questions on StackOverflow. People want to see what you have done to show you want to fix, not repeat the lion's share of the work. That's why you got downvotes.

As far as plain SQL, you'll want to do this:

SELECT a.Acctno, a.ClientNo, a.ClientName, coalesce(e1.Names,'Not Defined') "PrimaryOffName", coalesce(e2.Names,'Not Defined') "SecondaryOffName"
FROM Account a
LEFT OUTER JOIN Emp e1
    ON t.PrimaryOffID = e1.EmpID
LEFT OUTER JOIN Emp e2
    ON t.PrimaryOffID = e2.EmpID

I made up table names. You can do this in Report Studio by creating two queries for Emp and outer joining them in succession to the Account query.

If you're able to, you'll want to move the OffID fields to a separate juntion table and remove them from the Account table. You can then create a Status field or flag in that junction table that identifies primary and secondary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top