i´m working with a table that looks like this: Start https://i.stack.imgur.com/uibc3.png My desired result would look like this: Result https://i.stack.imgur.com/v0sic.png So i´m triyng to select the max value from two “combined” colums. If the values are the same amount (Part C), the outcome doesn’t matter. I tried to order the table by max value and then using distinct but the
Tag: tsql
Create daily logs based on records that contain multi day values
This is an extension to this question: Break down long multi day periods of time into several single day periods where the problem was solved using Tallies. The situation I am dealing now is let’s say I have a table like this: The goal is to create a daily outage report over a period of time (Let’s say the Month
Migrate columns to JSON in SQL
I have a table (Table A) that looks like this: I want to convert it to a table (Table B) like this How do you insert/transform into a json column? Or do I just have to construct the json string myself? Additionally, does the nullableness of the int columns affect creation? Are clicks for example not included or do they
IIF or case when in window function
I have some timestamps in a table and I want to create a dummy variable (0 or 1) that tests if the row above is equal to the current row, after the timestamps are sorted. I need to do this in different partitions. Is there a window function that can do this in SQL Server? So I know my partition
JOIN with OR condition and use only MIN(Column)
I have two tables. Certain values from table t need to be matched with certain values of table m in order to identify a target value from table m. Table t looks as follows. Table m looks as follows. The logic should be as follows: Column A from table t should be matched with column matchA from table m and
How to update status for part with nothing data when this part is missed?
I work with SQL Server 2012 and have an issue: I can’t update status with No data returned where no result returned from select statement cross apply function. Meaning where no data returned when join parts and company to function then update status to this part with nothing data for that part when data is missing. When I run a
Naming a default constraint
I’m trying to create a default constraint here, but the system is generating a weird name for it. If I want to name it df_MY_TABLE_GUID or something, how could I specify that name be used? Answer Just specify the constraint name with the full syntax, like the UNIQUE in your example: As a matter of routine, I always prefer and
SQL Server – Aggregate data by minute over multiple days
Context I’m using Microsoft SQL Server 2016. There is a database table “Raw_data”, that contains the status of a machine, together with it’s starting time. There are several machines and …
Update table with using NEWID() function
In general, I would update a table randomly like the following scripts. However, I get some issue. It would update more/less then 2 rows. I try execute the following scripts many times for debug. The results are not always only two records. If I remove the order by newid(), the number of output result will be fine. It seems like
TSQL – Compare two columns with self join – Calculate the difference between yesterdays total’s and today’s total
I’m trying to calculate the difference between today’s and yesterday’s totals on a column. If a self join isn’t the best way to do that, that is fine, whichever will give me the result I’m after …