Solved

Need help with where statement

Posted on 2008-06-19
5
288 Views
Last Modified: 2008-09-14
I am writing a query where I need to have two fields both true/false dependent on each other. I have attached the script. The section that is commented out is what I have for that piece. It gets the data that I need on its own, I just cant seem to get it working in with the bigger query.

Thanks in advance.
SELECT  F0006.MCDL01 PROP_NAME,

		F1507.NHMCU PROP_NUM,

		F1507.NHUNIT UNIT_NUM,

		CASE 

			WHEN F1507.NHUST='O' OR F1507.NHUST='D'

				THEN F15017.NWDOCO

			WHEN F1507.NHUST='V' OR F1507.NHUST='C'

				THEN '0'

		END LEASE_NUM,

		CASE 

			WHEN F1507.NHUST='O' OR F1507.NHUST='D'

				THEN F1501B.NEDL01

			WHEN F1507.NHUST='V' OR F1507.NHUST='C'

				THEN 'AVAILABLE'

		END TENANT_DBA,

		F0116.ALADDS STATE,

		F0116.ALCTY1 CITY,

		CONVERT(VARCHAR,CONVERT(MONEY,SUM(A.NWPMU1)),1) GLA,

		CONVERT(VARCHAR,CONVERT(MONEY,SUM(B.NWPMU1)),1) ACM

 

FROM PRODDTA.F0006, PRODDTA.F15017, PRODDTA.F0116, PRODDTA.F1501B, PRODDTA.F1507

	LEFT OUTER JOIN PRODDTA.F1514 A ON F1507.NHMCU=A.NWMCU

		AND F1507.NHUNIT=A.NWUNIT

		AND A.NWARTY='GLA'

	LEFT OUTER JOIN PRODDTA.F1514 B ON F1507.NHMCU=B.NWMCU

		AND F1507.NHUNIT=B.NWUNIT

		AND B.NWARTY='ACM'

--	(SELECT NHMCU, NHUNIT

--	FROM PRODDTA.F15017

--	INNER JOIN PRODDTA.F1507 ON F15017.NWMCU=F1507.NHMCU

--		AND F15017.NWUNIT=F1507.NHUNIT

--		AND F1507.NHUTTY='OTHER'

--	INNER JOIN PRODDTA.F1501B ON F15017.NWDOCO=F1501B.NEDOCO

--		AND F15017.NWLSVR=F1501B.NELSVR

--		AND F1501B.NELSET='OA') C

 

WHERE F1507.NHUTTY!='DISAB'

	AND F0006.MCAN8=F0116.ALAN8

	AND F0006.MCMCU=F1507.NHMCU

	AND F0006.MCMCU=F15017.NWMCU

	AND F1507.NHMCU=F15017.NWMCU

	AND F1507.NHUNIT=F15017.NWUNIT	

	AND F15017.NWDOCO=F1501B.NEDOCO

 

GROUP BY F0006.MCDL01, F1507.NHMCU, F1507.NHUNIT, F15017.NWDOCO, 

	F0116.ALADDS, F0116.ALCTY1, A.NWPMU1, F1507.NHUST, A.NWARTY, 

	F1501B.NEDL01

Open in new window

0
Comment
Question by:edensandavant
  • 2
5 Comments
 
LVL 2

Expert Comment

by:AntonyDN
ID: 21823772
How many rows does the "Commented" bit return?

If it's more than one, how do the values relate to the columns in the main query?

At a guess, from what I see, you need to do something like this, (see code window),  but we probably need more info about the underlying data - you could also put the joins inside the inner query ...
	

-- ......

LEFT OUTER JOIN PRODDTA.F1514 B ON F1507.NHMCU=B.NWMCU

		AND F1507.NHUNIT=B.NWUNIT

		AND B.NWARTY='ACM'

	JOIN

	(SELECT NHMCU, NHUNIT

	FROM PRODDTA.F15017

	INNER JOIN PRODDTA.F1507 ON F15017.NWMCU=F1507.NHMCU

		AND F15017.NWUNIT=F1507.NHUNIT

		AND F1507.NHUTTY='OTHER'

	INNER JOIN PRODDTA.F1501B ON F15017.NWDOCO=F1501B.NEDOCO

		AND F15017.NWLSVR=F1501B.NELSVR

		AND F1501B.NELSET='OA') C

	ON c.[columnname] = F15017.[matchingColumn]

	AND c.[anotherColumnName] = F1507.[anotherMatchingColumn]

 

WHERE F1507.NHUTTY!='DISAB' 

-- ........

Open in new window

0
 

Author Comment

by:edensandavant
ID: 21824546
It does return more than one row. What i need to do it have the main query check that subquery to see if the nhmcu and nhunit match. If they do match, I need those rows excluded.

I already tried to put those two lines that you added and that didn't work.
0
 
LVL 2

Accepted Solution

by:
AntonyDN earned 500 total points
ID: 21831048
You've used some quite old SQL styles here, which is a bit confusing, so I have reformatted it a bit. Maybe it was generated by something like Business Objects? If the result of that inner query determines whether rows get returned or not, then I'd have thought it could just form part of the main query. But then I haven't seen your data! (I did a version that cut it out, but maybe there's some subtlety there I can't see ....)

Anyway, try the code in the code box - you will probably need to add a unique column to the inner query, "C" and join that to some appropriate column(s) in the main query so you don't join to multiple rows, but I think this should do what you want .....
 

	FROM PRODDTA.F0006

	JOIN PRODDTA.F15017

		AND F0006.MCMCU=F15017.NWMCU

	JOIN PRODDTA.F1501B

		AND F15017.NWDOCO=F1501B.NEDOCO

	JOIN PRODDTA.F1507

		AND F1507.NHMCU=F15017.NWMCU

		AND F1507.NHUNIT=F15017.NWUNIT	

		AND F1507.NHMCU=F0006.MCMCU

	JOIN PRODDTA.F0116

		AND F0006.MCAN8=F0116.ALAN8
 

	JOIN 

	(

	SELECT F1507.NHMCU, F1507.NHUNIT

	FROM PRODDTA.F15017

	INNER JOIN PRODDTA.F1507 ON F15017.NWMCU=F1507.NHMCU

		AND F15017.NWUNIT=F1507.NHUNIT

		AND F1507.NHUTTY='OTHER'

	INNER JOIN PRODDTA.F1501B ON F15017.NWDOCO=F1501B.NEDOCO

		AND F15017.NWLSVR=F1501B.NELSVR

		AND F1501B.NELSET='OA'

	WHERE NHMCU != NHUNIT

	) C

	ON F1507.NHMCU=C.NHMCU

	-- Or some other unique, common identifier .....
 

	LEFT OUTER JOIN PRODDTA.F1514 A ON F1507.NHMCU=A.NWMCU

		AND F1507.NHUNIT=A.NWUNIT

		AND A.NWARTY='GLA'

	LEFT OUTER JOIN PRODDTA.F1514 B ON F1507.NHMCU=B.NWMCU

		AND F1507.NHUNIT=B.NWUNIT

		AND B.NWARTY='ACM'

 

WHERE F1507.NHUTTY!='DISAB'

 

GROUP BY F0006.MCDL01, F1507.NHMCU, F1507.NHUNIT, F15017.NWDOCO, 

	F0116.ALADDS, F0116.ALCTY1, A.NWPMU1, F1507.NHUST, A.NWARTY, 

	F1501B.NEDL01

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now