Solved

Need help with where statement

Posted on 2008-06-19
5
294 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

777 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