Skip to content

Tag: sql-server

Combine SQL rows into one column based off two other columns

I have this SQL statement: It returns a table like: But I am looking to have something like this: How do I change my SQL statement to accomplish this? Answer The solution to this problem ended up being to create a temp table for the main query. Then pulling the appropriate data out of the temp table utilizing…

Prevent Duplicate Entries in SQL Application

I have an application that’s running on two different machines. The use of the application is pretty simple, we scan a product and it associates the product_id and creates a Unique_ID that’s auto-incremental. Ex: U00001 then the next is U00002 My problem is while both the machines are running, som…

SQL Select Distinct Top5 with Order By and Where

I have a table where Employees can check assets and each of their checks is recorded in a table. Now I want to extract the 5 last checked Asset_Ids for a given employee. He might have checked the same asset more than once, each check is recorded in a table -EmployeeLog- Basically my table has 3 columns : Empl…

How to find the sum from value of sql column

I have two tables with following data. studenttbl: feetbl: I need to find below data, but I am not getting balance value of seatno 1006: I am using this SQL query: Please help me with this. Answer This issue is due to INNER JOIN versus LEFT JOIN. When you INNER JOIN youll only get matches between two tables. …

Split String into rows but keep associated data

I’m not real familiar with Split_String and my searches aren’t turning up anything that I can figure out for my case. What I need to do is split a field into rows each time its delimited but then for each new row created, copy the associated columns with that field to those new rows. How data will…

Count total day difference in a Pivot SQL

I have data in two tables with dates PAL date and HUB date. I’m looking to show the number of day difference in a table with 6 columns: Client 1day 2days 3days 4days 5days Client will display the clients grouped, 1 Day will display the count of how many datediff of 1. 2 Days will display the count of ho…

Reset ROW_NUMBER() when previous partition ID changes

I am trying to reset the ROW_NUMBER() value when there is a change in one of the IDs when sequenced in datetime order. For example in the table below, when Location_ID changes, I want to reset the RowNum to 1. Here’s the statement I am using. I get RowNumX, but want RowNumY. EventDate Scan_Timestamp Ass…