Solved

Need help with where statement

Posted on 2008-06-19
5
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

751 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