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.
What I'm trying to do is use the ClockOutTime
alias and the TotalTime
alias. 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.
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). 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. 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. I used a cte in my code but you don't have to do that. 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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.