Solved

Create Complex View in SQL Server 2005

Posted on 2013-01-17
3
448 Views
Last Modified: 2013-01-17
Greetings Experts!

I am trying to wrap my head around a issue I have not encountered before.

I have a table with the following structure:

fk_E01_01 (FK, int, not null)
NAME (varchar(50), not null)
VALUE (varchar(30), not null)

This results in a table like the one below

fk_E01_01   |   Name                  |   VALUE
-------------------------------------------------------------
123457        |   Trauma                |   No Activation
123458        |   Key                      |   102
123458        |   Section                 |   15
123458        |   CardNumber        |   17
123458        |   Level                    |   A
123458        |   Determinant         |   02
123459        |   Key                      |   170
123459        |   Section                |   20
123459        |   Quarter Section   |   A
123459        |   Trauma                |   Activation
123460…..
     
What I need is a view that concatenates the value for “Key” and “Section” on fk_E01_01 to result like this:

fk_E01_01   |     MapKey
-----------------------------------
123458        |   102-15
123459        |   170-20
123460…..

I have very little SQL training and this is beyond me.

Using SQL Server 2005

Thanks,

Task
0
Comment
Question by:taskhill
3 Comments
 
LVL 14

Expert Comment

by:Emes
ID: 38788353
Try

select
fk_E01_01
NAME +VALUE
from table
group by   fk_E01_01,NAME +VALUE
0
 
LVL 11

Accepted Solution

by:
Simone B earned 500 total points
ID: 38788381
Is there always only one Name value (Key or Section) for each fk? If so, this should work:

CREATE VIEW MyView
AS
SELECT A.fk_E01_01, A.[Value] + '-' + B.[Value] AS MapKey
FROM 
	(SELECT fk_E01_01, [value]
	FROM MyTable
	WHERE [Name] = 'Key') A
INNER JOIN 
	(SELECT fk_E01_01, [value]
	FROM MyTable
	WHERE [Name] = 'Section') B
ON A.fk_E01_01 = B.fk_E01_01

Open in new window

0
 

Author Closing Comment

by:taskhill
ID: 38788425
Flawless!!!!  Thank you!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to prevent deleting SQL tables thru FE Application? 20 85
SSIS On fail action 5 38
backup job space check 4 43
SQL Log size 3 17
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question