简体   繁体   English

SQL Server 2008:如何对计算的别名列使用子查询/ CTE

[英]SQL Server 2008 : how to use Subquery/CTE for calculated alias columns

I have a query I'm trying to build that that basically takes the punch out time for a job and using DATEADD and the TotalTime column to calculate the punch in time. 我正在尝试建立一个查询,该查询基本上占用了工作的打卡时间,并使用DATEADDTotalTime列来计算打卡时间。

What I'm trying to do is use the ClockOutTime alias and the TotalTime alias. 我正在尝试使用ClockOutTime别名和TotalTime别名。 Being that they are aliases I'm not able to use them in a further calculation which leads me to believe that I may need to reformat it into a subquery or cte, however I'm not a SQL expert and have been unsuccessful trying to get this to work. 由于它们是别名,因此我无法在进一步的计算中使用它们,这使我相信我可能需要将其重新格式化为子查询或cte,但是我不是SQL专家,并且尝试获取失败这个工作。

If anyone can help point me in the right direction I would be very grateful. 如果有人能帮助我指出正确的方向,我将不胜感激。

Thanks! 谢谢!

SELECT DISTINCT
    wol.Work_Order_KEY AS WorkOrderKey,
    Contact_Name AS Employee,
    Labor_Date,
    CONVERT(VARCHAR(5), Labor_Date, 108) AS ClockOutTime,
    REPLACE(CAST(CONVERT(DECIMAL(10, 2), CAST(Hours AS INT) + ((Hours-
    CAST(Hours AS INT)) * .60)) AS VARCHAR), '.', ':') AS TotalTime,
    wol.Asset_ID AS AssetID, 
    al.Group_ID AS GroupID
FROM 
    WorkOrderContacts woc,
    WorkOrderLaborList woll 
JOIN
    WorkOrderList wol ON wol.Work_Order_KEY = woll.Work_Order_KEY 
JOIN
    AssetList al ON wol.Asset_ID = al.Asset_ID

you'll need to create an outer query and use the alias there. 您将需要创建一个外部查询并在其中使用别名。

select * from (select 
    distinct wol.Work_Order_KEY as WorkOrderKey
    ,Contact_Name as Employee
    ,Labor_Date
    ,CONVERT(VARCHAR(5),Labor_Date,108) as ClockOutTime
    ,replace(cast(convert(decimal(10,2),cast(Hours as int)+((Hours-
    cast(Hours as 
    int))*.60)) as varchar),'.',':') as TotalTime
    ,wol.Asset_ID as AssetID
    ,al.Group_ID as GroupID

FROM 
    WorkOrderContacts woc,
    WorkOrderLaborList woll join 
    WorkOrderList wol on wol.Work_Order_KEY=woll.Work_Order_KEY join 
    AssetList al on wol.Asset_ID=al.Asset_ID) as temp 
    where TotalTime = 'your value'

Your calculation for TotalTime looks a little suspect to me (see my comment in the sql below). 您对TotalTime的计算对我来说有点怀疑(请参阅下面的sql中的注释)。 And the cross join might be appropriate but often a cross join is not wanted. 交叉连接可能是适当的,但是通常不需要交叉连接。 I attempted to do some formatting to help here. 我试图做一些格式化来帮助这里。 I also changed everything to use convert. 我还更改了所有内容以使用convert。 Doesn't really matter if you use one or the other but I prefer for a single query to be consistent. 如果您使用一个或另一个,这并不重要,但我希望单个查询保持一致。 Also, not a huge fan of a column named Hours since that is a reserved word in t-sql but it is still manageable. 而且,不是小时名称列的忠实拥护者,因为这是t-sql中的保留字,但仍可管理。 I used a cte in my code but you don't have to do that. 我在代码中使用了cte,但是您不必这样做。 You could do a derived table like the other answer here or you could repeat the entire calculation (but that is kind of ugly). 您可以像此处的其他答案一样创建派生表,也可以重复整个计算(但这有点丑陋)。

with MyCTE as
(
    select 
        distinct wol.Work_Order_KEY as WorkOrderKey
        , Contact_Name as Employee
        , Labor_Date
        , CONVERT(VARCHAR(5),Labor_Date,108) as ClockOutTime
        , replace(convert(varchar(10), convert(decimal(10,2), convert(int, Hours) + ((Hours - convert(int, Hours)) * .60))), '.', ':') as TotalTime --should this be dividing by 60 instead of multiplying?
        , wol.Asset_ID as AssetID
        , al.Group_ID as GroupID
    FROM WorkOrderContacts woc
    cross join WorkOrderLaborList woll --do you really want a cross join here or is there an appropriate inner/outer join to be made?
    join WorkOrderList wol on wol.Work_Order_KEY = woll.Work_Order_KEY 
    join AssetList al on wol.Asset_ID = al.Asset_ID
)
select *
from MyCTE
where TotalTime = 'ValueYouAreSearchingFor'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM