Skip to content

Tag: sql-server

Find names based on max number dynamically in SQL

I have a data set like this: Table A: What’s the maximum number of Place on which a Partner has spent money and who are these Partners? Display only the Partners reaching this max number I tried this: But I feel like its not the right logic. What am I missing? Answer There’s really no need for you…

How to query cumulative sum period by month

I have an issue with table source like this: Available month in table is only as is from column month, no April, May, June etc. I need to create a view to create “Year to Date” table as shown here: I need to view all months from Year 2020 January – December with value based on table source. …

Search by best values for every minute

The problem I’m having is that I have a table of data that has a new row added every second (imagine the structure {id, timestamp(datetime), value}). I would like to do a single query for MSSQL to go through the table and output only the number of objects that have the top 2 values asc for each minute (…

T-SQL Logic (Where exists one column but not the other)

I need a bit of logical help in writing a query. Here is the set up. I need a query that returns all records from main_table where the id of main_table matches the id of sub_table, but seq of main_table does not match seq of sub_table. Expected result is id seq A1 3 A2 2 A2 3 My attempt What