How do I merge the contents of two columns into one in SQLite? I'm not looking to union 2 columns, I just want SQLite to automatically copy the entire contents of two columns and dump them (in any order) into a single column. For example, suppose I have the following table:
Table1:
Column1: Column2:
Red, Yellow
Green, Red
Blue, Gold
Purple, Green
Black, White
And this is the desired result:
Red
Green
Blue
Purple
Black
Yellow
Red
Gold
Green
White
What's the simplest SQLite query that'll get to the desired result?
I tried the following: Select Column1 || Column2 FROM Table1;
But I got the undesired result:
RedYellow
GreenRed
BlueGold
PurpleGreen
BlackWhite
I think that UNION ALL should give you the result:
Select Column1 AS Column_1_2 FROM Table1
UNION ALL
Select Column2 AS Column_1_2 FROM Table1;
Related
So im wondering if its possible for SQLite to understand number ranges.
I want to be able to have a range such as "25-30" and lookup "27" to see if it falls within that range.
The issue is that the range will contain some text beforehand such as "Alice 25-30"
An example of what Id be looking to achieve can be seen in Table3 of this link:
https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=483f62c5fbf13998659cd5f7ebbb3ce9
More than happy for solutions that can break the string at the first number, but still keep the number so
Alice | 25-30
Not
Alice | 5-30 (ive seen this suggested before :D)
To actually create Table 3 ill be using either INNER or LEFT OUTER JOIN not just Re-creating the table but was speedier to do this
Thanks in advance.
You can do it with a join of the 2 tables like this:
INSERT INTO Table3 (`ID`, `Age`,'Age Range')
SELECT t1.ID, t1.Age, t2.`Age Range`
FROM Table1 t1 INNER JOIN Table2 t2
ON t1.Age + 0 BETWEEN `Age Range` + 0
AND SUBSTR(`Age Range`, INSTR(`Age Range`, '-') + 1) + 0
SQLite performs implicit conversions of strings to numbers when they are used in expressions with numeric operations like +0, so what the query does is to compare Age to the 1st and the 2nd part of Age Range numerically.
Note that + 0 would not be needed in ON t1.Age + 0 BETWEEN if you had defined Age as REAL which makes more sense.
Change the INNER join to LEFT join if you want the row from Table1 inserted to Table3 even if there is no matching Age Range.
See the demo.
Results:
ID
Age
Age Range
1
30
25-30
2
40.5
31-45
Can anyone help with the examples as below, thanks a lot
id color date
1 red 01/01
1 red 01/02
1 yellow 01/03
1 red 02/01
2 red 01/01
2 blue 01/02
2 blue 02/02
3 red 01/01
4 red 02/01
The ideal output should be:
id pattern
1 (red, yellow) to (red)
2 (red, blue) to (blue)
3 (red)
4 (red)
The result displays two things:
The unique pattern within one month
should aggregate the pattern together within time ranges
Ex, we can see that id_1 has a pattern changes on Jan from (red,yellow) but on Feb, it only has one pattern (red). Therefore the final output should be (red,yellow) to (red)
The query that I have now is
select drv.id, extract(month from date) as month,
trim(trailing',' from (XMLAGG(TRIM(color)',' order by date)(varchar(10000)))) as pattern from
(select id, color,
lag(color)over(partition by id, extract(month from date) order by date) as prev_color
from table
qualify prev_color <> color
) as drv
group by id, extract(month from date)
The query is incomplete, and it returns result like below,
but is it possible that we may return it by month?
Is there any way that we can mix using XMLAGG and partition by?
Can anyone give any ideas? Thanks a lot!
You need a GROUP BY id in the outer query. In the inner query you can filter out the 'no change' rows but also need to include the date column so you can reference it in the outer query. By default XMLAGG adds a space between items but as long as there are no spaces in the values you can easily translate that to whatever delimiter you prefer. (If there can be embedded spaces, then you can append a delimiter to each entry with the concatenation operator || and trim off the last one, etc.)
select drv.id,
oreplace(XMLAGG(TRIM(color) order by theDate)(varchar(10000)),' ','-') as pattern from
(select id, color, theDate,
lag(color)over(partition by id order by theDate) as prev_color
from theTable
qualify prev_color is null or prev_color <> color
) as drv
group by 1
order by 1
I used theDate and theTable in this example because DATE and TABLE are reserved words.
I have a database of NBA basketball. In it are two different tables, one has info on games that have been played and the other table has player information.
In the Game_Detail table, I have the teams, game date, and scores for each team. I also have 10 columns with the 5 starting players from each team.
Game_Detail Table
Date HomeTeam AwayTeam H_Pts A_Pts H1 H2 H3 H4 H5 A1 A2 A3 A4 A5
1/1 ORL BOS 100 99 1 22 32 55 692 12 33 55 333 90
In the 10 columns H1 through A5, each row has a player ID.
In the Player_Detail table with player information, there is the player ID as well.
Player_Detail Table
player_id name height weight
For every row of H1 through A5 in the first table, the Game_Detail table, I want to replace the player_id with the weight of the player (one of the columns in the Player Detail table).
What is the best way to go about doing this? I'm a SQLite/SQL new user so not sure on best practice as it's a large file. The code I have tried so far to run this is not working at all.
Also, does the command change at all if I want to use name instead of weight (seeing as it's a different type, string instead of integer)?
You can use (a lot of) joins.
SELECT gd01.date,
gd01.hometeam,
gd01.awayteam,
gd01.h_pts,
gd01.a_pts,
pd01.weight,
pd02.weight,
...
pd09.weight
pd10.weight
FROM game_detail gd01
LEFT JOIN player_detail pd01
ON gd01.h1 = pd01.player_id
LEFT JOIN player_detail pd02
ON gd01.h2 = pd02.player_id
...
LEFT JOIN player_detail pd09
ON gd01.a4 = pd09.player_id
LEFT JOIN player_detail pd10
ON gd01.a5 = pd10.player_id;
If you want another detail just change the column references, e.g. from weight to name.
I have an SQLite Database, two of the tables look like this:
ID Name
1 Test1
2 Test2
3 Test3
4 Test4
ID Color
1 Blue
1 White
1 Red
2 Green
2 Red
4 Black
In the first Tables, ID is unique, the second table lists colors an ID has, it can be from 0 to n colors.
Now I want to select all Names exactly once, that have one or more given color. Lets say, I want to have all names associated with blue, white and/or green. The resultset should have the IDs 1 and 2.
I am completly lost here, as I normally dont do any SQL. I am just familiar with very basic SQL. What I would do is Join the tables together, but I dont know how I do that, as ID is not unique in the second table. Also there would be the problem of IDs beeing duplicated in the resultset, if it has multiple colors that I want to select.
Thanks in advance for any help.
You don't need a join for this. Get the list of IDs from the color table in a subquery, and fetch the names from the test table with an in clause:
sqlite> select * from tests where id in
(select id from colors where name in ('Blue', 'White', 'Green'));
1|Test1
2|Test2
Duplicates don't matter in the subquery, but you could use distinct if you want that list without duplicates in other contexts.
I have a simple question about the SQL of an UPDATE query. I found something very close to what I want to know here:
MySQL: Count occurrences of distinct values
But.. it's not an update query. Here's the example of what I want to do:
In one table (let's call the table "data"), I want to make an UPDATE query. Here’s what the table looks like:
Id color count
1 blue 0
2 blue 0
3 red 0
4 red 0
5 blue 0
6 white 0
Now, [id] is the auto-incremental key for this example, [color] is a TEXT field. [count] is a number
What I want, is for the count to be UPDATED so that it tells how many instances a [color] occurs. After the UPDATE query runs, the table would look like this:
Id color count
1 blue 3
2 blue 3
3 red 2
4 red 2
5 blue 3
6 white 1
Looks pretty simple, but I've messed with the DCOUNT and COUNT commands, and I'm probably missing something very easy, but still... no joy. All the help I've seen only only deals with SELECT queries, but I will definitely need this query to update the [count] field.
Thanks in advance!
You should try :
UPDATE data JOIN
(
SELECT color, count(1) AS color_cnt
FROM data
GROUP BY color
) AS sub ON data.color = sub.color
SET cnt = sub.color_cnt