Skip to content

Tag: sql

SQL Pivot and get column row data

I have tables and data as below. Trying to get the Manufacturer and the questions they answered by joining and pivot but able to. Added the expected output at the end but no luck. Any suggestions are …

Select Last Rows with Distinct Field

I have a table with the following schema: In this table only the id field is unique. I’m concerned with getting the rows containing the last X distinct itemid, ordered by date. For example, in the sample above, if I’d like to get the last 3 distinct itemid, I’d be getting the first 4 rows, s…

SQL show based on date condition

I have following query to start of with: I then get following table (only a snapshot for better desciption): I now have a problem: I only want to show the services where the service starting with ‘AAF21 OKN’ is first (by date). Patient ID: ‘Forløb10030’ is for example what i want. But …

Insert new records with several ids from another table

I have a table, which has 9 records with id = 1 – 9 (for example, there can be more than 20 ids). I have one varchar value = ‘premium’. I need to insert these values to another table, after this action I should have 9 records with id from the first table and ‘premium’ varchar in …

Firebird column unknown on SELECT AS

I have a table of calls and their durations: I want to select all numbers that called for over 500 seconds in total. My query uses SELECT AS to rename the SUM(…) field so I can reference it in the HAVING clause: When I run this query in Firebird SQL, I get the following error: Answer You can not use

Get pair-wise single row

I have some data in a MS SQL Server database. Sample data is: Sl ColumnA ColumnB 1 111 112 2 112 111 3 113 114 4 114 113 5 115 116 6 116 115 Now I need result like. ColumnA ColumnA 111 112 113 114 115 116 Notice here: In Sl 1 number Column A = Sl 2 Column B