Query Syntax

My columns in the table is defined as below

fund1   fund2   pricingdatefund1    pricingdatefund2

I have to fetch the records in a way that  if pricing date fund 1= 15 Nov then fetch fund 1 record , if pricing date fund 2= 15 Nov then fetch fund2 record and if both of the pricingdatefund1, pricingdatefund2 matches my date then fetch both fund 1 and fund2.

I know it is complicated. I hope I have explained it

Thanks
jasmeen kaurAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Can you please provide few rows and the expected output? I mean columns you need in the output.
Pawan KumarDatabase ExpertCommented:
Please try this , tested solution for you.

Table Creation and Data Geneation

CREATE TABLE Funds
(
	 fund1 VARCHAR(30)  
	,fund2   VARCHAR(30)  
	,pricingdatefund1 DATETIME    
	,pricingdatefund2 DATETIME
)
GO

INSERT INTO Funds 
VALUES   ( 'A' , 'B' , '2017/11/15' ,  '2017/11/15'  ),
		 ( 'D' , 'C' , '2017/11/15' ,  '2017/11/16'  ),
		 ( 'R' , 'T' , '2017/11/14' ,  '2017/11/15'  )

GO

Open in new window


SOLUTION

DECLARE @myDate AS DATETIME = '2017/11/15'
SELECT * FROM 
(
	SELECT fund2 Fund , pricingdatefund2 Datex
	FROM Funds 
	WHERE pricingdatefund2 = @myDate AND pricingdatefund2 <> pricingdatefund1
	UNION ALL
	SELECT fund1 Fund , pricingdatefund1 Datex
	FROM Funds 
	WHERE pricingdatefund1 = @myDate AND pricingdatefund2 <> pricingdatefund1
	UNION ALL
	SELECT fund1 Fund , pricingdatefund1 Datex
	FROM Funds 
	WHERE pricingdatefund1 = @myDate AND pricingdatefund2 = pricingdatefund1
	UNION ALL
	SELECT fund2 Fund, pricingdatefund2 Datex
	FROM Funds 
	WHERE pricingdatefund1 = @myDate AND pricingdatefund2 = pricingdatefund1
)r

Open in new window


OUTPUT

/*------------------------
OUTPUT
------------------------*/
Fund                           Datex
------------------------------ -----------------------
T                              2017-11-15 00:00:00.000
D                              2017-11-15 00:00:00.000
A                              2017-11-15 00:00:00.000
B                              2017-11-15 00:00:00.000

(4 row(s) affected)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pawan KumarDatabase ExpertCommented:
Question abandoned. Provided solution
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.