Why I Can't show only row who has max value in Sql Server? - sql

This is my query :
WITH TABLE as (
SELECT distinct STJH.[META_CODE_ORIGINE] AS Salle_TMD_JOUR_HISTORIQUE_ID
,STJH.[STJH_DATE_ACTION]
,STJH.[STJH_TYPE_ACTION]
, STH.ST_ID,
STJH.[STJ_JOUR]
,STJH.[STJ_HEURE_DEBUT]
,STJH.[STJ_HEURE_FIN]
,STH.[META_CODE_ORIGINE] AS SALLE_TMD_HISTORIQUE_ID
,STH.[ST_DATE_DEBUT]
,STH.[ST_DATE_FIN]
,STH.[STH_DATE_ACTION]
,MAX (sth.STH_DATE_ACTION) over (partition by STH.[META_CODE_ORIGINE]) as max_date_action,
FROM [BI_EASILY].[bloc].[D_TMD_JOUR_HISTORIQUE] STJH
left JOIN [BI_EASILY].[bloc].[D_TMD_HISTORIQUE] STH ON STJH.[ST_ID] = STH.[ST_ID]
WHERE STJH.[STJH_TYPE_ACTION] <> 3
AND STJH.ST_ID = 37 and STJH.ST_ID = 37
AND STJ_JOUR = 1 )
select * from table where table.max_date_action <= stjh_date_action
This is my result ( sorry i didn't copy manualy a lot of data)
I want to show only last row whose has max value of STJH_Date_Action. When i will remove my filter « Where STJ_JOUR =1 (Monday). I want to show only one row having STJ_JOUR_VALUE = 1. I tried to do this by this condition « where max_date_action< = STJH_DATE_ACTION » but it doesn’t work. Can you help me please.

for all the records in the result (max_date_action<=stjh_date_action) as you partition the data by (STH.[META_CODE_ORIGINE])

Related

Change successful SQL Server Select with JOINS to an Update

I have a log file tblktime that contains all activity for 'Ourfile'. A second summary table tblktimefst (one record per ourfile) has been created to capture the first occurrence of particular codes in the log file and summarize the 'duration' column between the first record for that ourfile and when it appears in the tblktime log. (The first occurrence of 'LSF' is stored as fstLSFBtime.)
The Select statement that I have written accurately returns the unique id field 'Ourfile' and the correct sum as 'TotDur' that I would like to update back to tblktimefst.fstLSFDurSum
SELECT DISTINCT
ktf.ourfile,
SUM(CASE WHEN kt.btime <= ktf.fstlsfbtime THEN kt.duration ELSE 0 END) AS TotDur
FROM
tblktime kt
INNER JOIN
tblktimefst ktf ON ktf.ourfile = kt.ourfile
AND kt.btime <= ktf.fstLSFBtime
GROUP BY
ktf.ourfile
SQL Server returns:
Ourfile
TotDur
47661
48
50265
48
36453
13
38270
54
39730
27
46156
190
37392
46
51905
25
and so on
The column that I'd like to update in tblktimefst is fstLSFDurSum for its unique 'Ourfile' record. I cant get the syntax correct to do so and hope I have provided enough info that someone may be able to assist.
If I can get the syntax correct for this one then I should be able to complete duration summaries between additional codes as well.
Thank you in advance.
What about (untested):
UPDATE tblktimefst
SET fstLSFDurSum = TotDur
FROM tblktime t2
INNER JOIN
(
select distinct
ktf.ourfile,
SUM( CAse when kt.btime <= ktf.fstlsfbtime then kt.duration else 0 end) as TotDur
from tblktime kt
Inner JOIN tblktimefst ktf on ktf.ourfile = kt.ourfile and kt.btime <= ktf.fstLSFBtime
group by ktf.ourfile) x ON x.ourfile = t2.ourfile

Need to add a subquery or something else to get a record from repeated rows?

I have the following code:
SELECT CONTRACT_QUOTE_ID,COUNT(CONTRACT_QUOTE_ID )
FROM CONTRACT_QUOTE
GROUP BY CONTRACT_QUOTE_ID
HAVING COUNT(CONTRACT_QUOTE_ID) > 1
Which basically shows the ids and the amount of time repated. But on each id there's a field that is the "CONTRACT_QUOTE_NO", but I don't know how to show them.
I've tried to do a subquery, but failed hard at it. Here's the code :
SELECT c1.CONTRACT_QUOTE_ID, c1.CONTRACT_QUOTE_NO, COUNTING = (SELECT COUNT(c2.CONTRACT_QUOTE_ID)
FROM CONTRACT_QUOTE c2
WHERE c1.CONTRACT_QUOTE_ID = c2.CONTRACT_QUOTE_ID)
FROM CONTRACT_QUOTE c1
GROUP BY c1.CONTRACT_QUOTE_ID,c1.CONTRACT_QUOTE_NO
HAVING COUNTING > 1
What could i do?

How to replace a value in one field by a value from another field (different column) within the same view table (SQL)?

I'd like to know if it is possible to replace a value from one field by using a value from another column and a different row.
For Example, click this to view the table image.
I'd like the SumRedeemed value 400 in row 15 to be replaced by the value -1*(-395); the value -395 comes from the EarnPointsLeft in row 6 (both have the same CID meaning that they are the same person). Any suggestions?
You need this update statement:
update t
set t.sumredeemed = (-1) * (select earnpointsleft from FifoPtsView where cid = t.cid)
from FifoPtsView t
where t.cid = 5000100008 and t.earnpointsleft = 0
This will work if the select statement will return only 1 row.
you can simply update your table
update t
set t.sumredeemed = ABS(t2.earnpointsleft )
from FifoPtsView t join FifoPtsView t2 on t.cid = t.cid and isnull(t2.earnpointsleft,0)>0
if you want negative values you can remove ABS ,
please give me your feedbacks

Count the number of rows in table

Hello i'm trying to get the total number of rows with this query doing Count(s.Folio) but i'm getting this result and i need to get 3 in this case
SELECT
ROW_NUMBER() OVER (ORDER BY
s.InstitucionOrigenId
) AS Consecutivo,
s.InstitucionDestino,
s.InstitucionOrigen,
#FechaAtencion FechaAtencion,
COUNT(s.Folio) TotalDocumentos,
DetalleFolio.Estado,
s.Folio,
s.FormaEntrega,
s.FechaPresentacion,
s.NumeroCuenta,
s.NumeroReferencia,
s.Importe,
s.Divisa,
DetalleFolio.FechaVencimiento
FROM
doc.vDocumento s
CROSS APPLY ope.fObtenerDetalleFolio(s.Folio, 1) DetalleFolio
WHERE
DetalleFolio.Estado = CASE
WHEN #EnBancoOrigen = 1
THEN 'RECIBIDO'
ELSE
'ATENDIDO'
END
GROUP BY
s.InstitucionOrigenId,
s.InstitucionDestino,
s.InstitucionOrigen,
DetalleFolio.Estado,
s.Folio,
s.FormaEntrega,
s.FechaPresentacion,
s.NumeroCuenta,
s.NumeroReferencia,
s.Importe,
s.Divisa,
DetalleFolio.FechaVencimiento
i'm kinda new to sql what am i missing? thanks in advance
Remove the group by and replace the count column with:
count(*) over () TotalDocumentos

MAX Subquery in SQL Anywhere Returning Error

In sqlanywhere 12 I wrote the following query which returns two rows of data:
SELECT "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
FROM "eDatabase"."OrderingInfo"
JOIN "eDatabase"."Vendor"
ON "eDatabase"."OrderingInfo"."ORD_VEN_FK" = "eDatabase"."Vendor"."VEN_PK"
WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1
Which returns:
'**United Natural Foods IN','2018-02-07 15:05:15.513'
'Flora ','2018-02-07 14:40:07.491'
I would like to only return the row with the maximum timestamp in the column "ORD_Timestamp". After simply trying to select by MAX("eDatabase"."OrderingInfo"."ORD_Timestamp") I found a number of posts describing how that method doesn't work and to use a subquery to obtain the results.
I'm having difficulty creating the subquery in a way that works and with the following query I'm getting a syntax error on my last "ON":
SELECT "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
FROM ( "eDatabase"."OrderingInfo"
JOIN
"eDatabase"."OrderingInfo"
ON "eDatabase"."Vendor"."VEN_PK" = "eDatabase"."OrderingInfo"."ORD_VEN_FK" )
INNER JOIN
(SELECT "eDatabase"."Vendor"."VEN_CompanyName", MAX("eDatabase"."OrderingInfo"."ORD_Timestamp")
FROM "eDatabase"."OrderingInfo")
ON "eDatabase"."Vendor"."VEN_PK" = "eDatabase"."OrderingInfo"."ORD_VEN_FK"
WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1
Does anyone know how I can adjust this to make the query correctly select only the max ORD_Timestamp row?
try this:
SELECT TOP 1 "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
FROM "eDatabase"."OrderingInfo"
JOIN "eDatabase"."Vendor"
ON "eDatabase"."OrderingInfo"."ORD_VEN_FK" = "eDatabase"."Vendor"."VEN_PK"
WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1
order by "ORD_Timestamp" desc
this orders them biggest on to and say only hsow the top row