Skip to content

Tag: sql-server

Get Row value as Column Header

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-

“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…

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 &#82…

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

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…