问题
I need to convert following Table using MS Access:
into How do I convert it? I am new to ms access 2016.
I have looked into the solution provided in ACCESS/SQL Combining multiple rows with one column into one row and creating multiple columns. But not able to get the result I want in my case.
回答1:
Here is the sql script which works on MS SQL server:
create table #T (Project varchar(50),Asset varchar(50), OrderNo int)
insert into #T (Project, Asset, OrderNo) values ('ABC','K11',3245)
insert into #T (Project, Asset, OrderNo) values ('ABC','K11',4564)
insert into #T (Project, Asset, OrderNo) values ('ABC','K11',5234)
insert into #T (Project, Asset, OrderNo) values ('XYZ','M33',5346)
insert into #T (Project, Asset, OrderNo) values ('XYZ','M33',8745)
declare @Count int,@Cur int
declare @Qry nvarchar(4000)
set @Cur=1
select @Count=max(t.count) from (
select count(OrderNo) count from #T group by Asset)t
set @Qry=''
while @Cur<@Count+1
begin
set @Qry= @Qry + ',(select t.OrderNo from (select ROW_NUMBER() over(order by OrderNo) ROW_NUMBER, OrderNo from #T where Asset= t1.Asset) t where ROW_NUMBER='+ cast(@Cur as varchar(10)) + ') as OrderNo' + cast(@Cur as varchar(10))
set @Cur=@Cur+1
end
set @Qry='select distinct t1.Project, t1.Asset ' + @Qry + ' from #T t1 ORDER BY t1.Project'
print @Qry
exec sp_executesql @Qry
drop table #T
and this is the result:
I do not think you can do it in MS Access.
回答2:
Here is pure Access solution using code. It may be done more efficient but you will get the idea now. It creates a temporary table Transposed
where you find your result. This is MyTable with data
and this is code for you:
Option Compare Database
Option Explicit
Public Function Transpose() As String
Dim ordersCnt As Integer, sql As String, i As Integer, tempTable As String, f As Variant, myTable As String
Dim insSql As String, fieldsSql As String, updateSql As String, updateSql2 As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim grp As DAO.Recordset
tempTable = "Transposed"
myTable = "MyTable"
ordersCnt = GetMaxOrders
Set db = CurrentDb()
If Not IsNull(DLookup("Name", "MSysObjects", "Name='" & tempTable & "'")) Then
DoCmd.DeleteObject acTable, tempTable
End If
fieldsSql = ""
sql = "CREATE TABLE " & tempTable & " (Project CHAR, Asset CHAR "
For i = 1 To ordersCnt
fieldsSql = fieldsSql & ", Order" & i & " INTEGER"
Next i
sql = sql & fieldsSql & ")"
db.Execute (sql)
insSql = "INSERT INTO " & tempTable & " (Project, Asset) VALUES ("
Set grp = db.OpenRecordset("SELECT DISTINCT Project, Asset FROM " & myTable & " GROUP BY Project, Asset")
grp.MoveFirst
Do While Not grp.EOF
sql = "'" & grp(0) & "','" & grp(1) & "')"
db.Execute insSql & sql
Set rs = db.OpenRecordset("SELECT * FROM " & myTable & " WHERE Project = '" & grp(0) & "' AND Asset = '" & grp(1) & "'")
updateSql = "UPDATE " & tempTable & " SET "
updateSql2 = ""
i = 0
rs.MoveFirst
Do While Not rs.EOF
i = i + 1
updateSql2 = updateSql2 & "Order" & i & "= " & rs(3) & ","
rs.MoveNext
Loop
updateSql = updateSql & Left(updateSql2, Len(updateSql2) - 1) & " WHERE Project = '" & grp(0) & "' AND Asset = '" & grp(1) & "'"
db.Execute updateSql
grp.MoveNext
Loop
End Function
Public Function GetMaxOrders()
Dim rst As DAO.Recordset
Dim strSQL As String
strSQL = "SELECT MAX(CountOfOrderNo) FROM (SELECT Count(OrderNo) AS CountOfOrderNo FROM MyTable GROUP BY Project, Asset) "
Set rst = CurrentDb.OpenRecordset(strSQL)
GetMaxOrders = rst(0)
rst.Close
Set rst = Nothing
End Function
and the result:
Enjoy.
来源:https://stackoverflow.com/questions/60638602/ms-access-convert-rows-values-into-columns-values