I have a SQL table that looks like this:
Name | Attributes
-----------------
Toto | Attr1
Toto | Attr2
Titi | Attr1
and I would like a SQL request to merge the rows with attributes "Attr1" AND "Attr2" to have this table:
Name
----
Toto
How can I do this? Thank you.
If you want names that have both attributes, you can use group by:
select name
from t
where attributes in ('Attr1', 'Att2')
group by name
having count(distinct attributes) = 2;
Related
Have a table with some data with different variants of names.
Select variants of data from table and group by its own data in Postgres. Select the only distinct name and select a common name.
SELECT name FROM table;
Output
name
-----------------------
name 1<br>
2 name of the student
user 1
user 1 of CA 2
something ele
The result what I want
Result
name | count
------------------------
name | 2
user 1 | 2
something else | 1
I use OrientDB.
I have a table like this:
NAME | CATEGORIES
-------------------
N1 | [A,B]
N2 | [C]
N3 | [C,A]
N4 | [A,B]
And I would like to build a query that returns a list of categories, and for each category a list of related names, like this:
CATEGORY | NAMES
-----------------------
A | [N1,N3,N4]
B | [N1,N4]
C | [N2,N3]
If "categories" wasn't an array, I could achieve it with:
SELECT
Categories as Category,
set(Name) as Names
FROM Table
GROUP BY Categories
But, being Categories arrays, this is what I get:
CATEGORY | NAMES
--------------------
[A,B] | [N1,N4]
[C] | [N2]
[C,A] | [N3]
What query should I rather write?
I reproduced your structure with this command
create class test extends v
create property test.name string
create property test.categories embeddelist string
insert into test(name,categories) values ("N1",["A","B"]),("N2",["C"]),("N3",["C","A"]),("N4",["A","B"])
and I used this query
select categories,$a.name as name from (select distinct(categories) as categories from (select categories from test order by categories unwind categories))
let $a = (select name from test where categories contains $parent.$current.categories)
Hope it helps.
I have a table with 2 columns - id and pets.
Pets column contain abbreviated pet names separated by , [comma] as shown below
+----+-------------+
| id | pets |
+----+-------------+
| 1 | CAT,DOG |
+----+-------------+
| 2 | CAT,DOG,TIG |
+----+-------------+
| 3 | ZEB,MOU |
+----+-------------+
Now I want to list all id's where pets = CAT, similarly all id's where pets = DOG etc
My initial try was to RUN the following SQL for each and every PET (CAT, DOG, etc)
select id
from "favpets"
where pets like '%CAT%'
The limitation of this simple solution is that the actual table and no. of pets are not as simple as mentioned above.
No. of such pets are more than 200. Therefore, 200 sql's have to be executed in order to list all id's corresponding to the pets
Is there any good alternative solution ? I'm using doctrine, so does doctrine provide any good implementation ?
With this query you will obtain all id, all pets, ordered by pet:
SELECT id, unnest(string_to_array(pets, ',')) AS mypet
FROM favpets
ORDER BY mypet;
Using it as subquery it will became easy to group and count:
SELECT mypet, COUNT(*) FROM
(
SELECT id, unnest(string_to_array(pets, ',')) AS mypet
FROM favpets
ORDER BY mypet
) AS a
GROUP BY mypet;
How to retrieve real column name of multiple time used alias column in a view?
My table is
----------------
|colname |TYPE |
----------------
|Column1 |int |
|Column2 |nchar|
|Column3 |nchar|
----------------
And this is my view:
CREATE VIEW myView
AS
SELECT
Column1 as UserID,
Column1 as customerID,
Column2 as name ,
Column3 as gender ,
FROM
myTable
GO
How to get my view original name and datatype and column name using view ?
Expected output like
---------------------------
|REALNAME|TYPE |ColName |
---------------------------
|Column1 |nchar|UserID |
|Column1 |nchar|customerID|
|Column2 |nchar|name |
|Column3 |nchar|gender |
---------------------------
Thanks in advance.
I am trying to do this is SQL:
-------------------------------
Table A
-------------------------------
ID | Title
1 | Something Groovy
2 | Something Else
-------------------------------
Table B
-------------------------------
ID | Title
1 | Something Different
2 | Something More
Awesome Select Statement
-------------------------------
Both in one field
-------------------------------
Title
Something Groovy
Something Else
Something Different
Something More
Any Ideas?
Use UNION ALL to obtain the union of two selects. If you want to eliminate duplicates, use simply UNION.
SELECT Title FROM TableA
UNION ALL
SELECT Title FROM TableB
This is a simple union:
Select title from TableA
UNION
Select title from TableB
Note that any items that are identical will be eliminated.