Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 9:24 AM
Points: 15,798, Visits: 16,160
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


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 9:54 AM
Points: 1,931, Visits: 6,441
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



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Post #1495317
Posted Monday, September 16, 2013 4:54 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 9:54 AM
Points: 1,931, Visits: 6,441
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



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Post #1495323
Posted Monday, September 16, 2013 5:13 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 5:31 PM
Points: 2,147, Visits: 7,498
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


select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);


  • 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-Forever

    SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

    Group: General Forum Members
    Last Login: Today @ 8:15 AM
    Points: 40,984, Visits: 38,281
    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."

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


    SSCommitted

    SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

    Group: General Forum Members
    Last Login: Today @ 9:54 AM
    Points: 1,931, Visits: 6,441
    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



    Best practices for getting help on SQLServerCentral
    Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
    Need a pattern-based splitter? Try PatternSplitCM
    Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

    "I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
    Post #1495545
    Posted Tuesday, September 17, 2013 4:30 PM


    SSC-Forever

    SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

    Group: General Forum Members
    Last Login: Today @ 8:15 AM
    Points: 40,984, Visits: 38,281
    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."

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

    Add to briefcase

    Permissions Expand / Collapse