tsql - Converting Access Pivot Table to SQL Server -
tsql - Converting Access Pivot Table to SQL Server -
i'm having problem converting ms access pivot table on sql server. hoping might help..
transform first(contacts.value) firstofvalue select contacts.contactid contacts right bring together contactrecord on contacts.[detailid] = contactrecord.[detailid] grouping contacts.contactid pivot contactrecord.wellknownname ; edit: responding of comments
contacts table has 3 fields
contactid | detailid | value | 1 1 scott contactrecord has like
detailid | wellknownname 1 | firstname 2 | address1 3 | foobar contractrecord dyanamic in user @ anytime can create field added contacts
the access query pulls out
contactid | firstname | address1 | foobar 1 | scott | null | null which pivot on wellknownname. key here number of columns dynamic since user can, @ anytime, create field contact. beingness new pivot tables altogether, i'm wondering how can recreate access query in sql server.
as transform... that's built in access function. more info found here. first() take first result on matching row.
i hope helps , appreciate help.
i quick search dynamic pivot tables comes this article.
after renaming things in lastly query on page came this:
declare @pivotcolumnheaders varchar(max); select @pivotcolumnheaders = coalesce(@pivotcolumnheaders + ',['+ cast(wellknownname varchar) + ']','['+ cast(wellknownname varchar) + ']') contactrecord; declare @pivottablesql nvarchar(max); set @pivottablesql = n' select * ( select c.contactid, cr.wellknownname, c.value contacts c right bring together contactrecord cr on c.detailid = cr.detailid ) pivotdata pivot( min(value) wellknownname in (' + @pivotcolumnheaders +') ) pivottable ' ; execute(@pivottablesql); which despite ugliness, job
tsql ms-access
Comments
Post a Comment