T-SQL Multiple Join on 1 Table - sql-server

I'm currently working on a complex T-SQL query in MS SQL Server 2012. I basically retrieve a basic list of projects, holding the ProjectId as well as the StaffVersionId (it is possible that the Staff changes throughout the project, although I hope it won't be the case :P ).
CREATE TABLE #BasicProjects
(
ProjectId INT
, StaffVersionId INT
)
Next I need to join on the ProjectData Table to get the Title of the project as well as join on the Employee Table to get the FullName of the Employee.
SELECT [P].ProjectId
, [PD].Label AS Title
, [E].Lastname + '' '' + Firstname AS Manager
, [E].Lastname + '' '' + Firstname AS Contact
FROM #BasicProjects [P]
INNER JOIN [MySchema].[ProjectData] [PD] ON [PD].ProjectDataId = [P].ProjectDataId
INNER JOIN [MySchema].[Staff] [Y] ON [Y].StaffVersionId = [P].StaffVersionId AND [Y].StaffTypeId = 3 // Manager
INNER JOIN [MySchema].[Staff] [X] ON [X].StaffVersionId = [P].StaffVersionId AND [X].StaffTypeId = 2 // Contact
INNER JOIN [dbo].[Employee] [E] ON [E].EmployeeId = [Y].EmployeeId
INNER JOIN [dbo].[Employee] [E] ON [E].EmployeeId = [X].EmployeeId
The query is kind of hard, because I have 2 kinds of staff employees 3 = Manager, 2 = Contact.
When I run my query I'm getting this error:
The correlation name 'E' is specified multiple times in a FROM clause.
Do you know how to solve this error? Or perhaps some advises on how to improve this query?
Thanks a lot!

You need different aliases for each join (just like you did for Staff)
SELECT [P].ProjectId
, [PD].Label AS Title
, [YE].Lastname + ' ' + YE.Firstname AS Manager
, [XE].Lastname + ' ' + XE.Firstname AS Contact
FROM #BasicProjects [P]
INNER JOIN [MySchema].[ProjectData] [PD] ON [PD].ProjectDataId = [P].ProjectDataId
INNER JOIN [MySchema].[Staff] [Y] ON [Y].StaffVersionId = [P].StaffVersionId AND [Y].StaffTypeId = 3 // Manager
INNER JOIN [MySchema].[Staff] [X] ON [X].StaffVersionId = [P].StaffVersionId AND [X].StaffTypeId = 2 // Contact
INNER JOIN [dbo].[Employee] [YE] ON [YE].EmployeeId = [Y].EmployeeId
INNER JOIN [dbo].[Employee] [XE] ON [XE].EmployeeId = [X].EmployeeId

Related

Filter a table using COUNT SQL Server

I am new to SQL. I have written the code below but am getting stuck with the COUNT function. I want to only display the ClientIDs that have 2 or more ServiceIDs on the Service table. I tried doing a nested select within the join on the Service table originally and was getting error messages. Now with the code below, I am getting an error
Msg 164, Level 15, State 1, Line 13
Each GROUP BY expression must contain at least one column that is not an outer reference.
I am trying to achieve the following. THANK YOU!
client Id
Service ID
Count
1
2
3
1
3
3
1
4
3
2
5
4
2
6
4
2
7
4
2
8
4
SELECT DISTINCT
O.OrgName,
Referral.ClientID,
Client.FirstName,
Client.LastName,
P.ProviderName AS 'School',
E.ProgramID,
LI.ListLabel AS 'Reason',
Race.ListLabel AS 'Race/Ethnicity',
Gender.ListLabel AS 'Sex/Gender',
[ServiceId],
(SELECT COUNT([ServiceID])
GROUP BY Referral.ClientID
HAVING COUNT([ServiceID]) >= 2) AS 'Number of Supports'
FROM
ProviderReferral Referral
JOIN
Provider P ON ReferFromProviderID = P.EntityID
JOIN
ProviderReferralExt ON Referral.ProviderReferralID = ProviderReferralExt.ProviderReferralID
INNER JOIN
MultiSelectValue MSV ON MSV.ContextID = Referral.ProviderReferralID
AND MSV.ContextTypeID = 87
AND MSV.ListID = 1000001179
INNER JOIN
Client ON Referral.ClientID = Client.EntityID
INNER JOIN
EnrollmentMember ON client.EntityID = EnrollmentMember.ClientID
INNER JOIN
Enrollment E ON EnrollmentMember.EnrollmentID = E.EnrollmentID
AND E.X_CMNonCM = 1
INNER JOIN
ListItem LI ON LI.ListValue = MSV.ListValue
AND LI.ListID = 1000001179
INNER JOIN
ListItem Race ON Race.ListValue = client.Race
AND Race.ListID = 1000000068
INNER JOIN
ListItem Gender ON Gender.ListValue = Client.Gender
AND Gender.ListID = 1
INNER JOIN
Service ON E.EnrollmentID = Service.EnrollmentID -- the supports table
JOIN
Organization O ON o.EntityID = p.OrganizationID
WHERE
P.OrganizationID = 33847
AND E.ProgramID = 1325
AND referral.DeletedDate = '9999-12-31'
AND o.DeletedDate = '9999-12-31'
AND enrollmentmember.DeletedDate = '9999-12-31'
ORDER BY
referral.ClientID, client.FirstName
You can use window function count as follows:
Select * from
(Select ...
...
Count([ServiceID]) over (partition by referral.clientid) as cnt
From ...
...
) t where cnt > 2;
Please note order by should be last clause in your query. Use it accordingly.
Based on the request in the comment, Adding the code into your original query as follows:
select * from
(select Referral.ClientID,
Client.FirstName,
Client.LastName,
P.ProviderName as School,
E.ProgramID,
LI.ListLabel as Reason,
Race.ListLabel as "Race/Ethnicity",
Gender.ListLabel as "Sex/Gender",
[ServiceId],
Count([ServiceID]) over (partition by referral.clientid) as cnt -- this -- added open parenthesis before partition
FROM ProviderReferral Referral
JOIN Provider P on ReferFromProviderID=P.EntityID
JOIN ProviderReferralExt on Referral.ProviderReferralID=ProviderReferralExt.ProviderReferralID
INNER JOIN MultiSelectValue MSV on MSV.ContextID = Referral.ProviderReferralID AND
MSV.ContextTypeID=87 AND MSV.ListID=1000001179
INNER JOIN Client on Referral.ClientID=Client.EntityID
INNER JOIN EnrollmentMember on client.EntityID=EnrollmentMember.ClientID
INNER JOIN Enrollment E on EnrollmentMember.EnrollmentID=E.EnrollmentID and E.X_CMNonCM=1
INNER JOIN ListItem LI on LI.ListValue = MSV.ListValue and LI.ListID = 1000001179
INNER JOIN ListItem Race on Race.ListValue=client.Race and Race.ListID=1000000068
INNER JOIN ListItem Gender on Gender.ListValue=Client.Gender and Gender.ListID=1
INNER JOIN Service on E.EnrollmentID=Service.EnrollmentID -- the supports table
JOIN Organization O on o.EntityID =p.OrganizationID
Where P.OrganizationID=33847
and E.ProgramID=1325
and referral.DeletedDate = '9999-12-31' and o.DeletedDate='9999-12-31'
and enrollmentmember.DeletedDate='9999-12-31') t
where cnt > 2 -- this
order by ClientID, FirstName

SQL Query to get data from various databases

I wrote the below query to pull the data from different databases. I have created two temp tables to pull the data from two different databases and finally a select statement from the original database to join all the tables. My query is getting executed but not getting any data.(Report is blank). I tried executing the two temp tables separately. it is giving the correct data. But when I execute the whole query, the result is blank. Below is the query. Please help.
"set fmtonly off
use GODSDB
IF object_id('tempdb..#CISIS_Call_Log') IS NOT NULL DROP TABLE #CISIS_Call_Log
select *
into #CISIS_Call_Log
from OPENQUERY (CSISDB,
'select
ccl.ContractOID,
ccl.db_insertdate,
ccl.ContractCallLogStatusIdentifier,
ccl.db_UpdateDate,
ccp.ContractCallLogPurposeOID,
ccp.ContractCallLogPurposeIdentifier,
ccp.Description
from csisdb.dbo.ContractCallLog CCL
inner join csisdb.dbo.ContractCallLogPurpose CCP on ccl.ContractCallLogPurposeIdentifier = ccp.ContractCallLogPurposeIdentifier
where JurisdictionShortIdentifier = ''ON''
AND ContractCallLogStatusIdentifier IN (''DNR'', ''NR'')
')
IF object_id('tempdb..#CMS_Campaign') IS NOT NULL DROP TABLE #CMS_Campaign
select *
into #CMS_Campaign
from OPENQUERY (BA_GBASSTOCMS, '
Select
SystemSourceIdentifier,
ContractOID,
OfferSentDate,
CampaignOfferTypeIdentifier,
CampaignContractStatusIdentifier,
CampaignContractStatusUpdateDate,
DeclineDate,
CampaignOfferOID,
CampaignOID,
CampaignStartDate,
CampaignEndDate,
Jurisdiction,
CampaignDescription
from CMS.dbo.vw_CampaignInfo
where Jurisdiction = ''ON''
and CampaignOfferTypeIdentifier = ''REN''
')
select mp.CommodityTypeIdentifier as Commodity
,c.RtlrContractIdentifier as ContractID
,cs.ContractStatusIdentifier as ContractStatus
,c.SigningDate
,cf.StartDate as FlowStartDate
,cf.EndDate as FlowEndDate
,datediff(day, getdate(), c.RenewalDate) as RemainingDays
,c.RenewalDate
,l.ContractCallLogStatusIdentifier as CallLogType
,Substring (l.Description, 1, 20) as CallPurpose
,l.db_insertDate as CallLogDate
,cms.CampaignOfferOID as OfferID
,cms.CampaignContractStatusIdentifier as OfferStatus
,cms.CampaignContractStatusUpdateDate as StatusChangeDate
,cms.DeclineDate
from Contract c
inner join contractstate cs on cs.contractoid = c.ContractOID
and cs.ContractStatusIdentifier in ('ERA', 'FLW')
and datediff(day, getdate(), c.RenewalDate) > 60
inner join SiteIdentification si on si.SiteOID = c.SiteOID
inner join MarketParticipant mp on mp.MarketParticipantOID = si.MarketParticipantOID
inner join Market m on m.MarketOID = mp.MarketOID
inner join Jurisdiction j on j.JurisdictionOID = m.JurisdictionOID
and j.CountryCode = 'CA'
and j.ProvinceOrStateCode = 'ON'
inner join ContractFlow cf on cf.ContractOID = c.ContractOID
inner join #CISIS_Call_Log l on convert(varchar(15), l.ContractOID) = c.RtlrContractIdentifier
inner join #CMS_Campaign cms on convert(varchar(15), cms.ContractOID) = c.RtlrContractIdentifier
set fmtonly on"
IF the data in each temp table is verified, then:
Try a smaller, less complex, query to test your temp tables with. Also try them using a LEFT join as well e.g.:
select
c.RtlrContractIdentifier as ContractID
, c.SigningDate
, datediff(day, getdate(), c.RenewalDate) as RemainingDays
, c.RenewalDate
, l.ContractCallLogStatusIdentifier as CallLogType
, Substring (l.Description, 1, 20) as CallPurpose
, l.db_insertDate as CallLogDate
, cms.CampaignOfferOID as OfferID
, cms.CampaignContractStatusIdentifier as OfferStatus
, cms.CampaignContractStatusUpdateDate as StatusChangeDate
, cms.DeclineDate
from Contract c
LEFT join #CISIS_Call_Log l on convert(varchar(15), l.ContractOID) = c.RtlrContractIdentifier
LEFT join #CMS_Campaign cms on convert(varchar(15), cms.ContractOID) = c.RtlrContractIdentifier
Does this return data? Does it return data from both joined tables?
If neither temp table is returning data then those join conditions need to be changed.
If both temp tables do return data from that query, then try INNER joins. If that still works, then add back more joins (one at a time) until you identify the join that causes the overall fault.
Without data for every table it just isn't possible for us to pinpoint the exact reason for a NULL result. Only you can, so you need to trouble-shoot the problem one step at a time.

TSQL - Returning single value on if any row exists?

So I have a view that I want to return one row for each user, with some fields brought in (joined) from other tables. Everything works, except the last part.
One of the tables has multiple rows associated with the user, and I want one of the values in the row the view returns to be 0/1 if ANY of the lookup rows have a value. The view:
CREATE VIEW [VW_USER_LIST]
AS
SELECT USR.*,
UPL.FIRST_NAME,
UPL.LAST_NAME,
UPL.MIDDLE_INITIAL,
UPL.LDAP_DN,
UPL.RSS_TOKEN,
UPL.LAST_UPDATE_DATE,
UPL.TIMEZONE,
UPL.CULTURE,
(RTRIM(UPL.FIRST_NAME + ' ' + ISNULL(UPL.MIDDLE_INITIAL,'')) + ' ' + UPL.LAST_NAME) AS FULL_NAME,
UPOM.ORGANIZATION_ID AS ORGANIZATION_ID,
UPO.NAME AS ORGANIZATION_NAME,
UPOM.ORGANIZATION_ROLE_ID AS ORGANIZATION_ROLE_ID,
CASE WHEN ROL.ROLE_ID IS NULL THEN CONVERT(bit,0) ELSE CONVERT(bit,1) END AS ISINTERNAL
FROM [USER] AS USR
INNER JOIN [USER_PROFILE] AS UPL ON USR.USER_ID = UPL.USER_ID
LEFT JOIN [USER_ORGANIZATION_MEMBERSHIP] AS UPOM ON USR.USER_ID = UPOM.USER_ID
LEFT JOIN [ORGANIZATION] AS UPO ON UPOM.ORGANIZATION_ID = UPO.ORGANIZATION_ID
LEFT JOIN [USER_ROLE] AS URL ON USR.USER_ID = URL.USER_ID
LEFT JOIN [ROLE] AS ROL ON URL.ROLE_ID = ROL.ROLE_ID AND ROL.IS_INTERNAL=1
The check that I am the closest to use is the last one there before the 'FROM', the 'CASE'. Returns a BIT value of 0 or 1. It's matching JOIN is the last JOIN in the list.
However, if a user has two entries in the USER_ROLE that are marked IS_INTERNAL=1 in the table, then the view is returning two entries for that user.
And I understand why.
However, apparently what I NEED is to use the EXISTS, DISTINCT, ANY, or another singular check on that join - but can't find a good example of how to pull that off - if it's even possible.
And to clarify, the USER_ROLE table may have multiple entries for the USER_ID, cross-referenced to the ROLE table. So if the user has ANY entry in the USER_ROLE table that has a matching ROLE_ID with an entry in the ROLE table with IS_INTERNAL = 1. Yeah, there's a lot of steps there!
Something like this?
CREATE VIEW [VW_USER_LIST]
AS
SELECT USR.*,
UPL.FIRST_NAME,
UPL.LAST_NAME,
UPL.MIDDLE_INITIAL,
UPL.LDAP_DN,
UPL.RSS_TOKEN,
UPL.LAST_UPDATE_DATE,
UPL.TIMEZONE,
UPL.CULTURE,
(RTRIM(UPL.FIRST_NAME + ' ' + ISNULL(UPL.MIDDLE_INITIAL,'')) + ' ' + UPL.LAST_NAME) AS FULL_NAME,
UPOM.ORGANIZATION_ID AS ORGANIZATION_ID,
UPO.NAME AS ORGANIZATION_NAME,
UPOM.ORGANIZATION_ROLE_ID AS ORGANIZATION_ROLE_ID,
ISINTERNAL = cast(case when exists (select 1
from [role] rol
where url.role_id = rol.rol_id
and rol.is_internal = 1) then 1
else 0
end as bit)
FROM [USER] AS USR
INNER JOIN [USER_PROFILE] AS UPL ON USR.USER_ID = UPL.USER_ID
LEFT JOIN [USER_ORGANIZATION_MEMBERSHIP] AS UPOM ON USR.USER_ID = UPOM.USER_ID
LEFT JOIN [ORGANIZATION] AS UPO ON UPOM.ORGANIZATION_ID = UPO.ORGANIZATION_ID
LEFT JOIN [USER_ROLE] AS URL ON USR.USER_ID = URL.USER_ID

Select table from another DB in stored procedure don't work

I have a query which works fine. I must made a join with a table from another DB, as you can see below: takacs.TecDocB4.dbo
SELECT
TYP_ID as ID,
MFA_ID as ModelID,
concat(DES_TEXTS.TEX_TEXT, ' / ', TYP_KW_FROM) + 'KW' + ' / ' + DES_TEXTS3.TEX_TEXT as Name
FROM
takacs.TecDocB4.dbo.TYPES
INNER JOIN MODELS ON MOD_ID = TYP_MOD_ID
INNER JOIN MANUFACTURERS ON MFA_ID = MOD_MFA_ID
INNER JOIN COUNTRY_DESIGNATIONS AS COUNTRY_DESIGNATIONS2 ON COUNTRY_DESIGNATIONS2.CDS_ID = MOD_CDS_ID AND COUNTRY_DESIGNATIONS2.CDS_LNG_ID = 21
INNER JOIN COUNTRY_DESIGNATIONS ON COUNTRY_DESIGNATIONS.CDS_ID = TYP_CDS_ID AND COUNTRY_DESIGNATIONS.CDS_LNG_ID = 21
INNER JOIN DES_TEXTS ON DES_TEXTS.TEX_ID = COUNTRY_DESIGNATIONS.CDS_TEX_ID
LEFT JOIN DESIGNATIONS ON DESIGNATIONS.DES_ID = TYP_KV_ENGINE_DES_ID AND DESIGNATIONS.DES_LNG_ID = 21
LEFT JOIN DES_TEXTS AS DES_TEXTS2 ON DES_TEXTS2.TEX_ID = DESIGNATIONS.DES_TEX_ID
LEFT JOIN DESIGNATIONS AS DESIGNATIONS2 ON DESIGNATIONS2.DES_ID = TYP_KV_FUEL_DES_ID AND DESIGNATIONS2.DES_LNG_ID = 21
LEFT JOIN DES_TEXTS AS DES_TEXTS3 ON DES_TEXTS3.TEX_ID = DESIGNATIONS2.DES_TEX_ID
WHERE
TYP_MOD_ID = 504;
Now I need to transform this query in a stored procedure, but I keep getting some errors as you can see in the attached screenshot:
It says that has an unresolved reference to object takacs.TecDocB4.dbo.TYPES. Can you please help me on this?
Why it is working as a query? and as a stored procedure it doesn't work.
Thanks in advance!

Intersect query with no duplicates

I have not used sql server in a large complex scale in years, and Looking for help on how to proper sintax intersect type query to joing these two data sets, and not create duplicate names. Some patients will have both an order and a clinical event entry and some will only have a clinical event.
Data Set 1
SELECT
distinct
ea.alias as FIN,
per.NAME_Last + ', ' + per.NAME_FIRST + ' ' + Isnull(per.NAME_MIDDLE, '') as PatientName,
oa.action_dt_tm as CirOrder,
od.ORIG_ORDER_DT_TM as DischOrder,
e.disch_dt_tm as ActualDisch,
prs.NAME_FULL_FORMATTED as OrderedBy,
from pathway py
join encounter e on e.CERNER_ENCOUNTER_ID = py.encntr_id
join encntr_alias ea on ea.CERNER_ENCNTR_ID = e.CERNER_ENCOUNTER_ID and ea.ENCNTR_ALIAS_TYPE_WCD = 1049
join person per on per.CERNER_PERSON_ID = e.cerner_PERSON_ID
join orders o on o.CERNER_ENCNTR_ID= e.CERNER_ENCOUNTER_ID and o.CATALOG_wCD = '82111' -- communication order
and o.pathway_catalog_id = '43809296' ---Circumcision Order
join order_action oa on oa.[CERNER_ORDER_ID] = o.CERNER_ORDER_ID and oa.ACTION_TYPE_WCD = '2494'--ordered
join orders od on od.CERNER_ENCNTR_ID= e.CERNER_ENCOUNTER_ID and od.CATALOG_WCD = '203520' --- Discharge Patient
join prsnl prs on prs.CERNER_PERSON_ID = oa.order_provider_id
where py.pathway_catalog_id = '43809296' and ---Circumcision Order
oa.action_dt_tm > '2016-01-01 00:00:00'
and oa.ACTION_DT_TM < '2016-01-19 23:59:59'
--use the report prompts as parameters for the action_dt_tm
Data Set 2
SELECT
distinct e.[CERNER_ENCOUNTER_ID],
ea.alias as FIN,
per.NAME_Last + ', ' + per.NAME_FIRST + ' ' + Isnull(per.NAME_MIDDLE, '') as PatientName,
ce.EVENT_END_DT_TM as CircTime,
od.ORIG_ORDER_DT_TM as DischOrder,
e.disch_dt_tm as ActualDisch,
'' OrderedBy, -- should be blank for this set
cv.DISPLAY
from encounter e
join clinical_event ce on e.CERNER_ENCOUNTER_ID = ce.CERNER_ENCNTR_ID
join encntr_alias ea on ea.CERNER_ENCNTR_ID = e.CERNER_ENCOUNTER_ID and ea.ENCNTR_ALIAS_TYPE_WCD = 1049
join person per on per.CERNER_PERSON_ID = e.cerner_PERSON_ID
join orders od on od.CERNER_ENCNTR_ID= e.CERNER_ENCOUNTER_ID and od.CATALOG_WCD = '203520' --- Discharge Patient
left outer join ENCNTR_LOC_HIST elh on elh.CERNER_ENCNTR_ID = e.CERNER_ENCOUNTER_ID
left outer join CODE_VALUE cv on cv.CODE_VALUE_WK = elh.LOC_NURSE_UNIT_WCD
where ce.event_wcd = '201148' ---Newborn Circumcision
and ce.[RESULT_VAL] = 'Newborn Circumcision'
and ce.EVENT_END_DT_TM > '2016-01-01 00:00:00'
and ce.event_end_dt_tm < '2016-01-19 23:59:59’
and ce.RESULT_STATUS_WCD = '25'
and elh.ACTIVE_STATUS_DT_TM < ce.event_end_dt_tm -- Circ time between the location's active time and end time.
and elh.END_EFFECTIVE_DT_TM > ce.[EVENT_END_DT_TM]
--use the report prompts as parameters for the ce.[EVENT_END_DT_TM]
The structure of an intersect query is as simple as:
select statement 1
intersect
select statement 2
intersect
select statement 3
...
This will return all columns that are in both select statements. The columns returned in the select statements must be of the same quantity and type (or at least be convertible to common type).
You can also do an intersect type of query just using inner joins to filter out records in the one query that are not in the other. So for a simple example let's say you have two tables of colors.
Select distinct ColorTable1.Color
from ColorTable1
join ColorTable2
on ColorTable1.Color = ColorTable2.Color
This will return all the distinct colors in ColorTable1 that are also in ColorTable2. Using joins to filter could help your query perform better, but it does take more thought.
Also see: Set Operators (Transact-SQL)

Resources