SQL查询Microsoft Access - 将水平字段更改为垂直字段

时间:2015-05-22 02:58:03

标签: sql ms-access

我需要访问SQL Query的帮助。 我使用4表创建访问视图,当我想将某些字段更改为垂直时,我的问题显示。我知道如果两个矩阵,但如果超过它我可以' t。

这是我在改变之前的样子

|DataKioskID | KioskName   | YearFiscal | MonthReport | ProductID | ProductName | Sales | Stock |
|AB0101061501| Sarana Tani |       2015 |           6 |       P15 |     Advanta |     56|     12|
|AB0101061501| Sarana Tani |       2015 |           6 |       P16 |     Advanta |     23|     15|
|AB0101061501| Sarana Tani |       2015 |           6 |       P02 |     Advanta |     14|     12|
|AB0102061501| TaniLestari |       2015 |           6 |       P02 |     Advanta |     15|     14|
|AB0102061501| TaniLestari |       2015 |           6 |       P15 |     Advanta |     12|     15|
|AB0102061501| TaniLestari |       2015 |           6 |       P16 |     Advanta |     14|     23|

代码:

SELECT Data_Kiosk_Header.DataKioskID, Master_Kiosk.KioskName, Data_Kiosk_Header.YearFiscal
, Max(Data_Kiosk_Header.MonthReport) AS monthReport
, Max(IIf(Data_Kiosk_Detail.ProductID='P15',Data_Kiosk_Detail.Sales,0)) AS Advanta_Sales
, Max(IIf(Data_Kiosk_Detail.ProductID='P16',Data_Kiosk_Detail.Sales,0)) AS Agro_Sales
, Max(IIf(Data_Kiosk_Detail.ProductID='P02',Data_Kiosk_Detail.Sales,0)) AS P12_Sales
, Max(IIf(Data_Kiosk_Detail.ProductID='P15',Data_Kiosk_Detail.Stocks,0)) AS Advanta_Stocks
, Max(IIf(Data_Kiosk_Detail.ProductID='P16',Data_Kiosk_Detail.Stocks,0)) AS Agro_Stocks
, Max(IIf(Data_Kiosk_Detail.ProductID='P02',Data_Kiosk_Detail.Stocks,0)) AS P12_Stocks
FROM Master_Kiosk 
INNER JOIN (Data_Kiosk_Header INNER JOIN (Data_Kiosk_Detail 
INNER JOIN Master_Product ON Data_Kiosk_Detail.ProductID = Master_Product.ProductID) ON Data_Kiosk_Header.DataKioskID = Data_Kiosk_Detail.DataKioskID) ON Master_Kiosk.kioskid = Data_Kiosk_Header.KioskName
GROUP BY Data_Kiosk_Header.DataKioskID, Master_Kiosk.KioskName, Data_Kiosk_Header.YearFiscal;

代码变成这样:

DataKioskID |   KioskName   |YearFiscal |monthReport    |Advanta_Sales  |Agro_Sales |P12_Sales  |Advanta_Stocks |Agro_Stocks    |P12_Stocks |
AB0101061501|   Sarana Tani |2015       |6              |56         |23         |14         |12         |15         |12         |
AB0102061501|   Tani Lestari|2015       |6              |12         |14         |15         |15         |23         |14         |

有人可以帮助我吗?,我想要这样。

|DataKioskID | KioskName   | YearFiscal | MonthReport |         Sales          |         Stock          |
|            |             |            |             | Advanta | Agro  | P12  | Advanta | Agro  | P12  |      
|AB0101061501| Sarana Tani |       2015 |           6 |      56 |     23|    14|      12 |     15|    12|
|AB0102061501| LestariTani |       2015 |           6 |      15 |     12|    14|      14 |     15|    16|

在这里我给DB你可以试试我的意思: DB Source

1 个答案:

答案 0 :(得分:1)

至少在查询级别上,你想要的正确是不可能的,因为你有2级分组...报告就是答案 此外,为了获得"单一"查询您需要以下
首先是销售的交叉表查询

TRANSFORM Max(Data_Kiosk_Detail.Sales) AS MaxOfSales

SELECT Data_Kiosk_Header.DataKioskID
,Master_Kiosk.KioskName
,Data_Kiosk_Header.YearFiscal
,Data_Kiosk_Header.MonthReport AS monthReport
,"Sales" AS Info
FROM Master_Kiosk
INNER JOIN (
Data_Kiosk_Header INNER JOIN (
    Data_Kiosk_Detail INNER JOIN Master_Product ON Data_Kiosk_Detail.ProductID = Master_Product.ProductID
    ) ON Data_Kiosk_Header.DataKioskID = Data_Kiosk_Detail.DataKioskID
) ON Master_Kiosk.kioskid = Data_Kiosk_Header.KioskName
GROUP BY Data_Kiosk_Header.DataKioskID
,Master_Kiosk.KioskName
,Data_Kiosk_Header.YearFiscal
,Data_Kiosk_Header.MonthReport
,"Sales"
PIVOT Data_Kiosk_Detail.ProductID;

第二个股票交叉表查询

TRANSFORM Max(Data_Kiosk_Detail.Stocks) AS MaxOfStocks

SELECT Data_Kiosk_Header.DataKioskID
,Master_Kiosk.KioskName
,Data_Kiosk_Header.YearFiscal
,Data_Kiosk_Header.MonthReport AS monthReport
,"Stocks" AS Info
FROM Master_Kiosk
INNER JOIN (
Data_Kiosk_Header INNER JOIN (
    Data_Kiosk_Detail INNER JOIN Master_Product ON Data_Kiosk_Detail.ProductID = Master_Product.ProductID
    ) ON Data_Kiosk_Header.DataKioskID = Data_Kiosk_Detail.DataKioskID
) ON Master_Kiosk.kioskid = Data_Kiosk_Header.KioskName
GROUP BY Data_Kiosk_Header.DataKioskID
,Master_Kiosk.KioskName
,Data_Kiosk_Header.YearFiscal
,Data_Kiosk_Header.MonthReport
,"Stocks"
PIVOT Data_Kiosk_Detail.ProductID;

然后将它们与联合查询一起加入

select * from MaxOfSales
UNION select * from MaxOfStocks;

然后,您可以使用上述查询创建报告以显示您需要的内容