Skip to content

Tag: sql

Same ID with multiple records to single row

I have two tables. Table A look like this. source ID Type_ID Error_info ABC 100 1 country column missing ABC 100 2 conversion factor missing decimals BCA 200 1 error value in height column BCA 200 2 convertion factor should be 0.001 Table B look like this. source ID Type_1_ID Error_info_1 Type_2_ID Error_info…

left join on the first appeared row by datetime

If classes ‘Math’ or ‘Biology’ have flag is NULL or = 0, we need to left join with the first appeared (according to the date) classes ‘Literature’ or ‘English Literature’ from the below. If classes ‘Math’ or ‘Biology’ have flag = 1 then w…

How to add a column default?

I created a Postgres DB which contains 5 tables. Then I realized that the column student.student_id lacks a column default to generate an UUID: Knowing that some tables are linked to the student table, how can I add to the function as column default for student_id so that the result will be: Answer Use ALTER …

How to update with order in SQL Server?

I am using SQL Server, and I have a table that looks like this: I want to update id to have a sequence of rows ordered by distance asc, then i asc I tried this but I am getting an error Incorrect syntax near “order” How to fix that? Answer You need to use ROW_NUMBER inside a derived table or

How can combine rows of a table with sql?

I have a table like this: Type rank A 1 A 2 B 3 A 4 B 5 B 6 B 7 A 8 And i want convert it to this table with sql query: Type rank A 2 B 3 A 4 B 7 A 8 How can I do this with and with out window functions? Answer It