Get rows with single values using SQlite - sql

By using SQlite, I'd like to get all rows that show in a specific column only one single distinct value. Like from following table:
A B
1 2
2 1
3 2
4 3
5 1
6 1
7 2
8 4
9 2
Here I'd like to get only row Nr. 4 an 8 as there values (3 and 4) occur only once in the entire column.

You could use a query like this:
SELECT *
FROM mytable
WHERE B IN (SELECT B FROM mytable GROUP BY B HAVING COUNT(DISTINCT A)=1)
Please see fiddle here.
Subquery will return all B values that are present only once (you could also use HAVING COUNT(*)=1 in this case), the outer query will return all rows where B is returned by the subquery.

Related

distinct value row from the table in SQL

There is a table with values as below,
Id Value
1 1
2 1
3 2
4 2
5 3
6 4
7 4
now need to write a query to retrieve value from the table and output should look as
ID Value
1 1
3 2
5 3
6 4
any suggestion ?
The query you want is nothing to do with being distinct, it's a simple aggregation of value with the minimum ID for each:
select Min(id) Id, value
from table
group by value

Mapping columns in oracle-sql

Table:
column 1 column 2 column 3
2 two 3
5 five 8
3 three 10
8 eight 11
12 one 15
I want to create a new column column 4like below:
column 1 column 2 column 3 column 4
2 two 3 three
5 five 8 eight
3 three 10
8 eight 11
12 one 15
I want to map column 3 and column 1 and if there's a match column 4 takes values of column 2.
Example: Value 3 in column 3 is present in column 1, so column 4 will take corresponding column 3 value three.
Thanks!
This looks like a left join:
select t.*, tt.column2 as column4
from table1 t left join
table1 tt
on t.column3 = tt.column1;
EDIT:
If you want to set the value, you can use update:
update table1 t
set column4 = (select tt.column2 from table1 tt where t.column3 = tt.column1)
where exists (select 1 from table1 tt where t.column3 = tt.column1);
However, this seems silly. You can easily get the value in the table using an explicit join or hiding the logic in a view.

Concatenate data if null

I would like ROW_Number() to work normally UNLESS column 'box' is Null. If 'box' is null the row number doesn't increase.
I have data that looks like this...
Row Box
1 5
2 3
3 1
4 Null
5 Null
6 2
7 8
8 Null
9 Null
I want my query to pull out data that looks like this...
Row Box
1 5
2 3
3 1
3 Null
3 Null
4 2
5 8
5 Null
5 Null
I'm trying to avoid using a cursor but I can't figure out how to get this working without one.
You can do this with a correlated subquery. Here is one way:
select (select count(box) from t t2 where t2.row <= t.row) as row,
box
from t
order by row;
This is counting the number of valid box values up to a given row.
In SQL Server 2012, you can do this with a cumulative count():
select count(box) over (order by row) as row, box
from t
order by row;
These assume that row is set as in the question. If row does not start with those values, then you have a problem. SQL tables are inherently unordered, and you need some column to specify the ordering.

Multiple columns from a table into one, large column?

I don't know what in the world is the best way to go about this. I have a very large array of columns, each one with 1-25 rows associated with it. I need to be able to combine all into one large column, skipping blanks if at all possible. Is this something that Access can do?
a b c d e f g h
3 0 1 1 1 1 1 5
3 5 6 8 8 3 5
1 1 2 2 1 5
4 4 2 1 1 5
1 5
there are no blanks within each column, but each column has a different number of numbers in it. they need to be added from left to right so a,b, c, d, e, f. And the 0 from be needs to be in the first blank cell after the second 3 in A. And the first 5 in H needs to be directly after the 1 in g, with no blanks.
So you want a result like:
3
3
0
5
1
4
1
6
1
4
etc?
Here is how I would approach the problem. Insert your array into a work table with an autonumber column (important to retain the order the data is in, databases do not guarnatee an order unless you can give them something to sort on) called id
as well as the array columns.
Create a final table with an autonumber column (see above note on why you need an automnumber) and the column you want as you final table.
Run a separate insert statment for each column in your work table and run them in the order you want the data.
so the inserts would look something like:
insert table2 (colA)
select columnA from table1 order by id
insert table2 (colA)
select columnB from table1 order by id
insert table2 (colA)
select columnC from table1 order by id
Now when you do select columnA from table2 order by id you should have the results you need.

Combine two result sets and still keep one of the columns unique

I have two intermediate result sets in a create view statement. The result sets are derived from two different join paths and I need to union them. But it doesn't stop here. Since the ID column needs to be unique, I will then need the rows in result set 2 that contains the same IDs as the first result set to overwrite the same rows in the first result set.
Let me illustrate this here:
Result set 1
ID Value
------------
1 a
3 a
5 a
6 a
7 a
8 a
Result Set 2
ID Value
------------
2 b
4 b
5 b
7 b
9 b
10 b
End result set
ID value
------------
1 a
2 b
3 a
4 b
5 b
6 a
7 b
8 a
9 b
10 b
I am not sure how to approach this. Union/except/intersect will create duplicate ids, so that's no good.
SELECT COALESCE(set2.ID, set1.ID) AS ID,
CASE WHEN set2.ID IS NULL THEN set1.Value ELSE set2.Value END AS Value
FROM set1
FULL JOIN set2
ON set1.ID = set2.ID
Try deleting elements from result set 1 where id exists in result set 2 before union all.