【问题标题】:How do I link the subquery to the main query?如何将子查询链接到主查询?
【发布时间】:2023-04-07 08:16:01
【问题描述】:

我无法让blast_seconds 显示为正确的值。在我的 select 子查询中,我需要让 blast_seconds 在我的主 Select 语句中同时显示。执行查询时,YearMonthScheduledSecondsTotalDaysTotaltrucks 匹配,但 blast_seconds 列在所有月份中显示相同的值。

这是我的查询:

SELECT      DATEPART(YEAR, [Time])      AS [Year]
        ,   DATEPART(MONTH, [Time])     AS [Month]
        ,   SUM(Total)                  AS ScheduledSeconds
        ,   COUNT(DISTINCT([Time]))     AS TotalDays
        ,   COUNT(DISTINCT(Equipment))  AS TotalTrucks
        ,   (
                SELECT      SUM(CASE 
                                    WHEN dbo.reasons.status_id = '298' 
                                        THEN (dbo.by_operator_reasons.seconds) 
                                    ELSE 0 
                                END)        
                FROM        dbo.reasons     
                INNER JOIN  by_operator_reasons 
                ON          dbo.reasons.id = by_operator_reasons.reason_id
                INNER JOIN  equipment
                ON          by_operator_reasons.equipment_id = equipment.id     
                WHERE       reasons.descrip LIKE 'Blast' 
                AND         [Time] BETWEEN '2011-01-01' AND '2012-05-15'
                AND         by_operator_reasons.deleted_at IS NULL
                AND         equipment.type = 'Truck'
            ) AS Blast_Seconds              
FROM        by_equipment_times
WHERE       [Time] BETWEEN '2011-01-01' and '2012-05-15' 
AND         equipment_type  = 'Truck'
GROUP BY    DATEPART(YEAR, [Time])
        ,   DATEPART(MONTH, [Time])
ORDER BY    DATEPART(YEAR, [Time])      ASC
        ,   DATEPART(MONTH, [Time])     ASC

这是我当前的输出:

Year  Month  SchedSec  Days  TotalTrucks  Blast_Seconds
----  -----  --------  ----  -----------  -------------
2011    1    51340448   31        20      4931156
2011    2    51979509   28        22      4931156
2011    3    58845600   31        22      4931156
2011    4    59121967   30        24      4931156
2011    5    66857271   31        25      4931156
2011    6    67306766   30        28      4931156
2011    7    76976358   31        30      4931156
2011    8    80393145   31        30      4931156
2011    9    75556005   30        30      4931156
2011    10   77741205   31        29      4931156
2011    11   75272400   30        29      4931156
2011    12   77691044   31        29      4931156
2012    1    77683752   31        29      4931156
2012    2    72662400   29        29      4931156
2012    3    77574538   31        29      4931156
2012    4    75172177   30        29      4931156
2012    5    37584000   15        29      4931156

【问题讨论】:

    标签: sql sql-server-2008-r2


    【解决方案1】:

    子查询与主查询不相关,即不依赖于它。您需要连接它,我相信您想使用 Time 列来连接它。

    SELECT      DATEPART(YEAR, [Time])      AS [Year]
            ,   DATEPART(MONTH, [Time])     AS [Month]
            ,   SUM(Total)                  AS ScheduledSeconds
            ,   COUNT(DISTINCT([Time]))     AS TotalDays
            ,   COUNT(DISTINCT(Equipment))  AS TotalTrucks
            ,   (
                    SELECT      SUM(CASE 
                                        WHEN dbo.reasons.status_id = '298' 
                                            THEN (dbo.by_operator_reasons.seconds) 
                                        ELSE 0 
                                    END)        
                    FROM        dbo.reasons     
                    INNER JOIN  by_operator_reasons 
                    ON          dbo.reasons.id = by_operator_reasons.reason_id
                    INNER JOIN  equipment
                    ON          by_operator_reasons.equipment_id = equipment.id     
                    WHERE       reasons.descrip LIKE 'Blast' 
                    AND         DATEPART(YEAR, [Time]) = DATEPART(YEAR, by_equipment_times.[Time])
                    AND         DATEPART(MONTH, [Time]) = DATEPART(MONTH, by_equipment_times.[Time])
                    AND         by_operator_reasons.deleted_at IS NULL
                    AND         equipment.type = 'Truck'
                ) AS Blast_Seconds              
    FROM        by_equipment_times
    WHERE       [Time] BETWEEN '2011-01-01' and '2012-05-15' 
    AND         equipment_type  = 'Truck'
    GROUP BY    DATEPART(YEAR, [Time])
            ,   DATEPART(MONTH, [Time])
    ORDER BY    DATEPART(YEAR, [Time])      ASC
            ,   DATEPART(MONTH, [Time])     ASC
    

    如果您经常花费几个月以上的时间,那么将子查询转换为派生表、按年/月分组并外连接到主查询可能会有所回报。

    【讨论】:

      【解决方案2】:

      这很可能是因为你没有告诉

      Select SUM(CASE WHEN dbo.reasons.status_id = '298' 
          then (dbo.by_operator_reasons.seconds) ELSE 0 END.... 
      

      子查询以获取相应行的值 - 它只是将找到的所有内容相加。 您必须将子查询绑定到主查询 - 类似于

      [...]
          AND equipment.equipemnt_id_or_something = T1.equipment_id_or_something
          ) AS Blast_Seconds              
      FROM        by_equipment_times as T1
      [...]
      

      或者我认为.... :)

      PS。 字段名称是虚构的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-14
        • 2013-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-11
        相关资源
        最近更新 更多