I have a table that stores patient lab test results. There can be results from multiple tests like Albumin, Potassium, Phosphorus etc. First reading for each patient from each of these categories is …
Tag: tsql
Join on TOP 1 from subquery while referencing outer tables
I am starting with this query, which works fine: SELECT C.ContactSys , … a bunch of other rows… FROM Users U INNER JOIN Contacts C ON U.ContactSys = C.ContactSys LEFT JOIN …
T-SQL Union (distinct, not all) and Order by with priority
Assume I have a Tag table (Id int and Name nvarchar(100)) with 3 values: 1 Software 2 Hardware 3 Warehouse Now I want to query with keyword, and prioritize the ones that starts with the keyword over …
SQL Pivot String Data
I have a tables in SQL Server: Product Product Table: This is the output that I need: An ImageID can have maximum 3 ProductID It is not necessary that all ImageID will have 3 products [eg. ImageID=3] Answer Your were very close, you just needed to incorporate Row_Number() Example
Return multiple tables specific to each column
I would like to get a more simple query that runs through all columns of a table and returns the SAME number of tables for EACH column. For example… select distinct a from tbl; select distinct b …
SQL: Sort rows based on attribute order
I want to sort an the value of an attribute (called position) to the row number of another attribute’s ordering. Example: If I have the following attributes: position, points I’d like to order the …
Create parent/child relation in SQL server based on the below scenario
In this picture, 2018_9UKDT – UKD Expense Total is parent of 2018_9Q400 – UKD Indirects and this in turn is parent to 98064 – IT SDS Costs, 98063 – ACS in charges, 98012 – UKD …
SQL MAX(COUNT(*)) GROUP BY Alternatives?
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 …
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 …
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 …