Solved

help with sql syntax

Posted on 2014-04-17
7
323 Views
Last Modified: 2014-04-21
I have the following sql statement that doesn't quite work for me.  I want to list the records from table a join a couple of more, for the records that do not include the same propertyid,bldgid,unitid,resiid from the collections table.   I do not want to include the resident in the list if there is already a record in table2 with the same propertyid,bldgid,unitid,resiid.

Select a.propertyid+a.bldgid+a.unitid+cast(a.resiid as char(5))as residentID,a.propertyid,a.bldgid,a.unitid,a.resiid,a.transamt,a.GLPeriod,b.ResiFirstName,b.ResiLastName,b.ResiStatus,b.moveindate,c.MoveOutDate,c.MoveOutCode	 from tag_esite.dbo.TransactionHeader  a
right outer join lease b on a.PropertyId=b.PropertyId and a.BldgId=b.BldgId and a.UnitId=b.UnitId and a.ResiId=b.Resiid
right outer join leasemoveout c on b.PropertyId=c.PropertyId and b.BldgId=c.BldgId and b.UnitId=c.UnitId and b.ResiId=c.ResiId
where a.sourcecode='wo' and a.propertyid='cl' and a.propertyid+a.bldgid+a.unitid+cast(a.resiid as CHAR(5)) not in (select propertyid+bldgid+unitid+cast(resiid as char(5)) as residentid from aptgallery.dbo.Collections_Master)

order by a.PropertyId,a.BldgId,a.UnitId,a.resiid

Open in new window


thanks in advance
0
Comment
Question by:mgmhicks
[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
  • 3
  • 3
7 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 40007349
Can you be more specific about why "it doesn't quite work" for you?  You'll get a better answer if you're more specific about your desired output and provide some CREATE TABLE and INSERT sample data statements.

Select a.propertyid+a.bldgid+a.unitid+cast(a.resiid as char(5))as residentID,
      a.propertyid,a.bldgid,a.unitid,a.resiid,a.transamt,a.GLPeriod,
      b.ResiFirstName,b.ResiLastName,b.ResiStatus,b.moveindate,
      c.MoveOutDate,c.MoveOutCode
from tag_esite.dbo.TransactionHeader  a
right outer join lease b
      on a.PropertyId=b.PropertyId
      and a.BldgId=b.BldgId
      and a.UnitId=b.UnitId
      and a.ResiId=b.Resiid
right outer join leasemoveout c
      on b.PropertyId=c.PropertyId
      and b.BldgId=c.BldgId
      and b.UnitId=c.UnitId
      and b.ResiId=c.ResiId
LEFT OUTER JOIN aptgallery.dbo.Collections_Master AS collections
      ON a.PropertyId=collections.PropertyId
      and a.BldgId=collections.BldgId
      and a.UnitId=collections.UnitId
      and a.ResiId=collections.Resiid
where a.sourcecode='wo'
      and a.propertyid='cl'
      and collections.PropertyId IS NULL
order by a.PropertyId,a.BldgId,a.UnitId,a.resiid
0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 40007367
My interpretation, show me records from TransactionHeader, except for residents in:
1. Collections_Master
2. lease
select	a.propertyid+a.bldgid+a.unitid+cast(a.resiid as char(5)) as residentID
,	a.propertyid
,	a.bldgid
,	a.unitid
,	a.resiid
,	a.transamt
,	a.GLPeriod
,	b.ResiFirstName
,	b.ResiLastName
,	b.ResiStatus
,	b.moveindate
,	c.MoveOutDate
,	c.MoveOutCode
from	tag_esite.dbo.TransactionHeader		a
left
join	lease					b	on	a.PropertyId = b.PropertyId
							and	a.BldgId = b.BldgId
							and	a.UnitId = b.UnitId
							and	a.ResiId = b.ResiId
left
join	leasemoveout				c	on	a.PropertyId = c.PropertyId
							and	a.BldgId = c.BldgId
							and	a.UnitId = c.UnitId
							and	a.ResiId = c.ResiId
left
join	aptgallery.dbo.Collections_Master	d	on	a.PropertyId = d.PropertyId
							and	a.BldgId = d.BldgId
							and	a.UnitId = d.UnitId
							and	a.ResiId = d.ResiId
where	a.sourcecode = 'wo'
and	a.propertyid = 'cl'
and	b.ResiId IS NULL
and	d.ResiId IS NULL
order
by	a.PropertyId
,	a.BldgId
,	a.UnitId
,	a.resiid

Open in new window

0
 

Author Comment

by:mgmhicks
ID: 40007395
what isn't working is that I get all records from transaction Header with a WO sourcecode, but I only want all records from transactionheader with a WO sourcecode, that doesn't  exist in the collections_master table.  What makes the records exist is the propertyid,bldgid, unitid, and resiid all being the same.

thanks
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 34

Expert Comment

by:Brian Crowe
ID: 40007484
Why did you choose to use RIGHT OUTER JOINs for lease and leasemoveout?
0
 

Author Comment

by:mgmhicks
ID: 40007568
I only want a single record from the transactionheader and then a join so that I can keep just a single record.  See Ideally is I get the sum of the 'WO' sourcecode amount for each individual propertyid,bldgid,unitid, and resiid.  So know I have the property,bldgid,unitid and resiid as a single record and the 'WO' transaction sum as a column in that single record.  Then I need to join the lease table to receive a little data and the leasemoveout for yet a little more data, all pertaining to the unique property,bldgid,unitid,resiid.  

So a resident may have multiple 'WO' transactions I need the sum for each individual record, and then joining by property,bldging,unitid,resiid to other tables get the other bit of information like moveout date and stuff.

Hope that clears it up more rather than making it worse.
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 40007680
I think you are misusing the RIGHT join.  Change them to LEFT joins.  Also, you mention a "sum for each individual record".  Are you referring to the concatenation of the propertyid, bldgid, unitid, and resiid?  Typically a sum is an aggregate calculation requiring grouping which I don't see any indication of.

Try the query below:

Select T.propertyid + T.bldgid + T.unitid + CAST(T.resiid AS CHAR(5)) AS residentID,
	T.propertyid, T.bldgid, T.unitid, T.resiid, T.transamt, T.GLPeriod,
	L. ResiFirstName, L.ResiLastName, L.ResiStatus, L.moveindate,
	M.MoveOutDate, M.MoveOutCode
from tag_esite.dbo.TransactionHeader AS T
LEFT OUTER JOIN lease AS L
	ON T.PropertyId = L.PropertyId
	AND T.BldgId = L.BldgId
	AND T.UnitId = L.UnitId
	AND T.ResiId = L.Resiid
LEFT OUTER JOIN leasemoveout AS M
	ON T.PropertyId = M.PropertyId
	AND T.BldgId = M.BldgId
	AND T.UnitId = M.UnitId
	AND T.ResiId = M.ResiId
LEFT OUTER JOIN aptgallery.dbo.Collections_Master AS C
	ON T.PropertyId = C.PropertyId
	AND T.BldgId = C.BldgId
	AND T.UnitId = C.UnitId
	AND T.ResiId = C.Resiid
WHERE T.sourcecode = 'wo'
	AND T.propertyid = 'cl'
	AND C.PropertyId IS NULL	-- Only include records that have not entered collections
ORDER BY T.PropertyId, T.BldgId, T.UnitId, T.resiid

Open in new window

0
 

Author Closing Comment

by:mgmhicks
ID: 40013219
Thank you
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

724 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