Skip to content

Tag: sql

Update and insert to one table from another

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…

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

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…

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…