troubleshooting Question

sql select - where column has more than two useable valuues

Avatar of SimonPrice33
SimonPrice33 asked on
Microsoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008
5 Comments1 Solution509 ViewsLast Modified:
Hi Experts,

I am working with the below query, and i need to return results for is TRADECODE is New or Existing, however if its Existing to ignore the rows with value NEW.

I would be grateful if someone could help me with this as I am stuck...  I have looked around online and see the same example with smaller calls, but nothing with anything of this complexity.

and unfortunately this cant be reduced at the moment.

SELECT     
	DISTINCT
	V.ENTITYID,
	V.CUSTOMERMNEMONIC,
	V.PRODUCTID,
	V.TRADENUMBER,
	V.TRADECODE,
	V.TRADEDATE,
	V.VALUEDATE,
	V.CCYPAIR,
	CASE
		WHEN V.PRODUCTID = 'FXOTC' THEN V.DIRECTION
		WHEN SUM(V.CCY1AMOUNT) > 0 THEN 'BUY'
		ELSE 'SELL'
	END DIRECTION,	
	LEFT(CCYPAIR,3) CCYID,
	SUM(V.CCY1AMOUNT) CCYAMOUNT,
	V.TRADERATE,
	SUM(V.CCY2AMOUNT) CCY2AMOUNT,
	V.MTMCCYID,
	CASE 
		WHEN V.PRODUCTID = 'FX' AND LEFT(v.CCYPAIR,3) <> V.MTMCCYID AND SUM(V.CCY2AMOUNT) <> 0 THEN SUM(MTM.MTMAMOUNT)
        WHEN V.PRODUCTID = 'FX' AND LEFT(v.CCYPAIR,3) = V.MTMCCYID AND SUM(V.CCY2AMOUNT) <> 0 THEN 0
        ELSE SUM(V.MTMLIMITCCYAMOUNT) 
    END MTMAMOUNT,
	V.MTMSPOTRATE,
	V.MTMCCY1DF,
	V.MTMCCY2DF,
	V.FEECCYID,
	SUM(V.FEECCYAMOUNT)	as FEECCYAMOUNT ,
	V.CCY1REVALRATE,
	V.CCY2REVALRATE,
	V.OPTIONEXPOSURETYPE,
	SUM(V.CCY1OTHERAMOUNT) as CCY1OTHERAMOUNT,
	SUM(V.CCY2OTHERAMOUNT) as CCY2OTHERAMOUNT,
	V.STRIKEPRICE,
	V.PREMIUMDATE,
	V.PREMIUMCCYID,
	V.PREMIUMAMOUNT,
	CASE 
		WHEN V.PRODUCTID = 'FX' THEN
			CASE
				WHEN V.TRADECODE IN ('SETTLED','ROLL_SET') AND LEFT(V.CCYPAIR,3) = CP.QUOTEDCCYID THEN SUM(MTM.MTMAMOUNT) 
				ELSE 0
			END
		ELSE
			CASE	
				WHEN V.TRADECODE IN ('SETTLED','ROLL_SET')THEN SUM(V.MTMLIMITCCYAMOUNT) 
				ELSE 0
			END		
	END MTMREALIZED,
	V.EXTERNALCUSTOMERREFID1,
	V.EXTERNALCUSTOMERREFID2,
	V.INTEXTIND,
	CASE 
		WHEN V.PRODUCTID = 'FXOTC' /* and LEFT(V.DIRECTION,1) = 'B' */ THEN 'N'
		WHEN V.TRADECODE = 'PREMSET' then 'N'
		WHEN LEFT(V.CCYPAIR,3) <> CP.QUOTEDCCYID THEN 'Y'               
		ELSE 'N'
	END EXCLUDECROSSIND,
    CASE 
		WHEN V.PRODUCTID = 'FX' AND LEFT(v.CCYPAIR,3) <> V.MTMCCYID AND SUM(V.CCY2AMOUNT) <> 0 THEN SUM(MTM.MTMAMOUNT)
        WHEN V.PRODUCTID = 'FX' AND LEFT(v.CCYPAIR,3) = V.MTMCCYID AND SUM(V.CCY2AMOUNT) <> 0 THEN 0
        ELSE SUM(V.MTMLIMITCCYAMOUNT) 
    END MTMLIMITCCYAMOUNT,
	sum(V.CCY1LIMITCCYAMOUNT) as CCY1LIMITCCYAMOUNT,
	sum(V.CCY1LIMITCCYOTHERAMOUNT) as CCY1LIMITCCYOTHERAMOUNT,
	sum(V.CCY2LIMITCCYAMOUNT) as CCY2LIMITCCYAMOUNT,
	sum(V.CCY2LIMITCCYOTHERAMOUNT) as CCY2LIMITCCYOTHERAMOUNT,
	sum(V.FEELIMITCCYAMOUNT) as FEELIMITCCYAMOUNT,
	sum(V.PREMLIMITCCYAMOUNT) as PREMLIMITCCYAMOUNT,
	sum(V.PAYOUTLIMITCCYAMOUNT) as PAYOUTLIMITCCYAMOUNT,
	sum(V.LOWERPAYOUTLIMITCCYAMOUNT) as LOWERPAYOUTLIMITCCYAMOUNT,
	V.CCY1LIMITCCYRATE
FROM         
	MARGINEODOPENTRADES V 	
		INNER JOIN CUSTOMER C ON
			V.CUSTOMERMNEMONIC = C.CUSTOMERMNEMONIC	
		 INNER JOIN CURRENCYPAIR CP ON
				((CP.CCYID1 = LEFT(V.CCYPAIR,3) AND CP.CCYID2 = RIGHT(V.CCYPAIR,3)) or
				(CP.CCYID1 = RIGHT(V.CCYPAIR,3) AND CP.CCYID2 = LEFT(V.CCYPAIR,3)))	
				and CP.ORGUNITID = 1
         LEFT OUTER JOIN TEXTMARGINOPENPOS_MTM MTM ON
				V.TRADENUMBER = MTM.TRADENUMBER AND
                V.ENTITYID = MTM.ENTITYID AND
                V.PRODUCTID = MTM.PRODUCTID AND 
                V.MTMCCYID = MTM.MTMCCYID AND
                V.TRADECODE = MTM.TRADECODE AND 
                V.VALUEDATE = MTM.VALUEDATE AND     
                V.GUID = MTM.GUID	
where

 v.TRADENUMBER = '311000375'
--and (v.TRADECODE = 'EXISTING' OR (v.TRADECODE = 'NEW'))
and v.TRADECODE in ('EXISTING','NEW')



/*WHERE;
	((LEFT(CCYPAIR,3) <> LD.LIMITCCYID AND RIGHT(CCYPAIR,3) = LD.LIMITCCYID) 
	OR (RIGHT(CCYPAIR,3) <>  LD.LIMITCCYID AND LEFT(CCYPAIR,3) <> LD.LIMITCCYID))*/		
GROUP BY
	V.GUID,
	V.ENTITYID,
	V.CUSTOMERMNEMONIC,
	V.PRODUCTID,
	V.TRADENUMBER,
	V.TRADEDATE,
	V.VALUEDATE,
	V.CCYPAIR,	
	V.MTMCCYID,
	LEFT(CCYPAIR,3),
	V.MTMSPOTRATE,
	V.MTMCCY1DF,
	V.MTMCCY2DF,
	V.TRADERATE,
	V.TRADEDATE,
	V.TRADECODE,
	V.FEECCYID,
	V.CCY1REVALRATE,
	V.CCY2REVALRATE,
	V.OPTIONEXPOSURETYPE,
	V.STRIKEPRICE,
	V.PREMIUMDATE,
	V.PREMIUMCCYID,
	V.PREMIUMAMOUNT,
	V.DIRECTION,
	V.EXTERNALCUSTOMERREFID1,
	V.EXTERNALCUSTOMERREFID2,
	V.INTEXTIND,
	CP.QUOTEDCCYID,
	V.CCY1LIMITCCYRATE,
	V.MTMLIMITCCYAMOUNT
)
ASKER CERTIFIED SOLUTION
John_Vidmar

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros