In sequelize, how do I select records that match all values that i am searching for? - sql

As an example, I have the following table:
T | S
------
1 | 5
1 | 6
1 | 7
2 | 6
2 | 7
3 | 6
Query: array [1,2]
I want to select all values in S that have the value 1 AND 2 in the T Column.
So in the above example I should get as a result (6,7) because only 6 and 7 have for column T the values 1 and 2.
But i do not want to have 5 in my results as 5 does not have 2 in the T column.
How would I do this in sequelize?

how do i make (1,2) to be used as an array?
Either you insert the array joined as comma-separated literal into the query text (variant 1) or you join the array into one string literal and transfer it iinto the query as a parameter (variant 2).
Variant 1
SELECT s
FROM sourcetable
WHERE t IN (1,2) -- separate filter values
GROUP BY s
HAVING COUNT(DISTINCT t) = 2 -- unique values count
Variant 2
SELECT s
FROM sourcetable
WHERE FIND_IN_SET(t, '1,2') -- separate filter values
GROUP BY s
HAVING COUNT(DISTINCT t) = 2 -- unique values count
If (s,t) is unique then DISTINCT keyword may be removed.

Related

Explode a row into multiple rows based on a column value presto

I am looking to explode a row into multiple rows based on a column[integer] value, I am trying to do this using presto
Below is an example
id
count
1
5
2
2
expected output
id
count
1
5
1
5
1
5
1
5
1
5
2
2
2
2
in the above example, id 1 has to be repeated 5 times and id 2 has to be repeated 2 times based on the count.
Based on my experience, presto doesnt support recursive CTE.
Any help would be appreciated.
Thanks
You could make the count into array with REPEAT and then CROSS JOIN.
Your input:
CREATE TABLE test AS
SELECT id, count
FROM (
VALUES
(1, 5),
(2, 2)
) AS x (id, count)
Then:
SELECT id, t.count
FROM test
CROSS JOIN UNNEST(repeat(count, count)) AS t (count)
You don't need recursive CTE's here, you can use sequence or repeat to generate an array for corresponding length and then flatten it with unnest:
-- sample data
WITH dataset (id, count) AS (
VALUES (1, 5),
(2, 2)
)
-- query
select id, count
from dataset,
unnest (repeat(id, count)) as t (ignore)
-- unnest (sequence(0, count - 1)) as t (ignore)
Output:
id
count
1
5
1
5
1
5
1
5
1
5
2
2
2
2

How to sum values in a column based on values in a different column SQL

For example, lets say I have
id
values
1
10
1
12
1
10
2
2
2
5
2
4
then i would want sql to return
id
values
1
32
2
11
This is very basic sql.
select id, sum(values) as values
from foo
group by id
Select ID,sum(values)
From table
Group by ID;

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

SQL: Selecting rows from non unique column values once partitioned by another column

Using SQL here. Trying to select all rows where the column value is unique within that specific partition.
Have tried:
select *
from dataTable
where value in ( select value
from dataTable
group by tv_id, value
having count(*) > 1)
but it returns the full table-- i think the issue is that the values for many of tv_ids are identical and overlap.
What I have:
tv_id value
1 1
1 2
1 2
1 3
2 1
2 1
2 2
2 3
2 4
3 1
3 1
3 2
What I want:
tv_id value
1 2
1 2
2 1
2 1
3 1
3 1
I have a bunch of tv_ids and essentially, I only want the rows where the value is not unique within each tv_id.
Ex: I don't want tv_id, value: 3, 2 because it is the only combination in the data.
Thanks in advance!
Maybe something like this does the trick
Oracle Option
I include this oracle version because it enables you to understand better what are you querying.
select tv_id, value
from dataTable
where (tv_id, value) in (
select tv_id, value
from dataTable
group by tv_id, value
having count(1) > 1
)
SQL
But this is a standard sql version that will work with almost any database engine
select tv_id, value
from dataTable d1
join (
select tv_id, value
from dataTable
group by tv_id, value
having count(1) > 1
) d2
on d1.tv_id=d2.tv_id
and d1.value=d2.value
You need to query the same table twice because the group by makes a distinct in your data, so you won't retrieve duplicated rows as you show in your expected output.

Get rows with single values using SQlite

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.