[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

S Chapman

3/23/2007 10:13:00 AM


I have a table that stores data as EAV as follows:

--------------------------------------------------------------------
ID | Name | Str Value| Date Value| Num Value
--------------------------------------------------------------------
01 | Type | ABC | |
01 | Count | | | 100
01 | DOB | | 01/01/07 |

02 | Type | DEF | |
02 | Count | | | 200
02 | DOB | | 02/01/07 |

03 | Type | GHI | |
03 | Count | | | 400
03 | DOB | | 03/01/07 |

I am trying to extract the results in the following form:

ID, Name, DOB, Count
----------------------------------------
01,ABC, 01/01/07,100
02,DEF, 02/01/07,200
03,GHI, 03/01/07,300

I can't seem to work out an elegant way of extrating the results using
Sql. The columns ID, Name, DOB and Count are userdefined which makes
it more complicated.

Any help would be greatly appreciated.

Thanks.

3 Answers

Rahul

3/23/2007 11:10:00 AM

0

On Mar 23, 3:12 pm, "S Chapman" <s_chapma...@hotmail.co.uk> wrote:
> I have a table that stores data as EAV as follows:
>
> --------------------------------------------------------------------
> ID | Name | Str Value| Date Value| Num Value
> --------------------------------------------------------------------
> 01 | Type | ABC | |
> 01 | Count | | | 100
> 01 | DOB | | 01/01/07 |
>
> 02 | Type | DEF | |
> 02 | Count | | | 200
> 02 | DOB | | 02/01/07 |
>
> 03 | Type | GHI | |
> 03 | Count | | | 400
> 03 | DOB | | 03/01/07 |
>
> I am trying to extract the results in the following form:
>
> ID, Name, DOB, Count
> ----------------------------------------
> 01,ABC, 01/01/07,100
> 02,DEF, 02/01/07,200
> 03,GHI, 03/01/07,300
>
> I can't seem to work out an elegant way of extrating the results using
> Sql. The columns ID, Name, DOB and Count are userdefined which makes
> it more complicated.
>
> Any help would be greatly appreciated.
>
> Thanks.

Hi,

As you send the sample data, you can only done it by following
query, otherewise you have to pivot it.


Select ID, [Str Value] as Name, [Date Value] as DOB, [Num Value] as
Count
>From tbl
Group by ID, [Str Value]

Rahul

bob

3/23/2007 11:12:00 AM

0

A few methods:

DROP TABLE #eav
GO
CREATE TABLE #eav ( id CHAR(2) NOT NULL, attribute_name VARCHAR(20) NOT
NULL, string_value VARCHAR(50), date_value DATETIME, numeric_value INT,
PRIMARY KEY ( id, attribute_name ) )
GO

INSERT INTO #eav ( id, attribute_name, string_value ) VALUES ( '01', 'Type',
'ABC' )
INSERT INTO #eav ( id, attribute_name, date_value ) VALUES ( '01', 'DOB',
'20070101' )
INSERT INTO #eav ( id, attribute_name, numeric_value ) VALUES ( '01',
'Count', '100' )
INSERT INTO #eav ( id, attribute_name, string_value ) VALUES ( '02', 'Type',
'DEF' )
INSERT INTO #eav ( id, attribute_name, date_value ) VALUES ( '02', 'DOB',
'20070102' )
INSERT INTO #eav ( id, attribute_name, numeric_value ) VALUES ( '02',
'Count', '200' )
INSERT INTO #eav ( id, attribute_name, string_value ) VALUES ( '03', 'Type',
'GHI' )
INSERT INTO #eav ( id, attribute_name, date_value ) VALUES ( '03', 'DOB',
'20070103' )
INSERT INTO #eav ( id, attribute_name, numeric_value ) VALUES ( '03',
'Count', '400' )
GO


-- One solution, plus suitable for SQL 2000
SELECT
id,
MAX( CASE WHEN attribute_name = 'Type' THEN string_value END ),
MAX( CASE WHEN attribute_name = 'DOB' THEN date_value END ),
MAX( CASE WHEN attribute_name = 'Count' THEN numeric_value END )
FROM #eav
GROUP BY id
GO

-- Or table for each attribute ( also SQL 2000 )
SELECT
a.id,
a.string_value AS 'Type',
b.date_value AS 'DOB',
c.numeric_value AS 'Count'
FROM #eav a
LEFT JOIN #eav b ON a.id = b.id AND b.attribute_name = 'DOB'
LEFT JOIN #eav c ON a.id = c.id AND c.attribute_name = 'Count'
WHERE a.attribute_name = 'Type'
GO

-- Get everything into one column, then pivot it - SQL 2005 method
SELECT *
FROM
(
SELECT
a.id,
attribute_name,
COALESCE ( string_value, CAST( date_value AS VARCHAR ), CAST(
numeric_value AS VARCHAR ) ) AS column_data
FROM #eav a
) x
PIVOT ( MAX( column_data ) FOR attribute_name In ( [Type], [DOB], [Count] )
) pvt


Let me know how you get on.

wBob

"S Chapman" wrote:

>
> I have a table that stores data as EAV as follows:
>
> --------------------------------------------------------------------
> ID | Name | Str Value| Date Value| Num Value
> --------------------------------------------------------------------
> 01 | Type | ABC | |
> 01 | Count | | | 100
> 01 | DOB | | 01/01/07 |
>
> 02 | Type | DEF | |
> 02 | Count | | | 200
> 02 | DOB | | 02/01/07 |
>
> 03 | Type | GHI | |
> 03 | Count | | | 400
> 03 | DOB | | 03/01/07 |
>
> I am trying to extract the results in the following form:
>
> ID, Name, DOB, Count
> ----------------------------------------
> 01,ABC, 01/01/07,100
> 02,DEF, 02/01/07,200
> 03,GHI, 03/01/07,300
>
> I can't seem to work out an elegant way of extrating the results using
> Sql. The columns ID, Name, DOB and Count are userdefined which makes
> it more complicated.
>
> Any help would be greatly appreciated.
>
> Thanks.
>
>

Dave Peterson

4/1/2010 5:11:00 PM

0

Second was the stuff after "instead".

I'd look for a variable named "Name" or a module named "Name" or a
procedure/function named "Name".

That's another string of letters that you shouldn't use for any of those things.



kevlarmcc wrote:
>
> I thought I was choosing to use the object itself rather than a variable for
> it. I get the same results with using a variable such as "ws". Thanks for the
> help, as I am new to this. What's second?
>
> "Dave Peterson" wrote:
>
> > First, using a variable named Worksheet is a bad idea. Excel has an object that
> > uses that name (it's a worksheet <vbg>.)
> >
> > Instead
> >
> > Option Explicit
> > Sub RunAll()
> > dim wks as worksheet
> > for each wks in worksheets
> > case "Report 1"
> > ......
> >
> >
> >
> > kevlarmcc wrote:
> > >
> > > I have two copies of code that I swear are indentical except for the
> > > captialization of an object, which seems to be automatic. Somehow one copy of
> > > the code doesn't auto cap the object and it is that copy that works. Code is
> > > below:
> > >
> > > Sub RunAll()
> > >
> > > For Each Worksheet In Worksheets
> > > Select Case Worksheet.Name
> > > Case "Report 1"
> > > Case Else
> > > Worksheet.Activate
> > > If ActiveSheet.Name = "BGE" Then _
> > > ActiveSheet.Name = "Sheet999"
> > > End Select
> > > Next Worksheet
> > > End Sub
> > >
> > > When Name is capped, it doesn't work. With lowercase name it works. I can't
> > > seem to replicate the non-capped object because when writing new code it
> > > won't let me not capitalize it. I am not sure what I did to get the object
> > > not capitalized in the first place. Anyone understand this?
> >
> > --
> >
> > Dave Peterson
> > .
> >

--

Dave Peterson