Skip to content

Tag: sql

Return Most Recent Record group by 2 ID fields

I need to display the current status of a part (‘approved’ or ‘not approved’) I am setting up the status table as MaterialNo, ToolID, Status, Asofdate the idea is I may have several tools approved and several not approved. I want to be able to see the most recent status for each tool b…

How to select count between start to (start+length) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago. Improve this question How the following result appears when creating a table as above. This is the result of count…

Ordering an alias column in union query

I have a SQL query that utilises a union a few times, and within it I’ve created an alias column called ‘Category’. When I try a simple order by on the alias column it works fine. However the data in my alias column has numbers and they order like 1,10,11 instead of 1,2,3 etc.. When I try ch…

Rewrite MS Access SQL statement to SQL Server

I wish to convert the following MS Access SQL statement to SQL Server. All of my attempts are resulting in different results from the old & original data. The new Query is: The problem caused by grouping date column in new statement, but in old one it is used in Last function to avoid grouping it and stil…

JSONB sort aggregation

I found this query that suits my needs thanks to this answer here in order to sort fields of data in a JSON document. (Fake, generated random data) SELECT jsonb_agg(elem) FROM ( SELECT * FROM …

Where clause in upsert conflict target

Suppose I have a table created like this: When attempting to insert a new row, there are three possibilties: No row for ‘name’ exists. The new row should be inserted. A row for ‘name’ exists that has a value of NULL. The existing row should be updated with the new value. A row for &#82…