SQL多个子查询,不需要GROUP BY

时间:2014-07-22 20:57:10

标签: sql sql-server sql-server-2008

我基本上有一个相当复杂的查询,用于弄清楚我们在工作中运行的数据库的一些事情。

目标是显示我们使用的某些部件是否存在缺货并显示这些部件。 它检查过去13周内的使用情况,并预测未来3周内是否存在短缺。

现在我遇到的问题是我们有一个叫做part-id的东西作为每个部分的唯一标识符,但不同的part-id可以引用相同的部分。因此,我们必须检查零件描述字段,并通过group by语句匹配它们。一旦我这样做,虽然我有问题,我在多个part-id上有相同的部分,我只计算第一个,因为我使用GROUP BY语句。

在分组之前,有没有办法将所有具有匹配描述的部分“加在一起”?

以下是查询:

SELECT
    st.[Commodity] AS 'Commodity'
    ,st.[Description] AS 'Description'
    ,st.[Good] AS 'Good'
    ,st.[3 week demand] AS '3 week demand'
    ,CASE
        WHEN ROUND(CONVERT(FLOAT,st.[Shortage]),0) BETWEEN 0 AND 1
        THEN '-1' 
        ELSE ROUND(CONVERT(FLOAT,st.[Shortage]),0) END AS 'Shortage'
FROM
    (
    SELECT
        tt.[Commodity]
        ,tt.[Description]
        ,SUM(tt.[Good]) AS 'Good'
        ,ROUND(CONVERT(FLOAT,SUM((tt.[Usage last 13 weeks] / 13) * 3)),1) AS '3 week demand'
        ,CASE
            WHEN SUM(tt.[Usage last 13 weeks]) > 0
            THEN SUM(
                    (tt.[Good] - (tt.[Usage last 13 weeks] / 13) * 3)
            )
            ELSE ISNULL(SUM(
                (
                    (tt.[Good] - (tt.[Usage last 13 weeks] / 13) * 3)
                )
            ),0)
            END AS 'Shortage'
    FROM
        (
            SELECT
            pa.id AS 'ID'
            ,pa.category AS 'Commodity'
            ,pa.[description] AS 'Description'
            ,'Usage last 13 weeks' =
                (
                SELECT 
                    ROUND(CONVERT(FLOAT,COUNT(*)),1) 
                FROM 
                    tbl_component AS co
                WHERE 
                    co.date_out > (DATEADD(WEEK,-13,GETDATE()))
                    AND workorder_out_id IS NOT NULL
                    AND co.id IN (SELECT component_id FROM tbl_repairlines WHERE component_id IS NOT NULL)
                    AND co.customer_id IS NULL
                    AND co.part_id = pa.id
                )
            ,'Good' = 
                (
                SELECT 
                    ROUND(CONVERT(FLOAT,COUNT(*)),1) 
                FROM 
                    tbl_component AS co2
                    ,tbl_loc_com_latest AS ll 
                WHERE date_out IS NULL 
                    AND co2.id = ll.component_id
                    AND co2.location_id = 26
                    AND UPPER(ll.system_comment) NOT LIKE 'BJ%'
                    AND co2.part_id = pa.id 
                    AND co2.[status] = 'GOOD'   
                )
            FROM 
                tbl_component AS co
                ,tbl_parts AS pa
            WHERE 
                pa.id = co.part_id
                AND ((co.id IN (SELECT component_id FROM tbl_repairlines WHERE component_id IS NOT NULL)) OR (pa.category = 'TAPE'))
                AND ((workorder_out_id IS NOT NULL) OR (pa.category = 'TAPE'))
                AND customer_id IS NULL
                AND pa.category IN ('HEAD')
            GROUP BY
                pa.id
                ,pa.category
                ,pa.[description]
            ) AS tt
        GROUP BY 
            tt.[Description]
            ,tt.[Commodity]
        ) AS st
GROUP BY
    st.[Commodity]
    ,st.[Description]
    ,st.[Good]
    ,st.[3 week demand]
    ,st.[Shortage]
HAVING
    st.[Shortage] < 0
ORDER BY
    'Shortage' ASC

0 个答案:

没有答案