SQL Server select all max and min data in a column - sql

+----+------+
| id | data |
+-----------+
| 1 | a |
| 2 | b |
| 3 | a |
| 3 | c |
+----+------+
I need to select data [a,b,c].

Yes, got it.
the query is 'SELECT distinct(data) from dataTable'
Thanks.

Do you want the distinct values in the second column?
select distinct data
from t;

Related

HANA SQL select count and and array for ids

Is there any way to generate a query where I can have 2 fields. First field is get the count and second is to get the names as an array?
Sample Table:
|---------------------|------------------|
| id | name |
|---------------------|------------------|
| 1 | John |
|---------------------|------------------|
| 2 | Doe |
|---------------------|------------------|
And then, I can get results as:
|---------------------|------------------|
| count | array_name |
|---------------------|------------------|
| 2 | ["John", "Doe"] |
|---------------------|------------------|
You can try the below - using STRING_AGG()
SELECT count(id), STRING_AGG(name,',' ORDER BY id)AS array_name
FROM tablename

SQL - LIMITing AND filtering a join at same time

I need a solution for following Problem.
I have two Tables:
ids from new user (got by subquery)
+------------+
| user_id |
+------------+
| 1 |
| 4 |
| 5 |
+------------+
users (table with all users)
+------------+
| user_id |
+------------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| ... |
+------------+
i need to join this two tables. every new user needs exactly 3 connections to other users.
for example:
+----------+------+
| new_user | user |
+----------+------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 4 | 1 |
| 4 | 2 |
| 4 | 3 |
| 5 | 1 |
| 5 | 2 |
| 5 | 3 |
+----------+------+
the problem is to limit the entries to exactly 3 and to exclude redundant entries (like 1|1, 3|3, ...)
In PostgreSQL you can use a lateral query to retrieve a limited number of rows in a subquery.
I don't know the exact structure of your main query or subquery but it should look like:
select t.*, ls.*
from main_table t,
lateral ( -- lateral subquery
select * from secondary_table s
where s.col1 = t.col2 -- filtering condition, if needed
fetch first 3 rows only -- limit to a max of 3 rows
) ls;
The lateral subquery is executed once per every row in the main_table.

Consolidate duplicate rows based on subgrouping

I have a table of temporal values in which there exist repeated values in groupings, but I want to remove all but one for each grouping and maintain the order (can't just say the distinct values).
If the sequence of rows was as such in order
+------+-----+
| time | col |
+------+-----+
| 1 | A |
| 2 | A |
| 3 | A |
| 4 | B |
| 5 | B |
| 6 | B |
| 7 | C |
| 8 | D |
| 9 | E |
| 10 | A |
| 11 | A |
| 12 | B |
+------+-----+
Then it should be resulted as
+-----+
| col |
+-----+
| A |
| B |
| C |
| D |
| E |
| A |
| B |
+-----+
Is there a way to do this without a cursor? How I would do it in not SQL would be to iterate over the list and say if the current index matches the previous index, then pop it.
SQL tables represent unordered sets. The rows you want to remove depend on the ordering, specifically adjacent identical values are being removed.
In order to have an ordering, the data needs a column that specifies it. Let me assume you have one.
If so, this is easily handled with lag():
select col
from (select t.*, lag(col) over (order by orderingcol) as prev_col
from t
) t
where prev_col <> col or prev_col is null;

I've got the following two SQL tables (in MySQL):

I've got the following two SQL tables (in proc SQL):
A
+----+------+--------------+
| id | age |
+----+------+
| 1 | 10 |
+----+------+
| 2 | 20 |
+----+------+
| 3 | 30 |
+----+------+
| 4 | 40 |
+----+------+
B
+----+
| id |
+----+
| 1 |
+----
| 2 |
+----+
| 3 |
+----+
The desired output would be: How do I get this output in proc sql
+----+------+
| id | age |
+----+------+
| 4 | 40 |
+----+------+
Try this
SELECT A.*
FROM A NATURAL LEFT JOIN B
WHERE B.id IS NULL
SQL FIDDLE DEMO
select * from A
where not exists(select * from b
where B.ID=A.Id)

SQL get interection of values across multiple rows grouped by primary key

I have table with data as follows
+----+------+
| id | code |
+----+------+
| 1 | M |
| 1 | Y |
| 2 | M |
| 2 | S |
| 3 | M |
| 3 | Q |
+----+------+
I would like to know if its possible to write a query that would return a list of codes that are unique to each ID? If there is no intersection the query should return no rows.
In the example above the only value common to all is M.
+----+------+
| id | code |
+----+------+
| 1 | M |
| 1 | S |
| 2 | M |
| 2 | S |
| 2 | H |
| 3 | M |
| 3 | S |
| 3 | Q |
+----+------+
The above would return M and S, common to all three ID's
Thanks
Try this:
SELECT code
FROM mytable
GROUP BY code
HAVING COUNT(*) = (SELECT COUNT(DISTINCT id) FROM mytable)
The above query assumes that code can appear only once per id.