Solved

Difficult Left Join

Posted on 2010-11-23
6
179 Views
Last Modified: 2012-05-10
MY attached code works...I create the #temp table...do my join...and the output is what we want to see.

However...is there a way to NOT create a temp table and do the Left Join with my #temp select as a table in the left join section?

Instead of this...join to my select instead of a temp table
....Left Join #temp z on Left(a.afsSource,7) = z.src
Begin

		select left(afssource,7) src,

				 (1 -sum(case when (afsstatus = 1) then afsamount else 0 end)/sum(afsamount)) *100 as percentHeld

		Into #temp

		from proc_cfa.dbo.P_AvailableForSale  

		group by left(afssource, 7)

		order by left(afssource,7)

	Select	a.afsid,

			a.afsSource,

			case when Left(a.afsSource,2) = 'LT' then 

					lt.status else 

					ss.status end as [status],

			a.afsDate,

			a.afsAmount,			

			a.afsSourceDetail,

			a.afsStatus,

			IsNull(a.afsTransaction,0) as afsTransaction,

--			100 * (1-a.afsAmount / sum(x.afsAmount)) as pctHeld,

			z.percentHeld as pctHeld

	From	proc_cfa.dbo.P_AvailableForSale a

					Left Join proc_lt.dbo.P_Deals lt ON SubString(a.afsSource,4,4) = lt.DealID

					Left Join crmprod_01.dbo.P_Deals ss ON SubString(a.afsSource,4,4) = ss.DealID

					Left Join proc_cfa.dbo.P_AvailableForSale x on a.afsSource = x.afsSource

					Left Join #temp z on Left(a.afsSource,7) = z.src

	Where	a.afsTransaction = 0

			and

			(

				(a.afsAMount > @minAmount - 1 and a.afsAMount < @maxAmount + 1)

					and

				(YEAR(a.afsDate) > @startYear - 1 and YEAR(a.afsDate) < @endYear + 1)

					and

				case when Left(a.afsSource,2) = 'LT' then 

					lt.status else 

					ss.status end = 

						IsNull(@dealStatus,case when Left(a.afsSource,2) = 'LT' then 

									lt.status else 

									ss.status end)

			)

	group by	a.afsid, a.afsSource, a.afsSourceDetail, a.afsDate, a.afsAMount, 

				a.afsTransaction, a.afsStatus,ss.status, lt.status, z.percentHeld

	Order By	a.afsSource,a.afsSourceDetail, a.afsid

End

Open in new window

0
Comment
Question by:lrbrister
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34196515
you could use WITH construct:
Begin

;with temp as (

                select left(afssource,7) src,

                                 (1 -sum(case when (afsstatus = 1) then afsamount else 0 end)/sum(afsamount)) *100 as percentHeld

--                Into #temp

                from proc_cfa.dbo.P_AvailableForSale  

                group by left(afssource, 7)

                order by left(afssource,7)

)

        Select  a.afsid,

                        a.afsSource,

                        case when Left(a.afsSource,2) = 'LT' then 

                                        lt.status else 

                                        ss.status end as [status],

                        a.afsDate,

                        a.afsAmount,                    

                        a.afsSourceDetail,

                        a.afsStatus,

                        IsNull(a.afsTransaction,0) as afsTransaction,

--                      100 * (1-a.afsAmount / sum(x.afsAmount)) as pctHeld,

                        z.percentHeld as pctHeld

        From    proc_cfa.dbo.P_AvailableForSale a

                                        Left Join proc_lt.dbo.P_Deals lt ON SubString(a.afsSource,4,4) = lt.DealID

                                        Left Join crmprod_01.dbo.P_Deals ss ON SubString(a.afsSource,4,4) = ss.DealID

                                        Left Join proc_cfa.dbo.P_AvailableForSale x on a.afsSource = x.afsSource

                                        Left Join temp z on Left(a.afsSource,7) = z.src

        Where   a.afsTransaction = 0

                        and

                        (

                                (a.afsAMount > @minAmount - 1 and a.afsAMount < @maxAmount + 1)

                                        and

                                (YEAR(a.afsDate) > @startYear - 1 and YEAR(a.afsDate) < @endYear + 1)

                                        and

                                case when Left(a.afsSource,2) = 'LT' then 

                                        lt.status else 

                                        ss.status end = 

                                                IsNull(@dealStatus,case when Left(a.afsSource,2) = 'LT' then 

                                                                        lt.status else 

                                                                        ss.status end)

                        )

        group by        a.afsid, a.afsSource, a.afsSourceDetail, a.afsDate, a.afsAMount, 

                                a.afsTransaction, a.afsStatus,ss.status, lt.status, z.percentHeld

        Order By        a.afsSource,a.afsSourceDetail, a.afsid

End

Open in new window

0
 
LVL 18

Expert Comment

by:lludden
ID: 34196532

Begin
                WITH cteData (src, percentHeld)
                AS (
		select left(afssource,7) src,
				 (1 -sum(case when (afsstatus = 1) then afsamount else 0 end)/sum(afsamount)) *100 as percentHeld
		from proc_cfa.dbo.P_AvailableForSale  
		group by left(afssource, 7)
		order by left(afssource,7)
                )
	Select	a.afsid,
			a.afsSource,
			case when Left(a.afsSource,2) = 'LT' then 
					lt.status else 
					ss.status end as [status],
			a.afsDate,
			a.afsAmount,			
			a.afsSourceDetail,
			a.afsStatus,
			IsNull(a.afsTransaction,0) as afsTransaction,
--			100 * (1-a.afsAmount / sum(x.afsAmount)) as pctHeld,
			z.percentHeld as pctHeld
	From	proc_cfa.dbo.P_AvailableForSale a
					Left Join proc_lt.dbo.P_Deals lt ON SubString(a.afsSource,4,4) = lt.DealID
					Left Join crmprod_01.dbo.P_Deals ss ON SubString(a.afsSource,4,4) = ss.DealID
					Left Join proc_cfa.dbo.P_AvailableForSale x on a.afsSource = x.afsSource
					Left Join cteData  z on Left(a.afsSource,7) = z.src
	Where	a.afsTransaction = 0
			and
			(
				(a.afsAMount > @minAmount - 1 and a.afsAMount < @maxAmount + 1)
					and
				(YEAR(a.afsDate) > @startYear - 1 and YEAR(a.afsDate) < @endYear + 1)
					and
				case when Left(a.afsSource,2) = 'LT' then 
					lt.status else 
					ss.status end = 
						IsNull(@dealStatus,case when Left(a.afsSource,2) = 'LT' then 
									lt.status else 
									ss.status end)
			)
	group by	a.afsid, a.afsSource, a.afsSourceDetail, a.afsDate, a.afsAMount, 
				a.afsTransaction, a.afsStatus,ss.status, lt.status, z.percentHeld
	Order By	a.afsSource,a.afsSourceDetail, a.afsid
End

Open in new window

0
 
LVL 18

Expert Comment

by:deighton
ID: 34196541
try using the 'WITH' keyword


WITH YourInitialTable AS
(
select left(afssource,7) src,
                         (1 -sum(case when (afsstatus = 1) then afsamount else 0 end)/sum(afsamount)) *100 as percentHeld
            Into #temp
            from proc_cfa.dbo.P_AvailableForSale  
            group by left(afssource, 7)
            order by left(afssource,7)
)
      Select      a.afsid,
                  a.afsSource,
                  case when Left(a.afsSource,2) = 'LT' then
                              lt.status else
                              ss.status end as [status],
                  a.afsDate,
                  a.afsAmount,                  
                  a.afsSourceDetail,
                  a.afsStatus,
                  IsNull(a.afsTransaction,0) as afsTransaction,
--                  100 * (1-a.afsAmount / sum(x.afsAmount)) as pctHeld,
                  z.percentHeld as pctHeld
      From      proc_cfa.dbo.P_AvailableForSale a
                              Left Join proc_lt.dbo.P_Deals lt ON SubString(a.afsSource,4,4) = lt.DealID
                              Left Join crmprod_01.dbo.P_Deals ss ON SubString(a.afsSource,4,4) = ss.DealID
                              Left Join proc_cfa.dbo.P_AvailableForSale x on a.afsSource = x.afsSource
                              Left Join YourInitialTable  z on Left(a.afsSource,7) = z.src
      Where      a.afsTransaction = 0
                  and
                  (
                        (a.afsAMount > @minAmount - 1 and a.afsAMount < @maxAmount + 1)
                              and
                        (YEAR(a.afsDate) > @startYear - 1 and YEAR(a.afsDate) < @endYear + 1)
                              and
                        case when Left(a.afsSource,2) = 'LT' then
                              lt.status else
                              ss.status end =
                                    IsNull(@dealStatus,case when Left(a.afsSource,2) = 'LT' then
                                                      lt.status else
                                                      ss.status end)
                  )
      group by      a.afsid, a.afsSource, a.afsSourceDetail, a.afsDate, a.afsAMount,
                        a.afsTransaction, a.afsStatus,ss.status, lt.status, z.percentHeld
      Order By      a.afsSource,a.afsSourceDetail, a.afsid
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 500 total points
ID: 34196551
Hi,

Check out this...

Without #temp table
Select  a.afsid,
                        a.afsSource,
                        case when Left(a.afsSource,2) = 'LT' then 
                                        lt.status else 
                                        ss.status end as [status],
                        a.afsDate,
                        a.afsAmount,                    
                        a.afsSourceDetail,
                        a.afsStatus,
                        IsNull(a.afsTransaction,0) as afsTransaction,
--                      100 * (1-a.afsAmount / sum(x.afsAmount)) as pctHeld,
                        z.percentHeld as pctHeld
        From    proc_cfa.dbo.P_AvailableForSale a
                Left Join proc_lt.dbo.P_Deals lt ON SubString(a.afsSource,4,4) = lt.DealID
                Left Join crmprod_01.dbo.P_Deals ss ON SubString(a.afsSource,4,4) = ss.DealID
                Left Join proc_cfa.dbo.P_AvailableForSale x on a.afsSource = x.afsSource
                Left Join (select left(afssource,7) src,
                                 (1 -sum(case when (afsstatus = 1) then afsamount else 0 end)/sum(afsamount)) *100 as percentHeld
                            from proc_cfa.dbo.P_AvailableForSale  
							group by left(afssource, 7)
							)z on Left(a.afsSource,7) = z.src
        Where   a.afsTransaction = 0
                        and
                        (
                                (a.afsAMount > @minAmount - 1 and a.afsAMount < @maxAmount + 1)
                                        and
                                (YEAR(a.afsDate) > @startYear - 1 and YEAR(a.afsDate) < @endYear + 1)
                                        and
                                case when Left(a.afsSource,2) = 'LT' then 
                                        lt.status else 
                                        ss.status end = 
                                                IsNull(@dealStatus,case when Left(a.afsSource,2) = 'LT' then 
                                                                        lt.status else 
                                                                        ss.status end)
                        )
        group by        a.afsid, a.afsSource, a.afsSourceDetail, a.afsDate, a.afsAMount, 
                                a.afsTransaction, a.afsStatus,ss.status, lt.status, z.percentHeld
        Order By        a.afsSource,a.afsSourceDetail, a.afsid

Open in new window

0
 

Author Comment

by:lrbrister
ID: 34198426
Hey folks,
  I was able to drop Brichsoft: solution straight in with no modifications.

Any problem with the points going to him?
0
 

Author Closing Comment

by:lrbrister
ID: 34201081
Worked perfectly...thanks
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

Suggested Solutions

Title # Comments Views Activity
need help in sql 4 63
SQL Server Deadlocks 12 47
Run time Error 4 34
ASP.NET 5 Templates 2 65
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 setup several different housekeeping processes for a SQL Server.

932 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

10 Experts available now in Live!

Get 1:1 Help Now