PDA

View Full Version : SQL pivot, filter, and most recent



Mike
04-10-2012, 09:50 AM
Hi all,

I have a SQL database with a lot of tables with data being regularly modified. Some of the data needs to be transferred to another database on another server when it is updated.

I have an audit table that records every record when it is edited, however it records them in a field per row format, so for every field in an edited record there's a new row in the audit table to record the value of the field from that edited record. It records every field whether it has been edited or not.

I would like to have a view (or something) that pivots those fields into a single row for each edited record, filters to only have the data I need (so ignoring edited records in tables I don't care about), and the only display the most recent edit on any given record (so if its edited twice, I only need the most recent to update my second database).

Any ideas how to do any/all of the above? :)

Example of Audit Table:


AUDITID
RECORDID
EDITTABLE
EDITDATE
EDITUSER
EDITFIELD
EDITVALUE


1
1
Computers
2012-09-29 10:10:14.547
Mike
Brand
Acer


2
1
Computers
2012-09-29 10:10:14.547
Mike
RAM
2


3
1
Computers
2012-09-29 10:10:14.547
Mike
Colour
Red


4
2
Chairs
2012-09-29 10:23:52.132
Bob
Brand
Comfy


5
2
Chairs
2012-09-29 10:23:52.132
Bob
Colour
Blue


6
2
Chairs
2012-09-29 10:23:52.132
Bob
Wheels
5


7
2
Chairs
2012-09-29 10:23:52.132
Bob
Arms
No


8
3
Bottles
2012-09-29 11:19:23.424
Mike
Capacity
300


9
3
Bottles
2012-09-29 11:19:23.424
Mike
Lid
Yes


10
1
Computers
2012-09-30 14:32:26.223
Mike
Brand
HP


11
1
Computers
2012-09-30 14:32:26.223
Mike
RAM
4


12
1
Computers
2012-09-30 14:32:26.223
Mike
Colour
Red


13
4
Chairs
2012-10-01 08:47:39.353
Sarah
Brand
NotSoComfy


14
4
Chairs
2012-10-01 08:47:39.353
Sarah
Colour
Blue


15
4
Chairs
2012-10-01 08:47:39.353
Sarah
Wheels
3


16
4
Chairs
2012-10-01 08:47:39.353
Sarah
Arms
Yes




So above record 1 is edited twice, so I'd only need the most recent edits for it. The values in EDITFIELD would need to be columns, and values in EDITVALUE needs to be the values in those columns.


Any ideas?

Cheers,
Mike.

Erayd
04-10-2012, 10:12 AM
Which database engine are you using? The syntax for pivots of any kind tend to be very engine-specific, and many databases can't actually do a pivot at all - in those cases, the result can usually be achieved using dynamic SQL.

Mike
04-10-2012, 10:19 AM
Which database engine are you using?Microsoft SQL Server 2008 R2 for database one, MS SQL Server 2008 for database two.

Cheers,
Mike.

Erayd
04-10-2012, 11:08 AM
MS-SQL can pivot natively - documentation for that feature is here (http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx).

Mike
05-10-2012, 09:00 AM
MS-SQL can pivot natively - documentation for that feature is here (http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx).Thanks I'll have a play with that. Will that also allow me to filter to only display certain tables (see EDITTABLE in my example) and also only the most recent for each record?

Cheers,
Mike.

Erayd
05-10-2012, 09:33 AM
Thanks I'll have a play with that. Will that also allow me to filter to only display certain tables (see EDITTABLE in my example) and also only the most recent for each record?That's what WHERE and HAVING are for :).

kingdragonfly
05-10-2012, 01:00 PM
I suggest you're looking for a "TOP 1 INNER SELECT" also known as "a select within a select". Not to be confused with a "INNER JOIN"

This is not really a question for this forum, as it's an advanced feature of SQL server.

I suggest using the SQLCentral.com forum.