您可以在此使用CTE做或派生表和一些條件聚合。我不知道你的意思,無結塊,我不知道這可能不匯聚:
CTE:
CREATE TABLE FACT_KPI (ID_KPI int, KPI_VALUE int)
CREATE TABLE DIM_KPI (ID_KPI int, KPI_LABEL varchar(25))
INSERT INTO FACT_KPI VALUES (1,25)
,(1,30)
,(2,15)
,(2,7)
,(3,12)
,(3,13)
INSERT INTO DIM_KPI VALUES (1, 'nb_departure')
,(2, 'nb_arrival')
,(3, 'headcount')
;WITH cte_example
AS
(SELECT F_KPI.ID_KPI, KPI_VALUE, KPI_LABEL
FROM FACT_KPI F_KPI
INNER JOIN DIM_KPI ON F_KPI.ID_KPI = DIM_KPI.ID_KPI)
SELECT CASE WHEN ID_KPI = 1 THEN KPI_VALUE END AS nb_departure,
CASE WHEN ID_KPI = 2 THEN KPI_VALUE END AS nb_arrival,
CASE WHEN ID_KPI = 3 THEN KPI_VALUE END AS headcount
FROM cte_example
派生表:
CREATE TABLE FACT_KPI (ID_KPI int, KPI_VALUE int)
CREATE TABLE DIM_KPI (ID_KPI int, KPI_LABEL varchar(25))
INSERT INTO FACT_KPI VALUES (1,25)
,(1,30)
,(2,15)
,(2,7)
,(3,12)
,(3,13)
INSERT INTO DIM_KPI VALUES (1, 'nb_departure')
,(2, 'nb_arrival')
,(3, 'headcount')
SELECT CASE WHEN ID_KPI = 1 THEN KPI_VALUE END AS nb_departure,
CASE WHEN ID_KPI = 2 THEN KPI_VALUE END AS nb_arrival,
CASE WHEN ID_KPI = 3 THEN KPI_VALUE END AS headcount
FROM
(SELECT F_KPI.ID_KPI, KPI_VALUE, KPI_LABEL
FROM FACT_KPI F_KPI
INNER JOIN DIM_KPI ON F_KPI.ID_KPI = DIM_KPI.ID_KPI) FACT_KPI
其他注意事項,請使用ASCII請下次參閱表格並請閱讀Sean Lange評論過的內容。
這是一個偉大的地方開始。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ –