查询以获取值和属性总和

时间:2015-10-01 12:56:53

标签: sql-server join

我试图写一个查询,但是找到一个重量和体积总和的错误: SQL错误[8120]:列' Attribute.attribute_value'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。   com.microsoft.sqlserver.jdbc.SQLServerException:Column' Attribute.attribute_value'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

  SELECT
    dg.delivery_group_id, 
     ctr.container_type, 
    ven.attribute_value AS 'vendor',
    carr.attribute_value AS 'carrier',
    com.attribute_value AS 'commodity',
    conv.attribute_value AS 'conveyance',
    stor.attribute_value AS 'store',
    SUM(ISNULL(CAST(wgt.attribute_value AS float), 0.0)) AS 'weight',
    SUM(ISNULL(CAST(vol.attribute_value AS float), 0.0)) AS 'volume'

    FROM (SELECT
    pal.container_id AS 'container_id',
    pal.container_type AS container_type,
    cas.container_id AS 'sub_container_id',
    pal.loc_id
    FROM container pal
    LEFT OUTER JOIN container cas
    ON cas.parent_container_id = pal.container_id
    AND cas.container_type = 'Case'
    WHERE pal.container_type = 'Pallet') AS ctr
    INNER JOIN Container_Detail cdtl
    ON (ctr.container_id = cdtl.container_id
    OR ctr.sub_container_id = cdtl.container_id)
   INNER JOIN container_delivery cd
   ON cdtl.detail_id = cd.detail_id 
   INNER JOIN delivery_line dl
   ON cd.delivery_line_id = dl.delivery_line_id
   INNER JOIN delivery_group dg
   ON dl.delivery_group_id = dg.delivery_group_id
   INNER JOIN Attribute ven 
   ON cdtl.attribute_id=ven.attribute_id AND ven.attribute_type='vendor'
   INNER JOIN Attribute carr 
    ON cdtl.attribute_id=carr.attribute_id AND carr.attribute_type='carrier'
   INNER JOIN Attribute com 
    ON cdtl.attribute_id=com.attribute_id AND com.attribute_type='commodity'
   INNER JOIN Attribute conv 
    ON cdtl.attribute_id=conv.attribute_id AND     
    conv.attribute_type='conveyance'   
    INNER JOIN Attribute stor 
    ON cdtl.attribute_id=stor.attribute_id AND stor.attribute_type='store'
    INNER JOIN Attribute wgt 
    ON cdtl.attribute_id=wgt.attribute_id AND wgt.attribute_type='weight'
    INNER JOIN Attribute vol 
    ON cdtl.attribute_id=vol.attribute_id AND vol.attribute_type='volume'
   WHERE ctr.container_id = '00007712120038613557'
    OR ctr.sub_container_id = '00007712120038613557'
     GROUP BY dg.delivery_group_id,    
        ctr.container_type,ctr.container_id,ctr.sub_container_id,    
        wgt.attribute_value, vol.attribute_value

以下是我的表格: enter image description here

2 个答案:

答案 0 :(得分:1)

通过以下方式将所有这些添加到您的论坛:

group by     dg.delivery_group_id, 
     ctr.container_type, 
    ven.attribute_value AS,
    carr.attribute_value ,
    com.attribute_value AS ,
    conv.attribute_value ,
    stor.attribute_value

答案 1 :(得分:1)

SELECT
    dg.delivery_group_id, 
     ctr.container_type, 
    ven.attribute_value AS 'vendor',
    carr.attribute_value AS 'carrier',
    com.attribute_value AS 'commodity',
    conv.attribute_value AS 'conveyance',
    stor.attribute_value AS 'store',
    SUM(ISNULL(CAST(wgt.attribute_value AS float), 0.0)) AS 'weight',
    SUM(ISNULL(CAST(vol.attribute_value AS float), 0.0)) AS 'volume'

    FROM (SELECT   pal.container_id AS 'container_id',  

      pal.container_type AS container_type,    cas.container_id AS 'sub_container_id',     pal.loc_id    FROM container pal    LEFT OUTER JOIN container cas    ON cas.parent_container_id = pal.container_id
        AND cas.container_type = 'Case'      WHERE pal.container_type = 'Pallet') AS ctr     INNER JOIN Container_Detail cdtl     ON (ctr.container_id = cdtl.container_id     OR ctr.sub_container_id = cdtl.container_id)    INNER JOIN container_delivery cd
       ON cdtl.detail_id = cd.detail_id     INNER JOIN delivery_line dl    ON cd.delivery_line_id = dl.delivery_line_id    INNER JOIN delivery_group dg    ON dl.delivery_group_id = dg.delivery_group_id    INNER JOIN Attribute ven 
       ON cdtl.attribute_id=ven.attribute_id AND ven.attribute_type='vendor'
       INNER JOIN Attribute carr 
        ON cdtl.attribute_id=carr.attribute_id AND carr.attribute_type='carrier'
       INNER JOIN Attribute com 
        ON cdtl.attribute_id=com.attribute_id AND com.attribute_type='commodity'
       INNER JOIN Attribute conv 
        ON cdtl.attribute_id=conv.attribute_id AND     
        conv.attribute_type='conveyance'   
        INNER JOIN Attribute stor 
        ON cdtl.attribute_id=stor.attribute_id AND stor.attribute_type='store'
        INNER JOIN Attribute wgt 
        ON cdtl.attribute_id=wgt.attribute_id AND wgt.attribute_type='weight'
        INNER JOIN Attribute vol 
        ON cdtl.attribute_id=vol.attribute_id AND vol.attribute_type='volume'
       WHERE ctr.container_id = '00007712120038613557'
        OR ctr.sub_container_id = '00007712120038613557'
         group by    
         dg.delivery_group_id, 
         ctr.container_type, 
        ven.attribute_value AS,
        carr.attribute_value ,
        com.attribute_value AS ,
        conv.attribute_value ,
        stor.attribute_value