I need assistance in creating a query. I have Client table that has unique client info - identified by their unique ClientID. I also have a Client_UserDefinedFields table that contains values of custom data for clients. They are linked via the ClientID and there may be many records for a ClientID in this Client_UserDefinedFields table.
My situation is that there are 3 custom data fields that I need to know the values for a given client (as shown by my CASE statement). My current query is bringing back the client 3 times (a row for each value) and I want to only see the client once (one row) and have these values shown as columns. Not sure if this is possible or how to that. Furthermore, when I tried using a CASE statement in my select, I cannot use AS 'fieldname' to identify it - since it's giving me an error on the AS keyword.
An example of my current SQL SELECT statement
SELECT
c.ClientID
, c.LastName
, c.FirstName
, c.MiddleName
, CASE WHEN cudf.UserDefinedFieldFormatULink = '93fb3820-38aa-4655-8aad-a8dce8aede' THEN cudf.UDF_ReportValue --AS 'DA Status'
WHEN cudf.UserDefinedFieldFormatULink = '2144a742-08c5-4c96-b9e4-d6f1f56c76' THEN cudf.UDF_ReportValue --AS 'FHAP Status'
WHEN cudf.UserDefinedFieldFormatULink = 'c3d29be9-af58-4241-a02d-9ae9b43ffa' THEN cudf.UDF_ReportValue --AS 'HCRA Status'
END
FROM Client_Program cp
INNER JOIN client c ON c.ulink = cp.clientulink
INNER JOIN code_program p ON p.ulink = cp.programulink
INNER JOIN Code_System_State css ON c.ContactMailingStateUlink = css.ulink
INNER JOIN Code_ClientStatus ccs ON c.ClientStatusULink = ccs.ULink
INNER JOIN Client_UserDefinedField cudf ON c.ULink = cudf.ClientULink
AND cp.ProgramStatusULink = '1' -- Open (active) program
AND c.ClientStatusULink = '10000000' --Active client
AND cp.programulink in ('7280f4a7-cd94-49be-86ad-a74421ff6f',
'0a9b94a3-edd7-4918-b79c-bf2b20f9da',
'54f6c691-2eba-49e5-8380-85f5349bca',
'ed8c497d-d4fe-41d7-a218-4235fd0734',
'5be826f0-b3c3-4ebe-871d-4d20b56da5')
AND cudf.UserDefinedFieldFormatULink IN ('93fb3820-38aa-4655-8aad-a8dce8aede', -- DA Status
'2144a742-08c5-4c96-b9e4-d6f1f56c76', --FHAP Status
'c3d29be9-af58-4241-a02d-9ae9b43ffa') --HCRA Status
Again, my issue is that I don't want to bring back the same client multiple times if they had more than one entry in the Client_UserDefinedFields table. I'd like to bring this in one row with each "Status" field correctly populated as a columns. How do I do this? Here's a sample of my current output:
ClientID LastName FirstName MiddleName PCHP/HCH Status DA Status FHAP Status HCRA Status
XXXXXXXXXXXX River Mike Allan Active (null) - None Selected - (null)
XXXXXXXXXXXX River Mike Allan Active Active (null) (null)
XXXXXXXXXXXX River Mike Allan Active (null) (null) - None Selected -
Ultimately would like to see just the one record with all the values
ClientID LastName FirstName MiddleName PCHP/HCH Status DA Status FHAP Status HCRA Status
XXXXXXXXXXXX River Mike Allan Active Active - None Selected - - None Selected -
Examples are very helpful as I'm not a SQL guru. Thank you!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…