SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE [Tab] (num int NULL) GO SET ANSI_NULLS, QUOTED_IDENTIFIER ON GO DROP PROCEDURE IF EXISTS delete_doubles_from_heap GO /* =========== Task "delete_doubles_from_heap":================== There is a table named Tab There is only one column named num. This column is not unique. Some values can be repeated. You must write some code to delete doubles from table Tab. Note: You cannot use INSERT or UPDATE, only DELETE command is allowed. Note: You cannot modify structure of Tab =========================================================*/ CREATE PROCEDURE delete_doubles_from_heap AS BEGIN /* Modify SQL code below to solve the task: */ WITH D AS (SELECT rn = ROW_NUMBER() OVER(partition by num order by num) from Tab ) delete D where rn > 2 END GO ------- TEST 1 ------- PRINT N'***** delete_doubles_from_heap *****' PRINT N'' PRINT N'test 1: preparing numbers in Tab: 1 1' TRUNCATE TABLE Tab; INSERT INTO Tab VALUES (1), (1); select N'test 1: RUN your qst.delete_doubles_from_heap...' DECLARE @expected_result nvarchar(1000) = N'1' SELECT @expected_result DECLARE @LOG NVARCHAR(1000) SET @LOG = N'test 1: expected result: ' + @expected_result PRINT @LOG DECLARE @actual_result NVARCHAR(1000) = N'' SELECT TOP(10) @actual_result = @actual_result + N' ' + CAST(num as nvarchar(100)) FROM Tab ORDER BY num SET @actual_result = LTRIM(@actual_result) DECLARE @print_actual_result nvarchar(max) SET @print_actual_result = N'test 1: actual result: ' + @actual_result SELECT @print_actual_result

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear