SQL Server 2008指定列已多次指定

时间:2017-01-19 09:08:39

标签: sql sql-server sql-server-2008 multiple-columns

以下代码抛出以下异常。我该如何解决这个问题?

  

Msg 8156,Level 16,State 1,Line 17
  “QISproduct”已多次指定“id”列。

查询:

SELECT 
    g.artcode
    , sum(g.aantal)
    , i.class_01
    , i.Isstockitem
FROM 
    gbkmut AS g
INNER JOIN 
    items AS i ON i.itemcode = g.artcode
INNER JOIN 
    (SELECT 
         QISP.id
         , QISprocess.nml
         , QISeventlog.id
         , QISeventlog.dtsample
         , QISproduct.nms
         , QISbatchlog.nm
         , QIStestlog.idvariable
         , QIStestlog.no
         , QISshortnote.ds
         , gewicht = CASE QIStestlog.IDvariable
                        WHEN '139'
                           THEN QIStestlog.no
                     END
         , aantal = CASE QIStestlog.IDvariable
                       WHEN '234'
                          THEN QIStestlog.no
                    END
         , siloleeg = CASE QIStestlog.idvariable
                         WHEN '23'
                            THEN CASE QIStestlog.no
                                    WHEN '10'
                                       THEN 'Ja'
                                       ELSE 'Nee'
                                 END
                      END
         , QISvariable.nml
         , gl.nm
         , QISprocess.id
     FROM 
         QIC.Vobra_new2.dbo.production AS QISP
     INNER JOIN 
         QIC.Vobra_new2.dbo.process AS QISprocess ON QISP.idprocess = QISProcess.id
     INNER JOIN 
         QIC.Vobra_new2.dbo.product AS QISproduct ON QISP.idproduct = QISproduct.id
     INNER JOIN 
         QIC.Vobra_new2.dbo.batchlog AS QISbatchlog ON QISP.idbatch = QISbatchlog.id
     INNER JOIN 
         QIC.Vobra_new2.dbo.eventlog AS QISeventlog ON QISeventlog.idproduction = QISP.id
                                                    AND QISeventlog.idbatch = QISbatchlog.id
     INNER JOIN 
         QIC.Vobra_new2.dbo.testlog AS QIStestlog ON QIStestlog.idevent = QISeventlog.id
     LEFT OUTER JOIN 
         QIC.Vobra_new2.dbo.shortnote AS QISshortnote ON QISshortnote.id = QIStestlog.no
                                                      AND QIStestlog.idvariable = '144'
     INNER JOIN 
         QIC.Vobra_new2.dbo.variable AS QISvariable ON QISvariable.id = QIStestlog.idvariable
     LEFT OUTER JOIN 
        QIC.Vobra_new2.dbo.vvarxproc AS vvp ON vvp.idvariable = QISvariable.id
                                            AND vvp.idprocess = QISP.idprocess
     LEFT OUTER JOIN 
         QIC.Vobra_new2.dbo.attribute AS QISattribute ON QISattribute.id = vvp.idattribute
     LEFT OUTER JOIN 
         QIC.Vobra_new2.dbo.grade AS QISgrade ON QISgrade.id = QISattribute.idgrade
     LEFT OUTER JOIN 
         QIC.Vobra_new2.dbo.gradelevel AS gl ON gl.idgrade = QISattribute.idgrade
                                             AND gl.nlevel = QIStestlog.no
     WHERE 
         QISbatchlog.nm NOT LIKE 'V%'
         AND QISP.dtstart > '2017-01-01'
         AND QISP.dtstart < '2017-01-19'
         AND QISP.idprocess IN ('12', '13', '14', '15', '16', '17', '18', '41')
         AND QIStestlog.idvariable IN ('234', '139', '128')
    ) QISproduct ON g.artcode = QISproduct.nms
WHERE 
    g.bkjrcode > '2015'
    AND g.reknr IN (3000, 3010, 3020)
    AND g.aantal > 0
    AND g.warehouse IN ('1', '9')
    AND g.datum >= '2017-01-01'
    AND g.oorsprong = 'R'
    AND g.kstplcode <> 'VPR'
GROUP BY 
    g.artcode, i.Class_01, i.IsStockItem

3 个答案:

答案 0 :(得分:0)

别名为QISProduct的计算查询包含来自两个表的id列,即QISEventLog和QISProcess。因此,将这些列重命名为不同的名称。更新了查询

        SELECT g.artcode
            ,sum(g.aantal)
            ,i.class_01
            ,i.Isstockitem
        FROM gbkmut AS g
        INNER JOIN items AS i ON i.itemcode = g.artcode
        INNER JOIN (
            SELECT QISP.id
                ,QISprocess.nml AS Processnml
                ,QISeventlog.id AS EventLogId
                ,QISeventlog.dtsample
                ,QISproduct.nms
                ,QISbatchlog.nm AS batchnm
                ,QIStestlog.idvariable
                ,QIStestlog.no
                ,QISshortnote.ds
                ,gewicht = CASE QIStestlog.IDvariable
                    WHEN '139'
                        THEN QIStestlog.no
                    END
                ,aantal = CASE QIStestlog.IDvariable
                    WHEN '234'
                        THEN QIStestlog.no
                    END
                ,siloleeg = CASE QIStestlog.idvariable
                    WHEN '23'
                        THEN CASE QIStestlog.no
                                WHEN '10'
                                    THEN 'Ja'
                                ELSE 'Nee'
                                END
                    END
                ,QISvariable.nml variablenml
                ,gl.nm AS glnm
                ,QISprocess.id AS ProcessId
            FROM QIC.Vobra_new2.dbo.production AS QISP
            INNER JOIN QIC.Vobra_new2.dbo.process AS QISprocess ON QISP.idprocess = QISProcess.id
            INNER JOIN QIC.Vobra_new2.dbo.product AS QISproduct ON QISP.idproduct = QISproduct.id
            INNER JOIN QIC.Vobra_new2.dbo.batchlog AS QISbatchlog ON QISP.idbatch = QISbatchlog.id
            INNER JOIN QIC.Vobra_new2.dbo.eventlog AS QISeventlog ON QISeventlog.idproduction = QISP.id
                AND QISeventlog.idbatch = QISbatchlog.id
            INNER JOIN QIC.Vobra_new2.dbo.testlog AS QIStestlog ON QIStestlog.idevent = QISeventlog.id
            LEFT JOIN QIC.Vobra_new2.dbo.shortnote AS QISshortnote ON QISshortnote.id = QIStestlog.no
                AND QIStestlog.idvariable = '144'
            INNER JOIN QIC.Vobra_new2.dbo.variable AS QISvariable ON QISvariable.id = QIStestlog.idvariable
            LEFT JOIN QIC.Vobra_new2.dbo.vvarxproc AS vvp ON vvp.idvariable = QISvariable.id
                AND vvp.idprocess = QISP.idprocess
            LEFT JOIN QIC.Vobra_new2.dbo.attribute AS QISattribute ON QISattribute.id = vvp.idattribute
            LEFT JOIN QIC.Vobra_new2.dbo.grade AS QISgrade ON QISgrade.id = QISattribute.idgrade
            LEFT JOIN QIC.Vobra_new2.dbo.gradelevel AS gl ON gl.idgrade = QISattribute.idgrade
                AND gl.nlevel = QIStestlog.no
            WHERE QISbatchlog.nm NOT LIKE 'V%'
                AND QISP.dtstart > '2017-01-01'
                AND QISP.dtstart < '2017-01-19'
                AND QISP.idprocess IN (
                    '12'
                    ,'13'
                    ,'14'
                    ,'15'
                    ,'16'
                    ,'17'
                    ,'18'
                    ,'41'
                    )
                AND QIStestlog.idvariable IN (
                    '234'
                    ,'139'
                    ,'128'
                    )
            ) QISproduct ON g.artcode = QISproduct.nms
        WHERE g.bkjrcode > '2015'
            AND g.reknr IN (
                3000
                ,3010
                ,3020
                )
            AND g.aantal > 0
            AND g.warehouse IN (
                '1'
                ,'9'
                )
            AND g.datum >= '2017-01-01'
            AND g.oorsprong = 'R'
            AND g.kstplcode <> 'VPR'
        GROUP BY g.artcode
            ,i.Class_01
            ,i.IsStockItem

答案 1 :(得分:0)

,QISbatchlog.nm 和  ,gl.nm 具有相同的列名称 您可以添加以更改列名称  ,gl.nm as col1

答案 2 :(得分:0)

您的查询中存在多个问题。您正在内部查询中填充具有相同列名的以下列。在内部查询中使用AS别名使用唯一名称。

rnorm(N_seq[2], 3.2, 1)

必须在sql中通过选择列表返回唯一列名。