简体   繁体   中英

SQL Query to transpose rows to columns in MS Access database

I am using MS Access DB it has a table named 'tssStockMaster' with fields (PartNo, ItemName,Stock,Workshop) as shown in the image with following data.

表格:tssStockMaster与数据

Now I am unable to write a query in MS Access Database using SQL to get the following output such that the stock quantity is shown for each workshops against each item name(group by PartNo).

在ms Access数据库中使用SQL查询的必需输出

Since we cant use PIVOT in MS Access how can I achieve this ?

If you know up front how many workshops will be there and the number isn't significant I bet you could use aggregate function with if conditions:

select 
    partno
  , itemname
  , sum(iif(workshop = 'W101', stock, 0)) as w101
  , sum(iif(workshop = 'Z239', stock, 0)) as z239
from t
group by partno, itemname

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM