Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help with where statement

Posted on 2008-06-19
5
Medium Priority
?
309 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
3 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

876 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