I have two tables as below. I want to join these two tables and need to set the column values of second table as column header as shown below. How can I achieve this? Table1 table2 The output table should be Answer You can use Dynamic Pivot as below-
Tag: sql-server
“Distinct” results in complex query
I have the following query, which returns all merchants in the database who have transactions between two given dates. But, there is additional information I need to include in the query. I’ve written the enhanced query below, but it results in duplicated merchants. I know the query could be written bet…
Extract string between two characters in a string
I have a set of strings that has datetime values and I would like to extract them. I am not sure if this is even possible using T-SQL. Expected Results: Answer For this dataset, string functions should do it: The idea is to count backwards from the end of the string, and capture the 14 characters that preced …
How to update row if exist and meet more than one condition and insert it otherwise in SQL Server
I’m trying to perform the below code which I have found it in a thread here The problem is that I need to do the same thing but with the SQL Server query. My main thought here is that if the new read has the same BoxNo and the same SSCC and the same StyleBarcode, then the value in Qty
Count the number of appearances of char given a ID
I have to perform a query where I can count the number of distinct codes per Id. The output should be something like: Can you give me some advise on this? Answer If the codes are only to the provided ones, the following query can provide the desired result. Note: as I can see from sample input data, code R…
Set row number for batch of N rows per category
I have table I want to create a another column in which i have to put row number. Logic : For 3 rows, it should be same and change to next value for 4th row. And Whenever the PHASE is changing, it should go to next number even if the set of 3 is not complete for the previous number.
SQL change subquery to join
I have the following query, which has a subquery. I need the subquery converted to a join with a separated-out “where” clause for the dates, which will be parametised in an excel spreasheet. The subquery obtains a count of all the merchant’s transactions between given dates. I’ve tried…
Using PIVOT to get SELECT into one column, not row
I am trying to make a select, which would show the result into ONE column, not row. I tried using PIVOT, but I am not successful at all. Result of my select: I want to have it like this: Answer You can use cross apply to obtain a single column: results: or, if you prefer having a separate column for
Query and Pivot Column for the last last 3 Years SQL
My need is to retrieve sell data from and pivot the last three years and sum the quantities. See example below. I wonder how to do it. I read that SQL pivot is the way to go, but I can’t figure out …
SQL query to display data in gridview
I have a grid view that displays data from a SQL Server database during page load. My gridview contains these columns: I have used a query: which will display data from SQL Server in the gridview when ReturnedOn column doesn’t have any data. The new query has to satisfy these conditions, It Should not d…