I’ve seen many topics about this and none of them is what I’m looking for. Say we have this simple table: CREATE TABLE A ( id INT, date DATETIME ); I want to retrieve the MAX value after …
Tag: sql-server
SQL Server query for calculating time duration for column containing multiple datetime entries against order number and order state
I need a T-SQL query for calculating time duration for column containing multiple datetime entries against order number and order state. Example: I need the query to find out total time duration for …
How can I assign unique value to each duplicate value in a column in a table
I have data in my table as below Now I want to generate unique rank value to each duplicate value like 1,2,3,4 etc as below How to do this in SQL Server 2016?
Round datetime to the beginning of the current hour
I would like to round a datetime (should also return a datetime) to the beginning of the hour it’s on, so for example: 2018-09-22 11:31:42.411 -> 2018-09-22 11:00:00.000 2018-09-22 11:08:02.456 -&…
Use ROLLUP with ORDER BY clause moves the Grand Total row from the bottom to the top
Here’s the schema: This works: However, when I add an ORDER BY clause, the Grand Total row moves from the bottom. I understand the first example has the rows sorted by Genre but I wonder why adding …
MS SQL Join Two Tables with ‘IN’ Faces Problem
I have two tables for my system. Table 1: Book Table 2: BookStatus Now I want to find out the latest status of books that had been lent from 2018/11/05 to 201/11/10 and how many times they have …
Getting a query result taken from the same data but with temporary var
I got a simple thing to do. Well, maybe not, but someone somewhere surely can help me out : P I got a simple data structure that contains expedition date delivery date transaction type I would …
Combine Queries using Union All Dynamically
I have three tables: I combined those tables using UNION ALL. My problem is, each year there is a new table to be added. One example is that Table D with corresponding record Order O1-20 and Year 2020. Instead of adding another set of UNION ALL, is there any other way to achieve the same result. I’m usiā¦
Update table using JSON in SQL
Is there a way to update a table using a JSON field in SQL. { “RelationshipType” : [ { “ID” : 1, “FromID” : 70, “ToID” : 12 }, { …
Indexed View vs. Table
I had a long running query (relative to the data) that was hitting an indexed view. I thought an indexed view was physically stored data? I dumped everything into a table and ran the same query …