Click here to Skip to main content
15,885,878 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm stuck

there are two fields in the table, of an equal type, joined by "code" field with another table, describing the code

Please suggest

how to create the view to show the description of these two fields from chained table instead of code.


SELECT        dbo.MFU.PrintFormatMaximum, dbo.MFU.PrintFormatMinimum
FROM            dbo.MFU LEFT OUTER JOIN
                         dbo.PrintFormatTypes ON dbo.MFU.PrintFormatMinimum = dbo.PrintFormatTypes.Code AND dbo.MFU.PrintFormatMaximum = dbo.PrintFormatTypes.Code


What I have tried:

forums,
TSQL,
SQL View,
MSDN
Posted
Updated 28-Oct-18 23:15pm
Comments
OriginalGriff 29-Oct-18 4:22am    
You need to show us sample data for the tables, sample output from your query, and explain what output you are expecting - we can't work it out when you are typing as little as possible!

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
[no name] 29-Oct-18 4:46am    
I do not realize how to attach the picture and files
OriginalGriff 29-Oct-18 4:48am    
Don't: use text. That way, it's easier for us to duplicate your sample data for testing...
[no name] 29-Oct-18 4:56am    
sure
the left table "Table_data" is as follows

field1 field2
-------------------
1 2
3 4

the right table "Table_dictionary" is
field_code field_description
----------------------------------
1 one
2 two
3 three
4 four

expected to see in view

field1 field2
----------------------
one two
three four

SELECT dbo.Table_Data.field1, dbo.Table_Data.field2
FROM dbo.Table_Data LEFT OUTER JOIN
dbo.Table_Dictionary ON dbo.Table_Data.field2 = dbo.Table_Dictionary.field_code AND dbo.Table_Data.field1 = dbo.Table_Dictionary.field_code


that is "one chained dictionary table <- many fields"

Quote:
that is "one chained dictionary table <- many fields"

You need two JOINs, one for each column in the original table:
SQL
SELECT d1.field_description AS Field1, d2.field_description AS Field2 FROM Table_data t
JOIN Table_dictionary d1 ON t.field1 = d1.field_code
JOIN Table_dictionary d2 ON t.field2 = d2.field_code
 
Share this answer
 
SQL
--Normal view is invalid after dropping view dependency table.
--we can't drop view dependency table by using "WITH SCHEMABINDING" option in view.
    
CREATE view VName WITH SCHEMABINDING 
 AS 
SELECT  
     MFU.PrintFormatMaximum,
     MFU.PrintFormatMinimum

FROM  dbo.MFU LEFT OUTER JOIN dbo.PrintFormatTypes 
 
  ON dbo.MFU.PrintFormatMinimum = dbo.PrintFormatTypes.Code AND
     dbo.MFU.PrintFormatMaximum = dbo.PrintFormatTypes.Code
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900