I have two tables: table1: (ID, Code, Name) table2: (ID, Code, Name) with same columns I want to to insert data from table1 to table2 or update columns if that exists in table2 (table1.ID = table2.ID) What is the simple way to do this? WITH OUT MERGE Answer There are some issues with Merge statement,so it sho…
Tag: sql
How to decide which fields must be indexed in a database table
Explanation I have a table which does not have a primary key (or not even a composite key). The table is for storing the time slots (opening hours and food delivery available hours) of the food shops. Let’s call the table “business_hours” and the main fields are as below. shop_id day (0 R…
Checking if a value exists in sqlite db with Go
I’m writing code to manage users in a sqlite database with Go. I’m trying to check if a username is taken, but my code is ugly. My table looks like: And I check if a username is taken with: Is there a better query I could use that would tell me if the username value exists in the table in
Select from two tables with group by date
I have two tables: Table t1: So from this table I want to get count field Id for each day. For this I use next query: It’s work good. So next table is t2. To select data by date from this table I use next query: It’s also work good. So my purpose is merge these two queries into one
Select all rows and ignore the first row
I’m currently using the following SQL query which is returning 25 rows. How can I modify it to ignore the first row: I’m using SQL Server 2008. Thanks. Answer You can use EXCEPT in SQL Server 2008. For SQL Server 2012 and above, you can use FETCH OFFSET
Convert iso_week to calendar date in SQL
I’ve been searching though the archives without finding what I am looking for- I’d be happy for some guidance. I have a data set where I want to report aggregated number of appointments by provider (STAFFID) and work week, the latter defined by the week’s Monday date. I’ve played with …
Update a single column on multiple rows with one SQL query
I need to update a single column over a thousand rows in the database. Normally when I need to do this, I’ll do the following: I feel like there should be a way to do this easily, but after searching around online, I cannot find a solution. What I was hoping for was this: An important piece of informati…
the row values updated or deleted either do not make the row unique or they alter multiple rows
I want to delete row and I get this error: the row values updated or deleted either do not make the row unique or they alter multiple rows Answer There are duplicate rows in your table. When this is the case you cannot edit table using UI. first delete rows with matching data using SQL then try and edit. Dele…
Add an autoincrementing ID column to an existing table with Sqlite
Using Sqlite, I want to add an auto-incrementing ID column to an existing table which had previously no ID: How to do it properly? I have this error: sqlite3.OperationalError: near “auto_increment”: syntax error Answer An SQLite table cannot modified in a significant manner using alter table once …
LAG functions and NULLS
How can I tell the LAG function to get the last “not null” value? For example, see my table bellow where I have a few NULL values on column B and C. I’d like to fill the nulls with the last non-null value. I tried to do that by using the LAG function, like so: but that doesn’t quite wo…