Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
LOOKUP_BI
Constraint Violating Yak Guru
295 Posts |
Posted - 2011-04-15 : 11:55:26
|
I just cant figure out this. I have 3 tables as suchTable A Name Gender CityJo M StLina F PkSandy F Li**Column name is PKTable DName TypeJo ALina BSusan CTable TypeID In_Type Out_Type1 A,M,N,J Primary2 Z,B,D Secondary3 P,Q,C,X Third**Column ID is PKI know the above data might not make sense, but I cant disclose the original data but the data structure is the same as the original.Here is what I need to do, get the Name,Type and Out_Type information by joining all 3 tablesI have tried the following's but they dont seem to work.I know Im missing something.SELECT a.NAME,d.[Type],t.[Out_Type] FROM A a INNER JOIN D dON a.Name=d.Name INNER JOIN [TYPE]tON d.[Type] IN ( SELECT t.[In_Type]FROM [Type])SELECT a.NAME,d.[Type],CASEWHEN d.[Type] IN (t.[In_Type]) AND t.[Out_Type]='Primary' THEN 'Primary' WHEN d.[Type] IN (t.[In_Type]) AND t.[Out_Type]='Secondary' THEN 'Secondary'WHEN d.[Type] IN (t.[In_Type]) AND t.[Out_Type]='Third' THEN 'Third'ELSE 'No Support' END AS [Out_Type]FROM A a INNER JOIN D dON a.Name=d.Name INNER JOIN [TYPE]tON d.[Type] IN ( SELECT t.[In_Type]FROM [Type])SELECT a.NAME,d.[Type],CASEWHEN d.[Type] IN (t.[In_Type]) AND t.[Out_Type]='Primary' THEN 'Primary' WHEN d.[Type] IN (t.[In_Type]) AND t.[Out_Type]='Secondary' THEN 'Secondary'WHEN d.[Type] IN (t.[In_Type]) AND t.[Out_Type]='Third' THEN 'Third'ELSE 'No Support' END AS [Out_Type]FROM A a INNER JOIN D dON a.Name=d.Name INNER JOIN [TYPE]t ON d.[Type] LIKE ('%' + t.[In_Type] + '%')All above Queries Do not return anythingMy output should look like thisName Type Out_0TypeJo A Primary Lina B Secondary |
|
LOOKUP_BI
Constraint Violating Yak Guru
295 Posts |
Posted - 2011-04-15 : 12:56:49
|
Got it to workSELECT a.NAME,d.[Type],t.[Out_Type] FROM A a INNER JOIN D dON a.Name=d.Name INNER JOIN [TYPE]tON t.[In_Type] LIKE '%' + d.[Type] + '%'SELECT a.NAME,d.[Type],CASEWHEN t.[In_Type] LIKE '%' + UPPER(d.[Type]) + '%' AND t.[Out_Type]='Primary' THEN 'Primary' WHEN t.[In_Type] LIKE '%' + UPPER(d.[Type]) + '%' AND t.[Out_Type]='Secondary' THEN 'Secondary'WHEN t.[In_Type] LIKE '%' + UPPER(d.[Type]) + '%' AND t.[Out_Type]='Third' THEN 'Third'ELSE 'No Support' END AS [Out_Type]FROM A a INNER JOIN D dON a.Name=d.Name INNER JOIN [TYPE]t ON t.[In_Type] LIKE '%' + UPPER(d.[Type]) + '%' |
 |
|
|
|
|
|
|