简体   繁体   中英

Adding a new column in this SQL Query?

I am querying data from the WIP and Employee tables:

WIP Id,Name

Employee Id,Name,Orgnization

Joining both I can query:

select w.ID,e.Organization,w.ConsultantName,e.OrganizationID, w.ConsultantID
from vwWIPRecords w
inner join vwEmployees e on w.ConsultantID=e.ID; 

Resutls:

1   VHAA    Web User    1   1
2   VHAA    NZ RP       1   3
3   VHAA    Ghom Mure   1   2
4   VHAA    Ghom Mure   1   2

Requirment:

In query add anther column which will concatenate and group by e.Organization and e.ConsultantName but it will be only for first unique record. For next (where name and organization is same) it will not show anything. This column will show unique Consultants of a company. Please see record number 3 and 4 in second example.

1   VHAAWeb User    1   1
2   VHAANZ RP       1   3
3   VHAAGhom Mure   1   2
4                   1   2

Thanks a lot for your help

Here is a start. The final column is a flag indicating the row should be blank. Let me know if this works for you so far and I can help further.

select w.ID,e.Organization, w.ConsultantName,
e.OrganizationID, w.ConsultantID, CASE WHEN D.Dup > 1 AND D.ID <> w.ID THEN 'Y' 
ELSE 'N' END As HideMe
from vwWIPRecords w
inner join vwEmployees e on w.ConsultantID=e.ID
inner join
(
   select MIN(w.ID) As ID,  e.Organization,w.ConsultantName,
   e.OrganizationID, w.ConsultantID, COUNT(*) AS Dup
   from vwWIPRecords w
   inner join vwEmployees e on w.ConsultantID=e.ID
) D
ON  D.Organization  = w.Organization 
AND D.ConsultantName = w.ConsultantName
AND D.OrganizationID = w.OrganizationID
AND D.ConsultantID = w.ConsultantID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM