Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need help in building a SQL query? Expand / Collapse
Author
Message
Posted Monday, September 16, 2013 1:42 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 17, 2013 9:03 AM
Points: 1, Visits: 9
Hi Experts,

I have table called "Entries" having general information of Employees as...

TableName: Entries

Select * from Entries where mskey = 1234

MSKEY, ATTRNAME, AVALUE
1234, NUMBER, 3
1234, FNAME, DAVID
1234, MNAME, M
1234, LNAME, HUDSON

Select * from Entries where mskey = 2345

MSKEY, ATTRNAME, AVALUE
2345, NUMBER, 5
2345, FNAME, MARK
2345, LNAME, WOOD
2345, EMAIL, mwood@email.com

NOTE: MSKEY is the Primary Key for the table Entries. NUMBER is also primary key.

I need to write a SQL query and the output should look like...

NUMBER, FNAME, MNAME, LNAME, EMAIL
1234, DAVID, M, HUDSON, NULL (Blank)
2345, MARK, NULL (Blank), WOOD, mwood@email.com

I did a SQL query as...

select a.avalue as [NUMBER], b.avalue as [FNAME], c.avalue as [MNAME], 
d.avalue as [LNAME], e.avalue as [EMAIL]
from Entries.a, Entries.b, Entries.c, Entries.d, Entries.e
where a.mskey = b.mskey and b.mskey = c.mskey and c.mskey = d.mskey and
d.mskey = e.mskey and a.attrname = 'NUMBER' and b.attrname = 'FNAME' and
c.attrname = 'MNAME' and d.attrname = 'LNAME' and e.attrname = 'EMAIL' and
a.mskey in
(select mskey from Entries where avalue in ('1234','2345'))

Please help me to write a SQL query using the table "Entries". Thanks in advance.

Regards,
Arun
Post #1495262
Posted Monday, September 16, 2013 2:45 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 13,138, Visits: 11,977
Hi and welcome to the forums. It is difficult to provide you a lot of direction because you didn't provide ddl and sample data for the table. Please take a few minutes and read the first article in my signature for best practices when posting questions.

One thing I can say for sure. You have an EAV "design" pattern. This is one of the most difficult patterns to work with and the performance can horribly wrong very quickly.

I would recommend that you use ANSI-92 style joins instead of the ANSI-89 style joins. They are easier to read and less prone to accidental cross joins.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1495286
Posted Monday, September 16, 2013 4:05 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, July 28, 2014 4:49 AM
Points: 556, Visits: 2,581
Below is some DDL and a solution:

--DDL 
USE tempdb

IF OBJECT_ID('tempdb..Entries') IS NOT NULL DROP TABLE Entries;
CREATE TABLE Entries (MSKEY int, ATTRNAME varchar(20), AVALUE varchar(20), primary key (MSKEY, ATTRNAME));

INSERT Entries (MSKEY, ATTRNAME, AVALUE)
SELECT 1234, 'NUMBER', '3' UNION ALL
SELECT 1234, 'FNAME', 'DAVID' UNION ALL
SELECT 1234, 'MNAME', 'M' UNION ALL
SELECT 1234, 'LNAME', 'HUDSON' UNION ALL
SELECT 2345, 'NUMBER', '5' UNION ALL
SELECT 2345, 'FNAME', 'MARK' UNION ALL
SELECT 2345, 'LNAME', 'WOOD' UNION ALL
SELECT 2345, 'EMAIL', 'mwood@email.com';

--OUTPUT
SELECT DISTINCT
e.MSKEY AS NUMBER,
ISNULL(fn.AVALUE,'') AS FNAME,
ISNULL(mn.AVALUE,'') AS MNAME,
ISNULL(ln.AVALUE,'') AS LNAME,
ISNULL(em.AVALUE,'') AS EMAIL
FROM Entries e
LEFT JOIN Entries fn
ON e.MSKEY=fn.MSKEY AND fn.ATTRNAME='FNAME'
LEFT JOIN Entries mn
ON e.MSKEY=mn.MSKEY AND mn.ATTRNAME='MNAME'
LEFT JOIN Entries ln
ON e.MSKEY=ln.MSKEY AND ln.ATTRNAME='LNAME'
LEFT JOIN Entries em
ON e.MSKEY=em.MSKEY AND em.ATTRNAME='EMAIL'

The solution I provided is not optimal - it uses a very expensive DISTINCT. I'm going to try to put together a faster query (unless someone beats me to it) . This will, however, get you the results you need.


-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)
My blog
Post #1495317
Posted Monday, September 16, 2013 4:54 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, July 28, 2014 4:49 AM
Points: 556, Visits: 2,581
This could still be optimized but will perform a little better than my previous query:

--OUTPUT
SELECT e.MSKEY AS NUMBER,
ISNULL(fn.AVALUE,'') AS FNAME,
ISNULL(mn.AVALUE,'') AS MNAME,
ISNULL(ln.AVALUE,'') AS LNAME,
ISNULL(em.AVALUE,'') AS EMAIL
FROM Entries e
LEFT JOIN Entries fn
ON e.MSKEY=fn.MSKEY AND fn.ATTRNAME='FNAME'
LEFT JOIN Entries ln
ON e.MSKEY=ln.MSKEY AND ln.ATTRNAME='LNAME'
LEFT JOIN Entries mn
ON e.MSKEY=mn.MSKEY AND mn.ATTRNAME='MNAME'
LEFT JOIN Entries em
ON e.MSKEY=em.MSKEY AND em.ATTRNAME='EMAIL'
WHERE e.ATTRNAME=fn.ATTRNAME



-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)
My blog
Post #1495323
Posted Monday, September 16, 2013 5:13 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 4:19 PM
Points: 1,786, Visits: 5,682
There is no need for all those joins.

select e.MSKEY
, max(case when e.ATTRNAME='FNAME' then e.AVALUE end) AS FNAME
, max(case when e.ATTRNAME='MNAME' then e.AVALUE end) AS MNAME
, max(case when e.ATTRNAME='LNAME' then e.AVALUE end) AS LNAME
, max(case when e.ATTRNAME='EMAIL' then e.AVALUE end) AS EMAIL
from Entries AS e
group by e.MSKEY



MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1495324
    Posted Monday, September 16, 2013 6:52 PM


    SSC-Dedicated

    SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

    Group: General Forum Members
    Last Login: Today @ 8:58 PM
    Points: 36,794, Visits: 31,253
    Just to backup Maggo's solution... the method he used is called a "CROSSTAB". You could also use a PIVOT although the CROSSTAB solution is usually a touch faster. Such a solution is one of the better ones because it only has to make a pass at the table one time. To wit, it's a common solution for "pivoting" EAV tables of this nature.


    --Jeff Moden
    "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

    First step towards the paradigm shift of writing Set Based code:
    Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

    (play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

    Helpful Links:
    How to post code problems
    How to post performance problems
    Post #1495341
    Posted Tuesday, September 17, 2013 8:56 AM


    Mr or Mrs. 500

    Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

    Group: General Forum Members
    Last Login: Monday, July 28, 2014 4:49 AM
    Points: 556, Visits: 2,581
    Well done Magoo; I knew there was a much better way but drew a blank.

    Jeff Moden (9/16/2013)
    Just to backup Maggo's solution... the method he used is called a "CROSSTAB". You could also use a PIVOT although the CROSSTAB solution is usually a touch faster. Such a solution is one of the better ones because it only has to make a pass at the table one time. To wit, it's a common solution for "pivoting" EAV tables of this nature.


    I wish I had read Cross Tabs and Pivots, Part 1 – Converting Rows to Columns before putting my query together.


    -- Alan Burstein



    Read this article for best practices on asking questions.
    Need to split a string? Try this (Jeff Moden)
    Need a pattern-based string spitter? Try this (Dwain Camps)
    My blog
    Post #1495545
    Posted Tuesday, September 17, 2013 4:30 PM


    SSC-Dedicated

    SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

    Group: General Forum Members
    Last Login: Today @ 8:58 PM
    Points: 36,794, Visits: 31,253
    Alan.B (9/17/2013)
    Well done Magoo; I knew there was a much better way but drew a blank.

    Jeff Moden (9/16/2013)
    Just to backup Maggo's solution... the method he used is called a "CROSSTAB". You could also use a PIVOT although the CROSSTAB solution is usually a touch faster. Such a solution is one of the better ones because it only has to make a pass at the table one time. To wit, it's a common solution for "pivoting" EAV tables of this nature.


    I wish I had read Cross Tabs and Pivots, Part 1 – Converting Rows to Columns before putting my query together.




    --Jeff Moden
    "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

    First step towards the paradigm shift of writing Set Based code:
    Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

    (play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

    Helpful Links:
    How to post code problems
    How to post performance problems
    Post #1495719
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse