Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query help

Author  Topic 

newuser12
Starting Member

6 Posts

Posted - 2009-04-02 : 04:15:10
I have 2 tables as follow


table 1

ID IDStatus User
1 INITIATED ABC
1 PENDAPPMGR ABC
1 PENDVERIF XYZ
2 CLOSED XYZ
10 INITIATED XYZ


table2

Table_Name Code Description
ID_Status NOTSTARTED Started
ID_Status INITIATED Initiated
ID_Status PENDVERIF Verification
ID_Status REJECTMGR MangRejected
ID_Status OPTIONPREP Preparation
ID_Status PENDAPPMGR PendingMana
ID_Status PENDAPPINV PendingInv
ID_Status APPROVED Approved
ID_Status REJECTINV InvRejected
ID_Status CLOSED Closed


I want all the following result

NoID IDStatus Description User
10 INITIATED Initiated XYZ
1 PENDVERIF Verification XYZ
0 OPTIONPREP Preparation XYZ
0 PENDAPPMGR PendingMana XYZ
0 REJECTMGR MangRejected XYZ
0 PENDAPPINV PendingInv XYZ
0 REJECTINV InvRejected XYZ
0 APPROVED Approved XYZ
2 CLOSED Closed XYZ

I have written following query to achive this but getting duplicate IDStatus for INITIATED,CLOSED and PENDVERIF as this
IDStatus avaliable in table1


select distinct
case when (IDStatus = Code ) then ID else 0 end as NoID ,
case when (IDStatus = Code) then IDStatus else Code end as MileStoneID,
Description,
User
from table1, table2
where Table_Name = 'ID_Status'
and Code != 'NOTSTARTED' and Description != 'Started'
and UserID ='xYZ'


o/p of above query which i am getting is

NoID IDStatus Description User
0 INITIATED Initiated XYZ
10 INITIATED Initiated XYZ
0 PENDVERIF Verification XYZ
1 PENDVERIF Verification XYZ
0 OPTIONPREP Preparation XYZ
0 PENDAPPMGR PendingMana XYZ
0 REJECTMGR MangRejected XYZ
0 PENDAPPINV PendingInv XYZ
0 REJECTINV InvRejected XYZ
0 APPROVED Approved XYZ
0 CLOSED Closed XYZ
2 CLOSED Closed XYZ

Plz tell me how i can remove duplicate rows from my output.

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-04-02 : 05:34:55
you don't have any duplicate rows in the output.

Do you want to only return 1 of

0 PENDVERIF Verification XYZ
1 PENDVERIF Verification XYZ

If so -- which one -- the 0 or the 1?


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

newuser12
Starting Member

6 Posts

Posted - 2009-04-02 : 05:39:29
from below 2 rows

0 PENDVERIF Verification XYZ
1 PENDVERIF Verification XYZ

I want 1 PENDVERIF Verification XYZ

I want rows those have count greater than 0


0 CLOSED Closed XYZ
2 CLOSED Closed XYZ

for above 2 i want 2 CLOSED Closed XYZ
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-04-02 : 09:22:51
This?

SELECT
MAX([NoId]) AS [NoID]
, [MileStoneId]
, [description]
, [user]
FROM
(
SELECT DISTINCT
case when (IDStatus = Code ) then ID else 0 end as NoID ,
case when (IDStatus = Code) then IDStatus else Code end as MileStoneID,
Description,
User
from
table1
, table2
where
Table_Name = 'ID_Status'
and Code != 'NOTSTARTED'
and Description != 'Started'
and UserID ='xYZ'
)
a
GROUP BY
[MileStoneId]
, [description]
, [user]



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -