Я знаю, что могу использовать Min()
, чтобы получить самое низкое значение. Итак, я использовал его в следующем запросе, и я ожидаю увидеть одно значение для каждого ticketID, но оно дает мне 2. Я должен также добавить, что у меня есть более 2 дат для столбца, который меня интересует в БД. Может кто-нибудь поможет мне понять, что я делаю неправильно.
USE xxxx
SELECT DISTINCT Access_R.ticketid,
Access_R.[number of built],
Iif(CW.ownedbyteamj IN( 'Effectiveness',
'Review',
'review' ),
Datediff(day, CW.createddatetime, Min(CW.createddatetimej)), Iif
(
CW.ownedbyteamt IN ( 'Effectiveness',
'Review',
'review'
), Datediff(day, CW.createddatetime, Min(CW.createddatetimet)),
-20000)) AS
'journal_s',
Datediff(day, CW.createddatetime, Access_R.submissiontodt)
AS
'From Ticket_Creation toSubmission',
( CASE
WHEN (
Access_R.submissiontodt > '1901-04-06 00:00:00.000' )
AND ( Access_R.buildcmpltdt >
'1901-04-06 00:00:00.000' ) THEN
Cast(
Datediff(day, Access_R.submissiontodt, Access_R.buildcmpltdt) AS
NVARCHAR
(255))
ELSE 'no data for mbuild review'
END )
AS 'From Submission to Build',
Datediff(day, Access_R.submissiontodt, Access_R.orderingtoolbuildcmpltdt)
AS
'From Submission toBuild',
Datediff(day, Access_R.submissiontodt, Access_R.orderingtoolvalidcmpltdt)
AS
'From Submission to Validation',
Datediff(day, Access_R.submissiontodt, Access_R.inproductiondt) AS
'From Submission to Migration'
FROM (SELECT t1.ticketid,
t1.submissiontodt,
t1.awaitingmigrationdt,
t1.inproductiondt,
t1.mbuildcmpltdt,
t1.orderingtoolbuildcmpltdt,
t1.orderingtoolvalidcmpltdt,
Datediff(day, t1.submissiontodt, t1.inproductiondt) AS
'Days from submission to PRD',
Count(t2.lid) AS
'Number ofbuilt'
FROM ticket AS t1
LEFT JOIN timepoint AS t2
ON t1.ticketid = t2.ticketid
WHERE internalbuildrequesttypeid <> 4
AND internalbuildrequesttypeid <> 5
AND internalbuildrequesttypeid <> 6
AND t1.ticketbuildstatusid <> 12
AND t1.ticketbuildstatusid <> 11
AND t1.ticketbuildstatusid <> 10
AND t1.submissiontodt >= '2016-01-01'
AND t1.submissiontodt <= '2019-10-01'
GROUP BY t1.ticketid,
t1.submissiontodt,
t1.inproductiondt,
t1.protocolnumber,
t1.internalticketstatusid,
t1.ticketbuildstatusid,
t1.awaitingmigrationdt,
t1.mbuildcmpltdt,
t1.orderingtoolbuildcmpltdt,
t1.orderingtoolvalidcmpltdt) Access_R
INNER JOIN (SELECT incidentid,
ownedbyteamj,
createddatetimej,
createddatetimet,
( CASE
WHEN journaltypename = 'Journal - Note'
AND ( ownedbyteamj LIKE
'EHR - Effectiveness'
OR ownedbyteamj LIKE 'Review'
OR ownedbyteamj LIKE 'review')
--AND Status in ('Submission','CT Review')
THEN Min(createddatetimej)
ELSE -1000
END ) AS
'Journal_ps',
Iif(( ownedbyteamt LIKE 'Effectiveness'
OR ownedbyteamt LIKE 'review'
OR ownedbyteamt LIKE'Review'
AND clinicaleffecrequeststatus IN
( 'Submission', 'CCT Review' ) ),
Cast(Datediff(day, ( createddatetimet ), (
closeddatetimet ))
AS VARCHAR
(255)),
'No data') AS
'Task-Review',
Iif(( ownedbyteamt LIKE 'Effectiveness'
OR ownedbyteamt LIKE 'review'
AND status IN
( 'Submission', 'CT Review' ) ), (
closeddatetimet ), 0) AS
'Max_Crops',
Iif(( ownedbyteamt LIKE 'Wil'
AND status NOT IN (
'Approval', 'Billing',
'Migration', 'Activation' ) ), Cast(
Datediff(day, Min(createddatetimet), Max(
closeddatetimet)) AS
VARCHAR(
255)),
'No data') AS
'Wil',
Iif(ownedbyteamt LIKE 'affectiveness', Cast(
Datediff(day, Max(createddatetimet),
Max(
closeddatetimet)) AS NVARCHAR(255)
), 'No Data') AS
'Time',
titlet,
createddatetime,
ownedbyteamt,
status,
closeddatetimet,
statuscctreviewdatetime
FROM taskview
GROUP BY createddatetimej,
createddatetimet,
incidentid,
titlet,
status,
ownedbyteamt,
ownedbyteamj,
closeddatetimet,
statuscctreviewdatetime,
status,
journaltypename,
ownedbyteamj,
createddatetimej,
createddatetime) CW
ON Access_R.ticketid = CW.incidentid
GROUP BY Access_R.ticketid,
CW.ownedbyteamj,
CW.createddatetimej,
Access_R.submissiontot,
Access_R.[number of built],
CW.createddatetime,
Access_R.orderingtoolbuildcmpltdt,
Access_R.orderingtoolvalidcmpltdt,
Access_R.inproductiondt,
Access_R.mbuildcmpltdt,
CW.ownedbyteamt,
CW.status,
CW.createddatetimet
Вот что я вижу, когда запускаю скрипт:
Я также добавилснимок того, что я вижу .. Спасибо
Это то, что у меня есть в БД, и я хочу самый низкий.