need your expertise on the "Conversion failed when converting date and/or time from character string". Much written about it and tried looking though it and understanding it to the extend I could but it did not solve the issue.
My code includes a number on conversations from dates and strings to the common numerical format. The conversation work separately form the main code and even together when the code is loose. However, when I start adding more conditions in the "where" function, SQL doesn't seem to like it. Do you possible know how to handle it best? Any help is much appreciated.
SELECT DISTINCT
DOTWHL as "DC",
DORELD as "Release date",
DODLDT as "Prelim ETA",
DOITNO as "Item #",
MMITDS as "Item name",
MMSTCN as "Lkr",
DOPPQT as "Plan Qty",
DOPPQT/MMCFI2 as "Plan Qty, pall",
DOPSTS as "Sts",
SUM(MHFOQT) as "FC between ETA & BBD",
MLSTQT as "Stock, LHU",
MMCFI2 as "LHU/PAL",
MMCFI3 as "LHU/Layer",
CASE WHEN DOPPQT >= MLSTQT
THEN CASE
WHEN MLSTQT > SUM(MHFOQT)
THEN 'YES'
ELSE 'NO'
END
ELSE CASE
WHEN DOPPQT > SUM(MHFOQT)
THEN 'YES'
ELSE 'NO'
END
END AS "FEFO violation necessary?",
CASE WHEN DOPPQT >= MLSTQT
THEN CASE
WHEN MLSTQT > SUM(MHFOQT)
THEN SUM(MHFOQT)
ELSE MLSTQT
END
ELSE CASE
WHEN DOPPQT > SUM(MHFOQT)
THEN SUM(MHFOQT)
ELSE DOPPQT
END
END AS "Recommended Qty, LHU",
CASE WHEN DOPPQT >= MLSTQT
THEN CASE
WHEN MLSTQT > SUM(MHFOQT)
THEN SUM(MHFOQT)/MMCFI2
ELSE MLSTQT/MMCFI2
END
ELSE CASE
WHEN DOPPQT > SUM(MHFOQT)
THEN SUM(MHFOQT)/MMCFI2
ELSE DOPPQT/MMCFI2
END
END AS "Recommended Qty, pall",
MLBREF as "Batch",
CASE WHEN MLSTQT > DOPPQT
THEN CASE
WHEN DOPPQT < SUM(MHFOQT)
THEN SUM(MHFOQT)-DOPPQT
ELSE NULL
END
ELSE NULL
END AS "Fill Up possibility, LHU",
Sales.Sold_Qty as "Sold Qty 10 weeks",
Sales.Forecasted_Qty as "FC Qty 10 weeks",
ISNULL(Sales.Sold_Qty/NULLIF(Sales.Forecasted_Qty,0), 0) as "Sold/FC, %",
MBBUYE as "NP",
MMRESP as "DmP",
CASE
WHEN Other_Assortments.item <> 0 THEN 'Many Markets'
ELSE 'DC 091 only'
END as "Distributed on"
FROM MVXJDTA.MDOPLP
LEFT JOIN MVXJDTA.MITMAS
ON DOCONO=MMCONO AND DOITNO=MMITNO
LEFT JOIN MVXJDTA.MITBAL
ON DOCONO=MBCONO AND DOFWHL=MBWHLO AND DOITNO=MBITNO
LEFT JOIN MVXJDTA.MITSTA
ON DOCONO=MHCONO AND DOTWHL=MHWHLO AND DOITNO=MHITNO
LEFT JOIN MVXJDTA.MITLOC
ON DOCONO=MLCONO AND DOFWHL=MLWHLO AND DOITNO=MLITNO
LEFT JOIN
(
SELECT DISTINCT
MHITNO as "Item_#",
sum(MHSOQT) as "Sold_Qty",
sum(MHFOQT) as "Forecasted_Qty"
FROM MVXJDTA.MITSTA
WHERE MHCONO=1
AND MHWHLO='091'
AND MHCYP6 between
(cast(datepart(yyyy, dateadd(ww,-10,getdate())) as varchar) + right('00' + cast(datepart(ww, dateadd(ww,-10,getdate())) as varchar),2)) and
(cast(datepart(yyyy, dateadd(ww,-1,getdate())) as varchar) + right('00' + cast(datepart(ww, dateadd(ww,-1,getdate())) as varchar),2))
AND right(MHCYP6, 2) <>'00'
group by MHITNO
)
as Sales
on DOITNO=Sales.Item_#
LEFT JOIN
(
select distinct
OIITNO as "item"
from MVXJDTA.OCUSMA
left join MVXJDTA.OASCUS
on OKCONO=OCCONO and OKCUNO=OCCUNO
left join MVXJDTA.OASITN
on OKCONO=OICONO and OCASCD=OIASCD
where OKCONO=1
and OITDAT='99999999'
and OKCSCD<>'KR'
and OKCSCD<>'HK'
and OKCSCD<>'TW'
and OKCSCD<>'SG'
and OKCSCD<>'MY'
and OKCSCD<>'TH'
and OKCSCD<>'PH'
group by OIITNO) as Other_Assortments
on Other_Assortments.item=DOITNO
WHERE DOCONO=1
AND DOTWHL='091'
AND DOFWHL='010'
AND MHCYP6 NOT IN (
SELECT
MHCYP6
FROM MVXJDTA.MITSTA
WHERE MHWHLO='091'
AND MHCONO=1
AND RIGHT(CONVERT(VARCHAR, MHCYP6, 102), 2) ='00'
)
AND MHCYP6 BETWEEN
CASE
WHEN DATEPART(WEEK, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103))<10
THEN CONCAT( DATEPART(YEAR, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)), 0,
DATEPART(WEEK, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)))
ELSE CONCAT( DATEPART(YEAR, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)),
DATEPART(WEEK, CONVERT(DATETIME, RIGHT(DODLDT, 2) + '/' + LEFT(RIGHT(DODLDT, 4), 2) + '/' + LEFT (DODLDT, 4), 103)))
END
CASE
WHEN MLBREF IS NULL
THEN '999999'
ELSE CASE
WHEN DATEPART(WEEK, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103)))<10
THEN CONCAT(DATEPART(YEAR, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))), 0,
DATEPART(WEEK, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))))
ELSE CONCAT(DATEPART(YEAR, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))),
DATEPART(WEEK, DATEADD(DAY, -82, CONVERT(DATETIME, RIGHT(MLBREF, 2) + '/' + LEFT(RIGHT(MLBREF, 4), 2) + '/' + '20'+LEFT (MLBREF, 2), 103))))
END
END
AND (MLSTQT-MLALQT)<>0
GROUP BY DOTWHL, DORELD, MLBREF, DODLDT, DOITNO, DOPPQT, MLSTQT, MMITDS, MLALQT, MBPLCD, MMSTCN, MMCFI2, MMCFI3, DOPSTS, Sales.Sold_Qty, Sales.Forecasted_Qty, MBBUYE, MMRESP, Other_Assortments.item
ORDER BY DOTWHL, DORELD
Related
I'm trying to get data per hour however as I execute, the data for day is displayed in the table. IDC_YMD with per hour result must be in the table and I'm not sure if I have the right SUBSTR(string, start, length).
SELECT EQP_ID, EQP_NAME, 'WorkPreparation' GUBUN,WorkPreparation
CNT,SITE_CODE,GODS_CODE,STEP_NAME,IDC_YMD,IDC_SUMR_UNIT_CODE,MTBI,MTBA
FROM
(
SELECT CASE WHEN WorkPreparation = 0 THEN '-' ELSE EQP_ID END AS EQP_ID, CASE WHEN WorkPreparation =
0 THEN '-' ELSE SUBSTR(EQP_NAME,1,4) END AS EQP_NAME,
WorkPreparation,SITE_CODE,GODS_CODE,STEP_NAME,IDC_YMD,IDC_SUMR_UNIT_CODE,MTBI,MTBA
FROM(
SELECT EQP_ID, EVENT_COMMENT EQP_NAME, SUM(WORK_PRE_CNT) WorkPreparation, A.SITE_CODE
, A.GODS_CODE
, A.EES_STEP_ID AS STEP_NAME
, TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12, 2))) || '/' || TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12,
2))) AS IDC_YMD
, 'D' AS IDC_SUMR_UNIT_CODE
, CASE WHEN (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0))) = 0
THEN ROUND(SUM(NVL(A.OPER_MOP, 0)),1)
ELSE ROUND(SUM(NVL(A.OPER_MOP, 0)) / (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0))) /
60,1)
END AS MTBI
, CASE WHEN (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0)) + SUM(NVL(A.WORK_PRE_CNT, 0)) +
SUM(NVL(A.PROD_CHANGE_CNT, 0)) + SUM(NVL(A.QUAL_EXATN_CNT, 0)) +
SUM(NVL(A.MTR_EXATN_CNT, 0)) + SUM(NVL(A.STEP_CHRC_LOSS_CNT, 0)) +
SUM(NVL(A.IDLE_3MN_UND_EXCPT_CNT, 0))) = 0
THEN 0
ELSE ROUND(SUM(NVL(A.OPER_MOP, 0)) / (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0)) +
SUM(NVL(A.WORK_PRE_CNT, 0)) + SUM(NVL(A.PROD_CHANGE_CNT, 0)) + SUM(NVL(A.QUAL_EXATN_CNT, 0)) +
SUM(NVL(A.MTR_EXATN_CNT, 0)) + SUM(NVL(A.STEP_CHRC_LOSS_CNT,
0)) + SUM(NVL(A.IDLE_3MN_UND_EXCPT_CNT, 0))) / 60,1)
END AS MTBA
FROM IFR_EES_PCL A
WHERE SITE_CODE = 'E502AA'
AND GODS_CODE = 'N5210'
AND MTBI_MNG_YN = 'Y'
AND EES_STEP_ID = 'TP'
AND A.IDC_YMD BETWEEN TO_CHAR(SYSDATE-6, 'YYYYMMDDHH') AND TO_CHAR(SYSDATE, 'YYYYMMDD')
GROUP BY EQP_ID, EVENT_COMMENT,A.SITE_CODE
, A.GODS_CODE
,A.EQP_TYPE
, A.EES_STEP_ID
, TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12, 2)))
, A.IDC_YMD
ORDER BY A.IDC_YMD, MTBI DESC--TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12, 2)))
))
WHERE ROWNUM <= 10
and EQP_NAME like 'TA%'
Thank you!
#BarId AS INTEGER
AS
BEGIN
SET NOCOUNT ON
BEGIN
select a.*, b.remark from
(
SELECT
min(bardetailid) as BarDetailId,
min(barid) as BarId,
min(Member) as Member,
min(MemberNo) as MemberNo,
min(BarMark) as BarMark,
[TypeSize] ,
min(BarLength) as BarLength,
min(BarNo) as BarNo,
min(BarTotal) as BarTotal,
[ShapeCode] ,
CAST(MAX(CAST(ShapeImage AS VARBINARY(MAX))) AS IMAGE) as ShapeImage,
CASE
WHEN c.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
ELSE ''
END
AS Dimension,
min(DimA) AS DimA,
min(DimB) AS DimB,
min(DimC) AS DimC,
min(DimD) AS DimD,
min(DimE) AS DimE,
min(DimF) AS DimF,
min(DimG) AS DimG,
min(DimN) AS DimN,
min(Active) AS Active,
min(c.CreatedDtTm) AS CreatedDtTm,
min(createdby) as CreatedBy,
min(createdip) AS [CreatedIp],
min(UpdatedDtTm) as [UpdatedDtTm],
min(UpdatedBy) AS [UpdatedBy],
min(UpdatedIp) AS [UpdatedIp]
FROM bar_schedule_detail c
WHERE active = 1
AND barid = #BarId
GROUP BY
[TypeSize],
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and
(
CASE
WHEN b.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
ELSE ''
END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1
and b.barid = #BarId
ORDER BY
substring(a.TypeSize,1,1) desc,
BarLength desc
END
END
Crystal report does not display the page when i debug the code as showed above. When i execute the code in my Sql Server, everything is executed correctly. Due to the limitation of my knowledge in programming. Can someone identify the issue that i am facing.
I have solved the issue by adding this code a.BarDetailId = b.BarDetailId and a.shapecode = b.shapecode and it worked.
SELECT [BarDetailId]
,[BarId]
,[Member]
,[MemberNo]
,[BarMark]
,[TypeSize]
,[BarLength]
,[BarNo]
,[BarTotal]
,[ShapeCode]
,[ShapeImage]
,(SELECT CASE WHEN DimA <> 0 THEN 'A=' + CONVERT(VARCHAR, CAST(DimA AS
Decimal(18, 0)))
ELSE '' END + CASE WHEN DimB <> 0 THEN
', B=' + CONVERT(VARCHAR, CAST(DimB AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimC <> 0 THEN
', C=' + CONVERT(VARCHAR, CAST(DimC AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimD <> 0 THEN
', D=' + CONVERT(VARCHAR, CAST(DimD AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimE <> 0 THEN
', E=' + CONVERT(VARCHAR, CAST(DimE AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimF <> 0 THEN
', F=' + CONVERT(VARCHAR, CAST(DimF AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimG <> 0 THEN
', G=' + CONVERT(VARCHAR, CAST(DimG AS Decimal(18, 0)))
ELSE '' END + CASE WHEN DimN <> 0 THEN
', N=' + CONVERT(VARCHAR, CAST(DimN AS Decimal(18, 0)))
ELSE '' END) AS Dimension
,[DimA]
,[DimB]
,[DimC]
,[DimD]
,[DimE]
,[DimF]
,[DimG]
,[DimN]
,[Remark]
,[Active]
,[CreatedDtTm]
,[CreatedBy]
,[CreatedIp]
,[UpdatedDtTm]
,[UpdatedBy]
,[UpdatedIp]
FROM BAR_SCHEDULE_DETAIL WHERE Active = 1 AND BarId = #BarId
How to group them based on TypeSize, Shape Code. I want to include the group by statement but receive this error message.
Column 'BAR_SCHEDULE_DETAIL.BarDetailId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
GROUP BY
Is used during an aggregate function like
sum(), count(), min(), max(), avg()
If you want to group the records together, each column is required to be either part of the
GROUP BY
clause, or be aggregated in some way using one of the functions above. More info can be found here:
http://www.sqlcourse2.com/agg_functions.html
I suspect you might mean you want the results ordered so that the groups of records are together, and you might be looking for the
ORDER BY
clause.
If you would only like all the combinations of the columns you commented on before, the query would look like this:
SELECT DISTINCT ,
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
AS dimension
FROM bar_schedule_detail
WHERE active = 1
AND barid = #BarId
In order to include the "top comment", if that means the first comment posted - then something like this would do it:
select a.*, b.remark from
(
SELECT
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN c.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
ELSE ''
END
AS dimension,
min(c.CreatedDtTm) as CreatedDtTm
FROM bar_schedule_detail c
WHERE active = 1
AND barid = #BarId
GROUP BY
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and
(
CASE
WHEN b.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
ELSE ''
END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1
and b.barid = #BarId
From here it gets increasingly more difficult without a copy of the DDL and some rows to test this answer with, but if you want to order by some arbitrary pieces of attributes, you might try this:
SELECT DISTINCT ,
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
AS dimension
FROM bar_schedule_detail
WHERE active = 1
AND barid = #BarId
In order to include the "top comment", if that means the first comment posted - then something like this would do it:
select a.*, b.remark from
(
SELECT
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN c.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
ELSE ''
END
AS dimension,
min(c.CreatedDtTm) as CreatedDtTm
FROM bar_schedule_detail c
WHERE active = 1
AND barid = #BarId
GROUP BY
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and
(
CASE
WHEN b.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
ELSE ''
END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1
and b.barid = #BarId
ORDER BY
substring(a.TypeSize,1,1) desc,
substring(a.TypeSize,2,len(a.TypeSize)-1)
select a.*, b.remark from
(
SELECT
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN c.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN c.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
ELSE ''
END
AS dimension,
min(c.CreatedDtTm)
FROM bar_schedule_detail c
WHERE active = 1
AND barid = #BarId
GROUP BY
[TypeSize] ,
[ShapeCode] ,
CASE
WHEN dima <> 0 THEN
'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dime <> 0 THEN
', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
ELSE ''
END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and
(
CASE
WHEN b.dima <> 0 THEN
'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimb <> 0 THEN
', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimc <> 0 THEN
', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimd <> 0 THEN
', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dime <> 0 THEN
', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimf <> 0 THEN
', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimg <> 0 THEN
', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
ELSE ''
END +
CASE
WHEN b.dimn <> 0 THEN
', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
ELSE ''
END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1
and b.barid = #BarId
When I execute this code, I get this error :
No column name was specified for column 4 of 'a'.
Does anyone know what the problem? This section is where I encounter the error :
as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
Give alias name for all your sub query columns :
SELECT .....,
.....,
....., MIN(c.CreatedDtTm) AS CreatedDT
In your sub query SELECT clause give alias name for min(c.CreatedDtTm) column
I have to make a manual calculation of the next run date for a job, can you help me?
to get the next run date for a job you can use then sysschedules and sysjobschedules tables
check the next_run_date and next_runtime columns from the table sysjobschedules
next_run_date int Next date on which
the job is scheduled to run. The date
is formatted YYYYMMDD.
next_run_time int Time at which the
job is scheduled to run. The time is
formatted HHMMSS, and uses a 24-hour
clock.
see this script
Select sched.*,jobsched.* FROM msdb.dbo.sysschedules AS sched
inner Join msdb.dbo.sysjobschedules AS jobsched ON sched.schedule_id = jobsched.schedule_id
or you can use the msdb.dbo.sp_help_jobschedule stored procedure, to get the same info.
UPDATE
if you need calculate manually the next_run_date you must check the sysschedules table and see the freq_interval, freq_subday_type, freq_subday_interval, freq_relative_interval, freq_recurrence_factor, active_start_date, active_start_time columns to determine the formula.
check this link to see an example of use.
Check this one:
SELECT
J.NAME JOB,
DATEADD(SS,(H.RUN_TIME)%100,DATEADD(N,(H.RUN_TIME/100)%100,DATEADD(HH,H.RUN_TIME/10000,CONVERT(DATETIME,CONVERT(VARCHAR(8),H.RUN_DATE),112))))
JOB_STARTED,
DATEADD(SS,((H.RUN_DURATION)%10000)%100,DATEADD(N,((H.RUN_DURATION)%10000)/100,DATEADD(HH,(H.RUN_DURATION)/10000,DATEADD(SS,(H.RUN_TIME)%100,DATEADD(N,(H.RUN_TIME/100)%100,DATEADD(HH,H.RUN_TIME/10000,CONVERT(DATETIME,CONVERT(VARCHAR(8),H.RUN_DATE),112)))))))
JOB_COMPLETED,
CONVERT(VARCHAR(2),(H.RUN_DURATION)/10000) + ':' +
CONVERT(VARCHAR(2),((H.RUN_DURATION)%10000)/100)+ ':' +
CONVERT(VARCHAR(2),((H.RUN_DURATION)%10000)%100) RUN_DURATION,
CASE H.RUN_STATUS
WHEN 0 THEN 'FAILED'
WHEN 1 THEN 'SUCCEEDED'
WHEN 2 THEN 'RETRY'
WHEN 3 THEN 'CANCELED'
WHEN 4 THEN 'IN PROGRESS'
END RUN_STATUS
,CASE S.FREQ_TYPE
WHEN 1 THEN 'ONCE'
WHEN 4 THEN ' DAILY'
WHEN 8 THEN ' WEEKLY'
WHEN 16 THEN ' MONTHLY'
WHEN 32 THEN ' MONTHLY RELATIVE'
WHEN 64 THEN ' WHEN SQL SERVER'
ELSE 'N/A' END [FREQ]
,CASE
WHEN S.NEXT_RUN_DATE > 0 THEN DATEADD(N,(NEXT_RUN_TIME%10000)/100,DATEADD(HH,NEXT_RUN_TIME/10000,CONVERT(DATETIME,CONVERT(VARCHAR(8),NEXT_RUN_DATE),112)))
ELSE CONVERT(DATETIME,CONVERT(VARCHAR(8),'19000101'),112) END NEXT_RUN
FROM
MSDB..SYSJOBHISTORY H,
MSDB..SYSJOBS J,
MSDB..SYSJOBSCHEDULES S,
(
SELECT
MAX(INSTANCE_ID) INSTANCE_ID
,JOB_ID
FROM MSDB..SYSJOBHISTORY GROUP BY JOB_ID
) M
WHERE
H.JOB_ID = J.JOB_ID
AND
J.JOB_ID = S.JOB_ID
AND
H.JOB_ID = M.JOB_ID
AND
H.INSTANCE_ID = M.INSTANCE_ID
-- IF you want to check all job for today then uncomments below
-- AND
-- RUN_DATE = (YEAR(GETDATE())*10000) + (MONTH(GETDATE()) * 100) + DAY(GETDATE())
ORDER BY NEXT_RUN
The short answer is that there's no single formula - it varies by value of freq_type.
The site seems to be down at the moment of writing, but there is an article at http://www.sqlmag.com/Article/ArticleID/99593/sql_server_99593.html which covers how to derive this information. Unfortunately, the site doesn't allow Google to cache its content, so it can't be retrieved until the site comes back up
This looks like a decent alternative source for the kind of query you're trying to write.
here is a very nice script where you can get the next run date.
-- http://www.sqlprofessionals.com/blog/sql-scripts/2014/10/06/insight-into-sql-agent-job-schedules/
SELECT [JobName] = [jobs].[name]
,[Enabled] = CASE [jobs].[enabled] WHEN 1 THEN 'Yes' ELSE 'No' END
,[Scheduled] = CASE [schedule].[enabled] WHEN 1 THEN 'Yes' ELSE 'No' END
,[Next_Run_Date] =
CASE [jobschedule].[next_run_date]
WHEN 0 THEN CONVERT(DATETIME, '1900/1/1')
ELSE CONVERT(DATETIME, CONVERT(CHAR(8), [jobschedule].[next_run_date], 112) + ' ' +
STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), [jobschedule].[next_run_time]), 6), 5, 0, ':'), 3, 0, ':'))
END
,[Category] = [categories].[name]
,[Owner] = SUSER_SNAME([jobs].[owner_sid])
,[Description] = [jobs].[description]
,[Occurs] =
CASE [schedule].[freq_type]
WHEN 1 THEN 'Once'
WHEN 4 THEN 'Daily'
WHEN 8 THEN 'Weekly'
WHEN 16 THEN 'Monthly'
WHEN 32 THEN 'Monthly relative'
WHEN 64 THEN 'When SQL Server Agent starts'
WHEN 128 THEN 'Start whenever the CPU(s) become idle'
ELSE ''
END
,[Occurs_detail] =
CASE [schedule].[freq_type]
WHEN 1 THEN 'O'
WHEN 4 THEN 'Every ' + CONVERT(VARCHAR, [schedule].[freq_interval]) + ' day(s)'
WHEN 8 THEN 'Every ' + CONVERT(VARCHAR, [schedule].[freq_recurrence_factor]) + ' weeks(s) on ' +
LEFT(
CASE WHEN [schedule].[freq_interval] & 1 = 1 THEN 'Sunday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 2 = 2 THEN 'Monday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 4 = 4 THEN 'Tuesday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 8 = 8 THEN 'Wednesday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 16 = 16 THEN 'Thursday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 32 = 32 THEN 'Friday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 64 = 64 THEN 'Saturday, ' ELSE '' END ,
LEN(
CASE WHEN [schedule].[freq_interval] & 1 = 1 THEN 'Sunday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 2 = 2 THEN 'Monday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 4 = 4 THEN 'Tuesday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 8 = 8 THEN 'Wednesday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 16 = 16 THEN 'Thursday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 32 = 32 THEN 'Friday, ' ELSE '' END +
CASE WHEN [schedule].[freq_interval] & 64 = 64 THEN 'Saturday, ' ELSE '' END
) - 1
)
WHEN 16 THEN 'Day ' + CONVERT(VARCHAR, [schedule].[freq_interval]) + ' of every ' + CONVERT(VARCHAR, [schedule].[freq_recurrence_factor]) + ' month(s)'
WHEN 32 THEN 'The ' +
CASE [schedule].[freq_relative_interval]
WHEN 1 THEN 'First'
WHEN 2 THEN 'Second'
WHEN 4 THEN 'Third'
WHEN 8 THEN 'Fourth'
WHEN 16 THEN 'Last'
END +
CASE [schedule].[freq_interval]
WHEN 1 THEN ' Sunday'
WHEN 2 THEN ' Monday'
WHEN 3 THEN ' Tuesday'
WHEN 4 THEN ' Wednesday'
WHEN 5 THEN ' Thursday'
WHEN 6 THEN ' Friday'
WHEN 7 THEN ' Saturday'
WHEN 8 THEN ' Day'
WHEN 9 THEN ' Weekday'
WHEN 10 THEN ' Weekend Day'
END + ' of every ' + CONVERT(VARCHAR, [schedule].[freq_recurrence_factor]) + ' month(s)'
ELSE ''
END
,[Frequency] =
CASE [schedule].[freq_subday_type]
WHEN 1 THEN 'Occurs once at ' +
STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), [schedule].[active_start_time]), 6), 5, 0, ':'), 3, 0, ':')
WHEN 2 THEN 'Occurs every ' +
CONVERT(VARCHAR, [schedule].[freq_subday_interval]) + ' Seconds(s) between ' +
STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), [schedule].[active_start_time]), 6), 5, 0, ':'), 3, 0, ':') + ' and ' +
STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), [schedule].[active_end_time]), 6), 5, 0, ':'), 3, 0, ':')
WHEN 4 THEN 'Occurs every ' +
CONVERT(VARCHAR, [schedule].[freq_subday_interval]) + ' Minute(s) between ' +
STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), [schedule].[active_start_time]), 6), 5, 0, ':'), 3, 0, ':') + ' and ' +
STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), [schedule].[active_end_time]), 6), 5, 0, ':'), 3, 0, ':')
WHEN 8 THEN 'Occurs every ' +
CONVERT(VARCHAR, [schedule].[freq_subday_interval]) + ' Hour(s) between ' +
STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), [schedule].[active_start_time]), 6), 5, 0, ':'), 3, 0, ':') + ' and ' +
STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), [schedule].[active_end_time]), 6), 5, 0, ':'), 3, 0, ':')
ELSE ''
END
,[AvgDurationInSec] = CONVERT(DECIMAL(10, 2), [jobhistory].[AvgDuration])
FROM [msdb].[dbo].[sysjobs] AS [jobs] WITh(NOLOCK)
LEFT OUTER JOIN [msdb].[dbo].[sysjobschedules] AS [jobschedule] WITh(NOLOCK)
ON [jobs].[job_id] = [jobschedule].[job_id]
LEFT OUTER JOIN [msdb].[dbo].[sysschedules] AS [schedule] WITh(NOLOCK)
ON [jobschedule].[schedule_id] = [schedule].[schedule_id]
INNER JOIN [msdb].[dbo].[syscategories] [categories] WITh(NOLOCK)
ON [jobs].[category_id] = [categories].[category_id]
LEFT OUTER JOIN
( SELECT [job_id], [AvgDuration] = (SUM((([run_duration] / 10000 * 3600) +
(([run_duration] % 10000) / 100 * 60) +
([run_duration] % 10000) % 100)) * 1.0) / COUNT([job_id])
FROM [msdb].[dbo].[sysjobhistory] WITh(NOLOCK)
WHERE [step_id] = 0
GROUP BY [job_id]
) AS [jobhistory]
ON [jobhistory].[job_id] = [jobs].[job_id];