select distinct value in sql with 35 columns - sql

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

Related

Select rows with same id but different value in other column

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

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.

Query two tables in minimum time

I have two tables with same kind of data. what is the fastest SQL query that looks in both tables and obtains the data from the two. no duplicates. access 2003 or above.sample code..
SELECT Field1, Field2, Field3
FROM Table1
UNION
SELECT Field1, Field2, Field3
FROM Table2
UNION removes duplicate records (where all columns in the results are the same).

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

Refer to other SQL SELECT statements aliased as tables in FROM clause

I have a very large query that follows the format below:
select ...
from ( select field1,
field2
from some_table ) table1,
( select field1,
field3
from other_table ) table2
where .....
Is is possible for me to refer to one of the tables "defined" in the from clause, lets say table1, in one of the other table definitions in the from clause?
For example:
select ....
from ( select field1,
field2
from some_table ) table1,
( select table1.field1,
field3
from other_table,
table1 ) table2
where .....
Disclaimer: What I am trying to do is not as simple as the example above. The example is simply to illustrate the idea.
WITH
table1 AS
(
SELECT field1, field2
FROM some_table
),
table2 AS
(
SELECT field1, field2
FROM other_table, table1
)
SELECT *
FROM table2
If you are using SQL 2005, you can use Common Table Expressions for doing what you are trying; Quassnoi gives us an example, in Oracle I don't know how to achieve it though