Get Max value of a DATE and an ID in order to filter results - tsql

I'm trying to get the latest "date" so the max value of "date" and from the same table I want the max value of "stand" also from the same ID.
I have tons of dates, stands for one ID but i only want to extract the latest.
im trying to save it into a function i dont know yet if thats the best idea. The rest of my query It's made of inner joins.
Datum is of type date.
stand is decimal(18,6)
DECLARE #MAXDATE DATE
DECLARE #MAXSTAND decimal(18,6)
SELECT #MAXDATE = MAX(Datum) FROM [dbo].[1] WHERE ID = ID
SELECT #MAXSTAND = Stand FROM [dbo].[2]WHERE ID = ID
Result I get: #MAXDATE: 2106-10-13
Result I get: #MAXSTAND: 0.000000
Result I want: #MAXDATE: 2018-01-16
result I want: #MAXSTAND: 1098.000000

Assuming SQL Server 2012 or higher, you can use the first_value window function:
SELECT FIRST_VALUE(Stand) OVER(ORDER BY Datum DESC)
FROM TableName
WHERE Id = #Id
This will return the value of Stand where the Datum column has the latest value for the specific Id.

With the data provided, I don't think you need to do something else :
SELECT MAX(Datum), MAX(Stand)
FROM TableName
WHERE ID = #MyId
edit : You want it by ID, you can do this :
SELECT MAX(Datum), MAX(Stand), ID
FROM TableName
GROUP BY ID

Related

How to work with data values formatted [{}, {}, {}]

I apologize if this is a simple question - I had some trouble even formatting the question when I was trying to Google for help!
In one of the tables I am working with, there's data value that looks like below:
Invoice ID
Status
Product List
1234
Processed
[{"product_id":463153},{"product_id":463165},{"product_id":463177},{"pid":463218}]
I want to count how many products each order has purchased. What is the proper syntax and way to count the values under "Product List" column? I'm aware that count() is wrong, and I need to maybe extract the data from the string value.
select invoice_id, count(Product_list)
from quote_table
where status = 'processed'
group by invoice_id
You can use a JSON function named: json_array_length and cast this column like a JSON data type (as long as possible), for example:
select invoice_id, json_array_length(Product_list::json) as count
from quote_table
where status = 'processed'
group by invoice_id;
invoice_id | count
------------+-------
1234 | 4
(1 row)
If you need to count a specific property of the json column, you can use the query below.
This query solves the problem by using create type, json_populate_recordset and subquery to count product_id inside json data.
drop type if exists count_product;
create type count_product as (product_id int);
select
t.invoice_id,
t.status,
(
select count(*) from json_populate_recordset(
null::count_product,
t.Product_list
)
where product_id is not null
) as count_produto_id
from (
-- symbolic data to use in query
select
1234 as invoice_id,
'processed' as status,
'[{"product_id":463153},{"product_id":463165},{"product_id":463177},{"pid":463218}]'::json as Product_list
) as t

more than one row returned by a subquery used as an expression problem

I am trying to update a column in one database with a query:
Here the query
and this is the output i think it is impossible to asign a query to a field but what is the solution for that plz.
enter image description here
= can be used when we are pretty sure that the subquery returns only 1 value.
When we are not sure whether subquery returns more than 1 value, we will have to use IN to accommodate all values or simply use TOP 1 to limit the equality matching to one value:
UPDATE mascir_fiche SET partner = (SELECT TOP 1 id FROM hr_employee WHERE parent_id IN (SELECT id FROM hr_employee));
With Limit:
UPDATE mascir_fiche SET artner = (SELECT id FROM hr_employee WHERE parent_id IN (SELECT id FROM hr_employee) limit 1);

SELECT max(date) return the latest record but the wrong data

I have multiple records all with the same user_id and each with its unique TIMESTAMP.
I need to return the correct data for a user_id based on the latest date - MAX(date).
My query returns the last date but the incorrect data (ie Data associated with a previous record-set):
SELECT user_id, doc_docpath_00, max(doc_uploadtimestamp) FROM doc
WHERE user_id = '90';
Could you help me understanding how to correct my query?
I don't think it can work that way.
I believe you should try something like:
select user_id, doc_docpath_00
from doc
where user_id = '90' and doc_uploadtimestamp = (
select max(doc_uploadtimestamp)
from doc where user_id = '90');

excluding rows from resultset in postgres

This is my result set
i am returning this result set on the base of refid using WHERE refid IN.
Over here, i need to apply a logic without any kind of programming (means SQL query only).
if in result set, i am getting period for particular refid then other rows with the same refid must not returned.
for example, 2667105 having period then myid = 612084598 must not get returned in result set.
according to me this can be achieved using CASE but i have no idea how to use it, i mean that i don't know should i use the CASE statement in SELECT statement or WHERE clause...
EDIT:
This is how it suppose to work,
myid = 612084598 is the default row for refid = 2667105 but if specifically wants the refid for period = 6 then it must return all rows except myid = 612084598
but if i am looking for period = 12, for this period no specific refid present in database.. so for this it must return all rows except first one.. means all rows with the refid which is default one..
Not very clear definition of the problem, but try this:
with cte as (
select
*,
first_value(period) over(partition by refid order by myid) as fv
from test
)
select
myid, refid, period
from cte
where period is not null or fv is null
sql fiddle demo

Need some help in creating a query in SQL?

ID|message| UpdateTime| TicketID| StaffID
10008;"Yes, it is!";"2012-04-15 16:15:00";1008;660
10013;"Thanks for swift reply!";"2012-04-15 17:15:00";1008;660
CAn u tell me when I write these 2 queries :
Select MAX(UpdateTime) from TicketUpdate where ticketUpdate.id = 10008;
Select MIN(UpdateTime) from TicketUpdate where ticketUpdate.id = 10008;
The output the same even in my database if have 2 different times.
Can u tell me what could be the problem here?
ID appears to be the unique identifier for this table. Instead, it looks like you want to use TicketID to find the max and min values per ticket.
For example, to find them for TicketID = 1008:
SELECT MAX(UpdateTime) FROM TicketUpdate WHERE TicketUpdate.TicketID = 1008;
SELECT MIN(UpdateTime) FROM TicketUpdate WHERE TicketUpdate.TicketID = 1008;
Or in one query:
SELECT
MAX(UpdateTime) AS newest,
MIN(UpdateTime) AS oldest
FROM TicketUpdate
WHERE TicketID = 1008;
To get the most recent and oldest for every individual TicketID, use a GROUP BY and omit the WHERE clause.:
SELECT
TicketID,
MAX(UpdateTime) AS newest,
MIN(UpdateTime) AS oldest,
FROM TicketUpdate
GROUP BY TicketID
If you query using ID, you will always get the same row since there appears to be only one value for each ID that uniquely identifies its row.