This is the code I have
select distinct sli.order_no,
sli.pkg_no,
case when line.primary_ind = 'Y' then sum(paid_amt) else 0 end as paid_amt,
line.pkg_li_no,
sum(case when sli.perf_no = 0 then 1 else 0 end) as num_seats_pur,
sli.status
from t_sub_line sli
left outer join t_line line on sli.li_seq_no = line.li_seq_no
where sli.order_no in (1,2)
group by
sli.order_no,
sli.pkg_no,
line.primary_ind,
line.pkg_li_no,
sli.status
having line.primary_ind = 'Y'
This code produces this output
order_no pkg_no paid_amt pkg_li_no num_seats_pur status
1 322 124.00 967 2 7
1 322 -124.00 992 2 4
2 854 253.00 952 1 7
2 854 -253.00 996 1 4
what I really need for the data to return is the following. I need the sum of paid_amt field.
order_no pkg_no paid_amt pkg_li_no num_seats_pur status
1 322 0 967 2 7
2 854 0 996 1 4
even if i change status to be max(status) so its not grouping on it. I don't have sum_paid amt.
when i try this code:
sum(case when line.primary_ind = 'Y' then sum(paid_amt) else 0 end) as paid_amt,
I get the following error message
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
A couple things you need to do.
Comment out pkg_li_no from the select and group by
Change the having statement to be included in the where statement
Remove the case from paid_amt since it is not necessary
select distinct sli.order_no,
sli.pkg_no,
sum(paid_amt)as paid_amt,
-- line.pkg_li_no,
sum(case when sli.perf_no = 0 then 1 else 0 end) as num_seats_pur,
sli.status
from t_sub_line sli
left join t_line line on sli.li_seq_no = line.li_seq_no
where sli.order_no in (1,2)
and line.primary_ind = 'Y'
group by
sli.order_no,
sli.pkg_no,
line.primary_ind,
-- line.pkg_li_no,
sli.status
Other notes:
I am not sure you want to select or group by the pkg_no or status, but you know more what outcome are looking for. If they are different values, there will be different records.
I'm not sure what you're trying to accomplish, but it seems that you overthinking the query. Try to simplifying it.
FYI HAVING happens after the grouping, WHERE happens before the grouping, you don't even need the CASE
select distinct sli.order_no,
sli.pkg_no,
sum(paid_amt) as paid_amt,
sum(case when sli.perf_no = 0 then 1 else 0 end) as num_seats_pur,
sli.status
from t_sub_line sli
left outer join t_line line on sli.li_seq_no = line.li_seq_no
where sli.order_no in (1,2)
and line.primary_ind = 'Y'
group by
sli.order_no,
sli.pkg_no,
sli.status
I'm trying to switch rows and columns with PIVOT (or another method). The documentation is pretty confusing to me. Thanks
DECLARE #CallCenterID Int;
DECLARE #BeginDateofReview SmallDateTime;
DECLARE #EndDateofReview SmallDateTime;
SELECT
COUNT(case when Score_Greeting = 'Yes' then Score_Greeting END) AS Score_Greeting_Passed,
SUM(CASE WHEN Score_Greeting IS NOT NULL THEN 1 ELSE 0 END) AS Score_Greeting_Reviewed,
ROUND(CONVERT(decimal(4,1), (COUNT(CASE WHEN Score_Greeting = 'Yes' THEN Score_Greeting END) * 100.0) / NULLIF(SUM(CASE WHEN Score_Greeting IS NOT NULL THEN 1 ELSE 0 END),0),0),0) AS Score_Greeting_PctngPassed,
COUNT(CASE WHEN Score_Authentication = 'Yes' THEN Score_Authentication END) AS Score_Authentication_Passed,
SUM(CASE WHEN Score_Authentication IS NOT NULL THEN 1 ELSE 0 END) AS Score_Authentication_Reviewed,
ROUND(CONVERT(decimal(4,1), (COUNT(CASE WHEN Score_Authentication = 'Yes' THEN Score_Authentication END) * 100.0) / NULLIF(SUM(CASE WHEN Score_Authentication IS NOT NULL THEN 1 ELSE 0 END), 0), 0), 0) AS Score_Authentication_PctngPassed,
FROM
Calls
WHERE
CallCenterID = #CallCenterID AND
(DateofReview >= #BeginDateofReview AND DateofReview <= #EndDateofReview)
Desired results:
Score_Greeting_Passed 5
Score_Greeting_Reviewed 9
Score_Greeting_PctngPassed 56
Score_Authentication_Passed 6
Score_Authentication_Reviewed 9
Score_Authentication_PctngPassed 67
You can use the UNPIVOT operator to transpose the rows and columns. If we assume your query above is stored in #YourData, it's going to be something like:
SELECT
UnpivotedData.ScoreType
, UnpivotedData.ScoreValue
FROM
#YourData
UNPIVOT (ScoreValue FOR ScoreType IN ( [Score_Greeting_Passed], [Score_Greeting_Reviewed], [Score_Greeting_PctngPassed],
[Score_Authentication_Passed], [Score_Authentication_Reviewed], [Score_Authentication_PctngPassed] )) AS UnpivotedData
I have data like this: (table name: Activities)
ActivityId CreatedOn TypeId
1 2017-01-01 1
1 2017-01-02 1
1 2017-01-02 2
2 2017-01-01 3
Where Type is a lookup value: (table name: Types)
TypeId Name
1 Question
2 Answer
3 Comment
Basically it's an activity history table.
I want to turn the above tables into a grouped sum row for types, for each ActivityId, like this:
ActivityId QuestionCount AnswerCount CommentCount
1 2 1 0
2 0 0 1
I know the answer is probably pretty simple, but it's eluding me for some reason.
Any help? Thanks in advance.
A simple join and conditional aggregation should do the trick (I suspect you were over-thinking it)
Select ActivityID
,QuestionCount = sum(case when Name='Question' then 1 else 0 end)
,AnswerCount = sum(case when Name='Answer' then 1 else 0 end)
,CommentCount = sum(case when Name='Comment' then 1 else 0 end)
From Activities A
Join Types B on A.TypeId=B.TypeId
Group By ActivityId
Returns
ActivityID QuestionCount AnswerCount CommentCount
1 2 1 0
2 0 0 1
You could also do it without the Join... Just less readable
Select ActivityID
,QuestionCount = sum(case when TypeId=1 then 1 else 0 end)
,AnswerCount = sum(case when TypeId=2 then 1 else 0 end)
,CommentCount = sum(case when TypeId=3 then 1 else 0 end)
From #Activities A
Group By ActivityId
You could also try a PIVOT
Select ActivityID
,[1] as QuestionCount
,[2] as AnswerCount
,[3] as CommentCount
From (Select ActivityId,TypeID,1 as Cnt From #Activities) A
Pivot (count(Cnt) For TypeId in ([1],[2],[3]) ) p
I am writing a query to access health information from my student information system for state and county record compliance. We are using SQL Server 2000 (legacy system) as the backend. The query is this:
select
i.Description Immunization,
Case
When si.Exempt = 'P' Then Count (LName)
Else 0
END AS 'Personal Exempt',
Case
When si.Exempt = 'M' Then Count (LName)
Else '0'
END AS 'Medical Exempt',
Case
When si.Exempt = 'R' Then Count (LName)
Else 0
END AS 'Religious Exempt',
Case
When si.Exempt = 'I' Then Count (LName)--AS 'Personal Exemption'
Else '0'
END AS 'Had Illness'
from student cross join immunization i left join StImmunization si on si.sno = Student.sno and si.immcode = i.code
where Student.Status = 'A' and i.TotalReq > 0
Group BY i.Description, si.exempt
The output is this:
Immunization PersonalExempt MedicalExempt RelgiousExempt HadIllness
Chicken pox (Varicella) 0 0 0 6
Chicken pox (Varicella) 1 0 0 0
Hepatitis B 1 0 0 0
T_DAP 0 0 0 0
Polio 0 0 0 0
Diptheria, Tetanus,
Pertussis 0 0 0 0
Hepatitis B 0 0 0 0
Diptheria, Tetanus,
Pertussis 0 0 0 0
MMR 0 0 0 0
T_DAP 1 0 0 0
MMR 1 0 0 0
Polio 1 0 0 0
Chicken pox (Varicella) 1 0 0 0
I would like the output to combine the like rows of the columns so it looks like this and if necessary add like row and columns:
Imunization PersonalExempt Medical Exempt Religious Exempt Had Illness
Chicken pox (Varicella) 2 0 0 6
Hepatitis B 1 0 0 0
T_DAP 1 0 0 0
Polio 1 0 0 0
Diptheria, Tetanus,
Pertussis 1 0 0 0
Notice row one (below) is the sum of rows 2 and 13 (above). Any ideas on how to accomplish this?
Better to use SUM here -- then you can select a 1 or 0 to make the sum give the count.
Old SQL GROUP BY trick.
select
i.Description AS Immunization,
SUM(Case When si.Exempt = 'P' Then 1 Else 0 END) AS 'Personal Exempt',
SUM(Case When si.Exempt = 'M' Then 1 Else 0 END) AS 'Medical Exempt',
SUM(Case When si.Exempt = 'R' Then 1 Else 0 END) AS 'Religious Exempt',
SUM(Case When si.Exempt = 'I' Then 1 Else 0 END) AS 'Had Illness'
from student cross join immunization i left join StImmunization si on si.sno = Student.sno and si.immcode = i.code
where Student.Status = 'A' and i.TotalReq > 0
Group BY i.Description
I have a SQL Server query that I want to do a left join to 2 tables. I need it to return all of the rows in the 1st table and only the matching data in the other 2 tables. My first incarnation created some sort of cross product with more than 10 times the required rows. So I added a rowcount function and had it filter for all rowcount values = 1. Now it returns the correct number of rows but with the wrong data from the other 2 tables.
I have certain filter rules (case statements) I apply to the contents of the 2 right tables to determine what, if anything, to return. I think this may be the source of the problem, but I'm not sure. I've tried using the filter code both within the select statement and as a where statement at the end of the query and even as part of the join on statement. I also tried using cross and outer applies. All to no avail.
I'm trying to achieve this without using loops. Is it possible? Please see the code below. This code generates too many rows. I won't repeat the 2nd iteration as it adds an extra select on top and a filter for m= 1 to the bottom to generate the correct number of rows.
These are the 2 tables that I am left joining to. Note that the 2nd table is only a small subset of a much larger table.
Thanks!!!
SELECT
GLENTRY.Fac + GLENTRY.Rundt + GLENTRY.Jrnllog AS UniqueID,
GLENTRY.Fac AS Fac,
(CASE WHEN
EntityTranslate2014.AcctEnd <> '' AND EntityTranslate2014.AcctEnd = SUBSTRING(GLENTRY.Acct,6,3)
THEN
EntityTranslate2014.Entity
ELSE
CASE WHEN
EntityTranslate2014.AcctEnd <> 0 AND EntityTranslate2014.Acct = GLENTRY.Acct
THEN
EntityTranslate2014.Entity
ELSE
CASE WHEN
EntityTranslate2014.SSDept <> 0 AND EntityTranslate2014.SSDept = SUBSTRING(GLENTRY.Acct,1,4)
THEN
EntityTranslate2014.Entity
ELSE
CASE WHEN
EntityTranslate2014.SSDept = 0
AND
EntityTranslate2014.AcctEnd = ''
AND
EntityTranslate2014.Acct = 0
THEN
EntityTranslate2014.Entity
ELSE
''
END
END
END
END)
AS NEWEntity,
(CASE WHEN AcctTranslate2014.Fac = GLENTRY.Fac OR AcctTranslate2014.Fac = '0'
THEN
AcctTranslate2014.NEWDept
ELSE
Null
END)
AS Department,
(CONVERT(DATETIME,GLENTRY.Period + '/01/' +
CASE WHEN
CONVERT(NVARCHAR(4),GLENTRY.Yearz) = ''
THEN
'2014'
ELSE
GLENTRY.Yearz
END)
AS YEARPER,
GLENTRY.Acct) AS SSAcct,
CONVERT(NVARCHAR(20),
CASE WHEN
AcctTranslate2014.Fac = GLENTRY.Fac OR AcctTranslate2014.Fac = '0'
THEN
AcctTranslate2014.NEWAcct
ELSE
Null
END)
AS NEWAccount,
GLENTRY.Rundt,
GLENTRY.Jrnlid,
GLENTRY.Amount,
GLENTRY.Dc,
GLENTRY.Ref,
GLENTRY.Refdt,
CONVERT(NVARCHAR(100),'Import of SS ' + CONVERT(VARCHAR(2),GLENTRY.Period) + '/' + CONVERT(VARCHAR(4),GLENTRY.Yearz) + ' GL activity') AS Descr,
GLENTRY.Invnr,
GLENTRY.Jrnllog,
ROW_NUMBER() OVER(PARTITION BY GLENTRY.Fac, GLENTRY.Yearz, GLENTRY.Period, GLENTRY.Pagez, GLENTRY.Acct, GLENTRY.Rundt, GLENTRY.Jrnlid,
GLENTRY.Amount, GLENTRY.Dc, GLENTRY.Ref, GLENTRY.Refdt, GLENTRY.Descr, GLENTRY.Invnr, GLENTRY.Origfac, GLENTRY.Jrnllog, GLENTRY.Seq
ORDER BY GLENTRY.Fac, GLENTRY.Yearz, GLENTRY.Period) AS m
FROM GLENTRY
LEFT OUTER JOIN
EntityTranslate2014 ON GLENTRY.Fac = EntityTranslate2014.Fac
LEFT OUTER JOIN
AcctTranslate2014 ON CONVERT(VARCHAR(8),GLENTRY.Acct) = AcctTranslate2014.SSAcct
WHERE GLENTRY.Yearz = 2014 AND GLENTRY.Period = 11
EntityTranslate2014 File
Fac Entity Descr AcctEnd SSDept Acct
1 51900 Entity1 0 0
2 50901 Entity2 0 0
3 10100 Entity3 0 0
3 10500 Entity4 4016 0
3 10500 Entity4 4020 0
3 10500 Entity4 4022 0
3 10500 Entity4 4024 0
3 10500 Entity4 4028 0
3 10500 Entity4 7016 0
4 30900 Entity5 0 0
5 10300 Entity6 0 0
6 11300 Entity7 0 0
7 11100 Entity8 0 0
7 11500 Entity9 4016 0
7 11500 Entity9 4020 0
7 11500 Entity9 4022 0
7 11500 Entity9 4024 0
7 11500 Entity9 4028 0
7 11500 Entity9 7016 0
9 32909 Entity10 0 0
10 12100 Entity11 0 0
11 32901 Entity12 0 0
12 53900 Entity13 0 0
13 10200 Entity14 0 0
14 32914 Entity15 0 0
15 32915 Entity16 0 0
16 11200 Entity17 0 0
17 32917 Entity18 0 0
18 32918 Entity19 0 0
19 32919 Entity20 0 0
20 32920 Entity21 0 0
21 13100 Entity22 0 0
22 52900 Entity23 0 0
89 99900 Entity24 0 0
123 12300 Entity25 0 0
124 12200 Entity26 0 0
133 13300 Entity27 0 0
201 11201 Entity28 0 0
202 11202 Entity29 0 0
402 25402 Entity30 0 0
403 25403 Entity31 0 0
549 25430 Entity32 0 0
549 25432 Entity33 7195 0
910 50910 Entity34 0 0
911 50911 Entity35 0 0
21 13500 Entity36 4016 0
21 13500 Entity36 4020 0
21 13500 Entity36 4022 0
21 13500 Entity36 4024 0
21 13500 Entity36 4028 0
21 13500 Entity36 7016 0
16 11202 Entity37 0 002 0
16 11201 Entity37 0 001 0
16 11200 Entity38 0 30918000
16 11200 Entity38 0 31918000
16 11200 Entity38 0 32110000
AcctTranslate2014
NewAcct SSAcct NewDEpt Fac
10111500 111200010 000 0
10111600 111200050 000 0
10111700 111550010 000 0
10113092 111050450 000 0
10115090 111050010 000 0
FROM GLENTRY
LEFT OUTER JOIN EntityTranslate2014
ON GLENTRY.Fac = EntityTranslate2014.Fac
LEFT OUTER JOIN AcctTranslate2014
ON CONVERT(VARCHAR(8), GLENTRY.Acct) = AcctTranslate2014.SSAcct
There's no relation defined between EntityTranslate2014 and AcctTranslate2014. If there's more than one record in either table for the corresponding record in GLENTRY, then those two tables will effectively cross-join with each other. For example, if a single GLENTRY joins to two records in EntityTranslate2014 and two records in AcctTranslate2014, then you'll get 4 records, one for each possible combination. That's simply how JOIN is defined.
If you know that this is happening and you know there's actually no relation, and you just want, say, for each record in GLENTRY the first record in EntityTranslate2014 to match to the first record in AcctTranslate2014, and the second record in EntityTranslate2014 to match to the second record in AcctTranslate2014 and so on, you can do what I've heard called a "ZIP JOIN":
FROM GLENTRY
LEFT OUTER JOIN (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY Fac ORDER BY <SomeField>) row_order
FROM EntityTranslate2014) ET2014
ON GLENTRY.Fac = ET2014.Fac
LEFT OUTER JOIN (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY SSAcct ORDER BY <SomeField>) row_order
FROM AcctTranslate2014) AT2014
ON CONVERT(VARCHAR(8), GLENTRY.Acct) = AT2014.SSAcct
AND AT2014.row_order = ET2014.row_order
Obviously, don't use SELECT *; this is just an example. So, what this is doing is assigning some order to each OUTER table, and them matching them up with each other. Thus, each record in each OUTER table will only show up once. If there's a mis-match, then the records from the other table will appear as NULLs.
The other common alternative is to simply return the first record for each OUTER table, so that you're guaranteed to only have one GLENTRY record since each OUTER table only contributes one or zero records for each key field.
If that doesn't work for your purposes, then you'll probably need to use two queries, and will need to do the matching in your application.
You can use a Correlated Subquery to "loop" through each row in one table comparing it to rows in another table without actually writing loop logic (which should be avoided in SQL). The rows in the inner query are compared to each row in the outer query based on the where predicate. Correlated Subqueries also allow you to filter the WHERE clause by the TOP 1 if more than 1 record would be returned in the WHERE clause query (as in the code below). This is just a brief example, as I don't have time to write out the code in its entirety, but it may help.
SELECT Entity
FROM EntityTranslate2014 ent
WHERE ent.AcctEnd <> ''
AND AcctEnd = (SELECT TOP 1 SUBSTRING(glt.Acct,6,3)
FROM Glentry glt
WHERE glt.fac = ent.fac
AND glt.Year = 2014
AND glt.Period = 11
ORDER BY glt.Fac, glt.Yearz, glt.Period)
In this example, each record from the EntityTranslate2014 table is compared to the results of the inner query based on the predicate that Glentry.Fac = EntityTraslate2014.fac. Hope that's helpful.
For more information on Correlated Subqueries, check out the following link.
https://technet.microsoft.com/en-us/library/ms187638%28v=sql.105%29.aspx
Updated...
You can use ROW_NUMBER to count sequences in the joined results, and ORDER BY to sort them so that the row you want is first in that sequence.
Thank you for posting the loop construct, I think I was able to approximate it's function with a series of CTEs(WITH alias as (...)). I'm not sure that I got all your table/field names correct, but this should be close to functional. A major guess on my part was how the "first" entity should be determined, you may need to tune the order by in the row number functions in the CTEs.
WITH ENTFIND AS (
SELECT
EntityTranslate2014.Fac,
EntityTranslate2014.Acct,
EntityTranslate2014.AcctEnd,
EntityTranslate2014.Entity,
EntityTranslate2014.SSDept,
EntityTranslate2014.Entity,
ROW_NUMBER() OVER (PARTITION BY Fac, Acct, AcctEnd ORDER BY Fac, Acct, AcctEnd, Entity) as E1Sort
FROM
EntityTranslate2014
),
E2 AS (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY Fac, Acct, AcctEnd ORDER BY Fac, Acct, AcctEnd, Entity) as E2Sort
FROM
ENTFIND
WHERE
AcctEnd <> 0
),
E3 AS (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY Fac, Acct, AcctEnd ORDER BY Fac, Acct, AcctEnd, Entity) as E3Sort
FROM
ENTFIND
WHERE
SSDept <> 0
),
E4 AS (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY Fac, Acct, AcctEnd ORDER BY Fac, Acct, AcctEnd, Entity) as E4Sort
FROM
ENTFIND
WHERE
Acct = 0
AND
AcctEnd = ''
AND
SSDept = 0
)
SELECT
GLENTRY.Fac + GLENTRY.Rundt + GLENTRY.Jrnllog AS UniqueID,
GLENTRY.Fac AS Fac,,
E1.Entity,
E2.Entity,
E3.Entity,
E4.Entity,
COALESCE(E1.Entity,E2.Entity,E3.Entity,E4.Entity) as 1stFoundEntity,
(CASE WHEN
EntityTranslate2014.AcctEnd <> '' AND EntityTranslate2014.AcctEnd = SUBSTRING(GLENTRY.Acct,6,3)
THEN
EntityTranslate2014.Entity
ELSE
CASE WHEN
EntityTranslate2014.AcctEnd <> 0 AND EntityTranslate2014.Acct = GLENTRY.Acct
THEN
EntityTranslate2014.Entity
ELSE
CASE WHEN
EntityTranslate2014.SSDept <> 0 AND EntityTranslate2014.SSDept = SUBSTRING(GLENTRY.Acct,1,4)
THEN
EntityTranslate2014.Entity
ELSE
CASE WHEN
EntityTranslate2014.SSDept = 0
AND
EntityTranslate2014.AcctEnd = ''
AND
EntityTranslate2014.Acct = 0
THEN
EntityTranslate2014.Entity
ELSE
''
END
END
END
END)
AS NEWEntity,
(CASE WHEN AcctTranslate2014.Fac = GLENTRY.Fac OR AcctTranslate2014.Fac = '0'
THEN
AcctTranslate2014.NEWDept
ELSE
Null
END)
AS Department,
(CONVERT(DATETIME,GLENTRY.Period + '/01/' +
CASE WHEN
CONVERT(NVARCHAR(4),GLENTRY.Yearz) = ''
THEN
'2014'
ELSE
GLENTRY.Yearz
END)
AS YEARPER,
GLENTRY.Acct) AS SSAcct,
CONVERT(NVARCHAR(20),
CASE WHEN
AcctTranslate2014.Fac = GLENTRY.Fac OR AcctTranslate2014.Fac = '0'
THEN
AcctTranslate2014.NEWAcct
ELSE
Null
END) AS NEWAccount,
GLENTRY.Rundt,
GLENTRY.Jrnlid,
GLENTRY.Amount,
GLENTRY.Dc,
GLENTRY.Ref,
GLENTRY.Refdt,
CONVERT(NVARCHAR(100),'Import of SS ' + CONVERT(VARCHAR(2),GLENTRY.Period) + '/' + CONVERT(VARCHAR(4),GLENTRY.Yearz) + ' GL activity') AS Descr,
GLENTRY.Invnr,
GLENTRY.Jrnllog,
ROW_NUMBER() OVER(PARTITION BY GLENTRY.Fac, GLENTRY.Yearz, GLENTRY.Period, GLENTRY.Pagez, GLENTRY.Acct, GLENTRY.Rundt, GLENTRY.Jrnlid,
GLENTRY.Amount, GLENTRY.Dc, GLENTRY.Ref, GLENTRY.Refdt, GLENTRY.Descr, GLENTRY.Invnr, GLENTRY.Origfac, GLENTRY.Jrnllog, GLENTRY.Seq
ORDER BY GLENTRY.Fac, GLENTRY.Yearz, GLENTRY.Period) AS m
FROM
GLENTRY
LEFT JOIN
ENTFIND E1 ON GLENTRY.Fac = E1.Fac
AND SUBSTRING(GLENTRY.Acct,6,3) = E1.AcctEnd
AND E1Sort = 1
LEFT JOIN
E2 ON GLENTRY.Fac = E2.Fac
AND GLENTRY.Acct = E2.Acct
AND E2.E2Sort = 1
LEFT JOIN
E3 ON GLENTRY.Fac = E3.Fac
AND SUBSTRING(GLENTRY.Acct,1,4) = E3.SSDept
AND E3.E3Sort = 1
LEFT JOIN
E4 ON GLENTRY.Fac = E4.Fac
AND E4.E4Sort = 1
LEFT OUTER JOIN
EntityTranslate2014 ON GLENTRY.Fac = EntityTranslate2014.Fac
LEFT OUTER JOIN
AcctTranslate2014 ON CONVERT(VARCHAR(8),GLENTRY.Acct) = AcctTranslate2014.SSAcct
WHERE
GLENTRY.Yearz = 2014 AND GLENTRY.Period = 11
I want to thank everyone for their ideas while I tried to get my query to work. I ended up with following solution. Note, for the sake of brevity, I only included the code for determining the Entity field. It's not pretty and I'm sure it's optimal, but it works.
Thanks again for all of your help!!
CASE WHEN
(SELECT
BET.Entity
FROM
BET
WHERE
GL.Fac = BET.Fac
AND
BET.AcctEnd <> ''
AND
BET.AcctEnd = SUBSTRING(CONVERT(NVARCHAR(8),GL.Acct),6,3))
IS NOT Null
THEN
(SELECT
BET.Entity
FROM
SOSViews.dbo.BI360EntityTranslate2014 BET
WHERE
GL.Fac = BET.Fac AND BET.AcctEnd <> ''
AND
BET.AcctEnd = SUBSTRING(CONVERT(NVARCHAR(8),GL.Acct),6,3))
ELSE
CASE WHEN
(SELECT
BET.Entity
FROM
SOSViews.dbo.BI360EntityTranslate2014 BET
WHERE
GL.Fac = BET.Fac
AND
BET.SOSDept <> 0
AND
BET.SOSDept = SUBSTRING(CONVERT(NVARCHAR(8),GL.Acct),1,4))
IS NOT Null
THEN
(SELECT
BET.Entity
FROM
BET
WHERE
GL.Fac = BET.Fac
AND
BET.SOSDept <> 0
AND
BET.SOSDept = SUBSTRING(CONVERT(NVARCHAR(8),GL.Acct),1,4))
ELSE
CASE WHEN
(SELECT
BET.Entity
FROM
BET
WHERE
GL.Fac = BET.Fac
AND
BET.SOSDept = 0 AND BET.AcctEnd = '' AND BET.Acct = 0)
IS NOT Null
THEN
(SELECT
BET.Entity
FROM
BET
WHERE
GL.Fac = BET.Fac
AND
BET.SOSDept = 0 AND BET.AcctEnd = '' AND BET.Acct = 0)
ELSE
'No Entity Translation'
END
END
END)
AS Entity,
Note: Although it may not be directly applicable to the OP's question, sometimes if there are too many rows in a join, it is as simple as joining on the wrong column. This just happened to me....