1

我需要编写一个 sql select 语句,但我被卡住了。我正在尝试为复杂的 GRID 编写一个 sql。网格应该用于输入预算值。

这是我的网格应该看起来的样子。可以看到有 MAIN 行,每个主行有 4 个子行。我需要从 select 语句中产生相同的结果 在此处输入图像描述

不幸的是,我的选择语句产生如下数据表

SELECT TYPE,
   NO,
   DESCRIPTION,
   ISNULL((SELECT VALUE FROM RN_BUDGET_INVEST_VALUES V WHERE  V.BUDGET_ID=40 AND V.FIRMA_ID=2 AND V.DEPARTMENT_ID=200 AND V.MODULE_ID=6 AND V.YEAR=2013 AND V.COLUMN_INDEX=1 AND V.ROW_INDEX=1 AND V.CREATED_USER=1),0) AS 'OCAK',
   ISNULL((SELECT VALUE FROM RN_BUDGET_INVEST_VALUES V WHERE  V.BUDGET_ID=40 AND V.FIRMA_ID=2 AND V.DEPARTMENT_ID=200 AND V.MODULE_ID=6 AND V.YEAR=2013 AND V.COLUMN_INDEX=1 AND V.ROW_INDEX=1 AND V.CREATED_USER=1),0) AS 'ŞUBAT',
   ISNULL((SELECT VALUE FROM RN_BUDGET_INVEST_VALUES V WHERE  V.BUDGET_ID=40 AND V.FIRMA_ID=2 AND V.DEPARTMENT_ID=200 AND V.MODULE_ID=6 AND V.YEAR=2013 AND V.COLUMN_INDEX=1 AND V.ROW_INDEX=1 AND V.CREATED_USER=1),0) AS 'MART',
   ISNULL((SELECT VALUE FROM RN_BUDGET_INVEST_VALUES V WHERE  V.BUDGET_ID=40 AND V.FIRMA_ID=2 AND V.DEPARTMENT_ID=200 AND V.MODULE_ID=6 AND V.YEAR=2013 AND V.COLUMN_INDEX=1 AND V.ROW_INDEX=1 AND V.CREATED_USER=1),0) AS 'NİSAN',
   ISNULL((SELECT VALUE FROM RN_BUDGET_INVEST_VALUES V WHERE  V.BUDGET_ID=40 AND V.FIRMA_ID=2 AND V.DEPARTMENT_ID=200 AND V.MODULE_ID=6 AND V.YEAR=2013 AND V.COLUMN_INDEX=1 AND V.ROW_INDEX=1 AND V.CREATED_USER=1),0) AS 'MAYIS',
FROM RN_BUDGET_INVESTMENT_VARIABLES VARS

在此处输入图像描述

我不知道如何创建子行。你能给我一个 iade 吗?

4

1 回答 1

1

你想做一个 RIGHT OUTER JOIN 让它看起来有点相似。此外,您必须加入表之间的外键关系,例如 BudgetNr 或 FIRMA_ID 或 CREATED_USER 或表之间的某些关系。此外,您的 WHERE 语句都返回相同的数据,因此您需要更改它们,因为它们都具有相同的 BUDGET_ID、FIRMA_ID、DEPARTMENT_ID、MODULE_ID、YEAR、COLUMN_INDEX、ROW_INDEX、CREATED_USER

     SELECT TYPE,
        NO,
        DESCRIPTION,
        ISNULL(V1.VALUE,0) AS 'OCAK',
        ISNULL(V2.VALUE,0) AS 'ŞUBAT',
        ISNULL(V3.VALUE,0) AS 'MART',
        ISNULL(V4.VALUE,0) AS 'NİSAN',
        ISNULL(V5.VALUE,0) AS 'MAYIS',

     FROM RN_BUDGET_INVESTMENT_VARIABLES VARS
     RIGHT OUTER JOIN RN_BUDGET_INVEST_VALUES V1 ON V.[LinkKeyto RN_BUDGET_INVESTMENT_VARIABLES table] = [LinkedKey in this table] WHERE  V1.BUDGET_ID=40 AND V1.FIRMA_ID=2 AND V1.DEPARTMENT_ID=200 AND V1.MODULE_ID=6 AND V1.YEAR=2013 AND V1.COLUMN_INDEX=1 AND V1.ROW_INDEX=1 AND V1.CREATED_USER=1
     RIGHT OUTER JOIN RN_BUDGET_INVEST_VALUES V2 ON V.[LinkKeyto RN_BUDGET_INVESTMENT_VARIABLES table] = [LinkedKey in this table] WHERE  V2.BUDGET_ID=40 AND V2.FIRMA_ID=2 AND V2.DEPARTMENT_ID=200 AND V2.MODULE_ID=6 AND V2.YEAR=2013 AND V2.COLUMN_INDEX=1 AND V2.ROW_INDEX=1 AND V2.CREATED_USER=1
     RIGHT OUTER JOIN RN_BUDGET_INVEST_VALUES V3 ON V.[LinkKeyto RN_BUDGET_INVESTMENT_VARIABLES table] = [LinkedKey in this table] WHERE  V3.BUDGET_ID=40 AND V3.FIRMA_ID=2 AND V3.DEPARTMENT_ID=200 AND V3.MODULE_ID=6 AND V3.YEAR=2013 AND V3.COLUMN_INDEX=1 AND V3.ROW_INDEX=1 AND V3.CREATED_USER=1
     RIGHT OUTER JOIN RN_BUDGET_INVEST_VALUES V4 ON V.[LinkKeyto RN_BUDGET_INVESTMENT_VARIABLES table] = [LinkedKey in this table] WHERE  V4.BUDGET_ID=40 AND V4.FIRMA_ID=2 AND V4.DEPARTMENT_ID=200 AND V4.MODULE_ID=6 AND V4.YEAR=2013 AND V4.COLUMN_INDEX=1 AND V4.ROW_INDEX=1 AND V4.CREATED_USER=1
     RIGHT OUTER JOIN RN_BUDGET_INVEST_VALUES V5 ON V.[LinkKeyto RN_BUDGET_INVESTMENT_VARIABLES table] = [LinkedKey in this table] WHERE  V5.BUDGET_ID=40 AND V5.FIRMA_ID=2 AND V5.DEPARTMENT_ID=200 AND V5.MODULE_ID=6 AND V5.YEAR=2013 AND V5.COLUMN_INDEX=1 AND V5.ROW_INDEX=1 AND V5.CREATED_USER=1

您可能还想查看PIVOT。我认为这更符合您的要求。

于 2013-10-29T13:22:33.710 回答