Select rows with same id but different value in other column - sql

Sample data:
I want to get row 1, 5, and 10. I hope you could help me, Thanks.

If you want to have distinct data based on different columnn data, you should use GROUP BY query.
If you have table called tbl which has column field1, field2 and field3 and you want to get all rows which has distinct data for all field in 'tbl', then your query should be like this :
SELECT field1, field2, field3
FROM tbl
GROUP BY field1, field2, field3

Related

How to use LIKE statement with multiple values from another field?

I want to select all records where field contains values from another field. How do I do that? Here is a code that I am trying.
select field1 , field2, field3
from table1
where field1 like '%'+(select distinct field4 from table2)+'%'
Thanks in advance.
Just do your like as a join condition:
select field1 , field2, field3
from table1
join (select distinct field4 from table2) x
on field1 like '%'+field4+'%'
Using the original structure of your query, you can do:
select field1, field2, field3
from table1 t1
where exists (select 1
from table2
where t1.field1 like '%' + field4 + '%'
);
The advantage of this method is that it will not duplicate records in table1. For instance, if there are two rows in table2 with the values 'a' and 'b' respectively and one row in table1 with the value 'ab', then this method will only return the row from table1 once.

select distinct value in sql with 35 columns

I have table that contains 35 columns, how would I select only the distinct records from that table, this is what my query looks like:
`SELECT field1, field2, field3 etc... from table1 group by field1, field2, field3 etc...`
This gets me the unique results that I want but I have 35 columns, its too long to group all 35 rows - is there any efficient way of doing this:
by doing this, I get repeated results:
SELECT distinct * from table1
DISTINCT will be also faster:
Your query should looks like:
SELECT distinct field1, field2, field3 etc...
from table1
Use distinct once.
This will affect all columns.
You don't need GROUP BY if you use DISTINCT. But you have to list all the fields that you want to show, and for sure not the primary key.
SELECT DISTINCT field1, field2, field3 etc...
FROM table1

SQL Having Clause with multiple selected fields

I have a SQL query such as the following:
SELECT field1, field2, field3, field4, field5
FROM tablename
WHERE field1 = condition
GROUP BY 1,2,3,4,5
HAVING COUNT(field1) > 2
I expected the query to return only the results which have more than 2 rows in the resultset, however the query returns 0 zero.
Could anyone point out what I'm doing wrong? I need to keep my query selecting the fields it has been, but limit the results coming back to only those who have at least 2 rows. If they only have 1, I don't want them included in my results.
The where clause specifies that field1 has to be equal to condition.
count(field1) would essentially always be 1 (distinct values of field1 would be 1 and equal to condition).
That's why we always have 0 results since the count is never > 2.
I suspect (although from my comment under the question I am not sure) that you want
SELECT field1, field2, field3, field4, field5
FROM tablename
WHERE field1 = condition
AND field1 IN
(SELECT field1
FROM tablename
GROUP BY 1
HAVING COUNT(field1) >=2 );

add a temporary column with a value

I have a select statement like this
select field1, field2
from table1
What I want is to have a newfield with only value "example".
newfield does not exist in the table, but when I run this query it kinda makes a virtual column with the values of example field.
select field1, field2, 'example' as TempField
from table1
This should work across different SQL implementations.
You mean staticly define a value, like this:
SELECT field1,
field2,
'example' AS newfield
FROM TABLE1
This will add a column called "newfield" to the output, and its value will always be "example".
I'm rusty on SQL but I think you could use select as to make your own temporary query columns.
select field1, field2, 'example' as newfield from table1
That would only exist in your query results, of course. You're not actually modifying the table.
In this example, the TABLE registrofaena doesn't have the column called minutos. The query returns a column "minutos" with demora/60 as the content (the values represent the delay in minutes). The table is not modified in the process.
This is the query:
SELECT idfaena,fechahora,demora, demora/60 as minutos,comentario
FROM registrofaena
WHERE fecha>='2018-10-17' AND comentario <> ''
ORDER BY idfaena ASC;
This is the view:
select field1, field2, NewField = 'example' from table1
select field1, field2, '' as newfield from table1
This Will Do you Job

Generate field in MySQL SELECT

If I've got a table containing Field1 and Field2 can I generate a new field in the select statement? For example, a normal query would be:
SELECT Field1, Field2 FROM Table
And I want to also create Field3 and have that returned in the resultset... something along the lines of this would be ideal:
SELECT Field1, Field2, Field3 = 'Value' FROM Table
Is this possible at all?
SELECT Field1, Field2, 'Value' Field3 FROM Table
or for clarity
SELECT Field1, Field2, 'Value' AS Field3 FROM Table
Yes - it's very possible, in fact you almost had it!
Try:
SELECT Field1, Field2, 'Value' AS `Field3` FROM Table