MYSQL - Using AVG() and DISTINCT together - sql

How can you write the following in MYSQL?
SELECT AVG(col1) FROM table WHERE DISTINCT col2
more info:
table
col1 | col2
-----------
2 | 555.555.555.555
5 | 555.555.555.555
4 | 444.444.444.444
returns '3'
Basically I'm trying to select average value of col1 where ip addresses in col2 are distinct.

SELECT col2,
AVG(col1)
FROM table
GROUP BY col2

Right, because the distinct clause would find the first and third rows, the average of 2 and 4 is 3.
What I think you're looking for is "group by col2" instead of distinct.

I think you want the group by operator. It will group the rows before running calculations on them.

Related

How to use group-by and get other rows results

Question: if this is my data:
col1,col2,col3,col4
===================
www.com,0,dangerous,reason A
www.com,1,dangerous 2,reason B
I want the a single result where column 2 value is max, so I will use in my select the Max(col2) function - but how can I get those corresponding col3 and col4 row ?
select
col1, max(col2), col3, col4
group by
col1
and ???
Thanks
Idan
You can use order by and limit to one row. The ANSI-standard syntax is:
select t.*
from t
order by t.col2 desc
fetch first 1 row only;
Not all databases support the fetch first clause, so you might have to use select top 1, limit, or some other construct.
You can use where in select statement
Like
Select * from table name where col2=max(col2)
You can get max column entire row with single value
If the column col2 which contain same value like 1,1,2,2 at this time above query return the 2 rows. At that time if you want single row you want to use this
Select * from table name where col2=max(col2) fetch first 1 row only
Might be this helpful

Exclude rows that have same value in two different columns

I have a table which has 2 columns that sometimes have the same values. I want to know how to exclude the rows where the value of column1 is equal to a value in column2.
EXAMPLE:
COL1 | COL2
1 -------- 7
2 -------- 8
3 -------- 2
4 -------- 5
5 -------- 9
Here I would exclude rows 2 and 5.
Thanks
select
*
from table
where col1 not in (
select
column2
from table
)
Something like this should work :
SELECT *
FROM yourtable
WHERE COL1 NOT IN (SELECT COL2
FROM yourtable)
I tend to avoid using IN for long lists of values, as it performs poorly on some database systems. The following selects all values from col1 that are not present in col2:
SELECT col1
FROM
yourtable t1
LEFT JOIN
yourtable t2
ON
t1.col1 = t2.col2
WHERE
t2.col2 IS NULL
Why does it work? Well, normally the join operator will link together rows that have the same value. Left join will keep some rows that are mismatched though (and it's those we want). The left join takes the table on the left (t1) and uses it as the reference table, and starts associating rows from the table on the right (after the word JOIN, in this case t2). If the col1 value has a matching value in col2 then the row will be fully populated with values for each. If the value from col1 has no matching value from col2, the col2 cell on the resulting row is blank/null. Because we hence want to know only those values that aren't matched, we say "where col2 is null"
The other trick with getting to grips with this is in understanding that the same table can appear twice in a query. We give it a different alias each time we use it so we can tell them apart. You could conceive it as virtually making a copy of the table, before it links them together
Use EXCEPT together with a correlated sub-query - as shown below.
Read up on EXCEPT here: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql
SELECT *
FROM TEST
EXCEPT
SELECT *
FROM TEST
WHERE COL1 IN (
SELECT COL2
FROM TEST
)
not sure, but maybe...
SELECT t1.*
FROM my_table AS t1
LEFT JOIN my_table AS t2
ON t2.col_b = t1.col_a
WHERE t2.col_b IS NULL

DB2, SQL query to SUM 2 columns

I need to add to columns in a row.
Table Data
id
Col1
Col2
1
10
20
2
11
20
3
12
20
Result expected
id
Sum
1
30
2
31
3
32
I tried sum(col1 + col2), but that gives the sum of all the columns together.
sum() is a aggregating function (one that give a single result for a group of rows), not a algebraic one: You want the addition (the mathematical sum) of the two columns:
select id, col1 + col2 as sum
from mytable
we have two type of columns in group clause (Aggregation column and Group column) in this query
select id, col1 + col2 as sum
from mytable
group by id
we have to insert id, col1 and col2 in front of group by, otherwise we get this error
Column 'TEST.COL1' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
if use MAX() aggregation function like this
SELECT
ID,
MAX(COL1+COL2) AS SUM
FROM TEST
GROUP BY ID
we get the result BUT this isn't good idea because the cost of this code 4 times more than
bellow code
SELECT
ID,COL1+COL2 AS SUM
FROM TEST
Try this
select id, col1 + col2 as sum
from mytable
group by id

Create SQL summary using union

I currently have some SQL that is used to create an excel report in the following format:
COL1 COL2 COL3
2 1 8
3 7 9
1 2 4
Now what I am trying to do is sum up the total of these each value and insert it at the bottom using UNION ALL (unless of course there is a better way.)
Now the values for each column are generated already by sums. The concept I can't grasp is how to sum all the values for the final row, if this is even possible.
So the output should look like so:
COL1 COL2 COL3
2 1 8
3 7 9
1 2 4
6 10 21
Thanks!
It looks like you want to add
WITH ROLLUP
to the end of your query
eg:
Select sum(a) as col1, sum(b) as col2
from yourtable
group by something
with rollup
Depending on the full nature of your query, you may prefer to use with cube, which is similar. See http://technet.microsoft.com/en-us/library/ms189305(v=sql.90).aspx
select
col1
,col2
,col3
from tableA
union
select
sum(col1)
,sum(col2)
,sum(col3)
from tableA
order by col1,col2,col3
SELECT COL1, COL2, COL3
FROM SomeTable
UNION ALL
SELECT SUM(COL1), SUM(COL2), SUM(COL3)
FROM SomeTable
note. there is also a ROLLUP clause but I think the above would be a simpler solution in this case
http://technet.microsoft.com/en-us/library/ms189305%28v=sql.90%29.aspx

How to distinct on second column from the same table?

I have a problem, where first field in unique/distinct and the second field is like concatenated string. i am looking for the distinct rows to be displayed in the second concatenated column not on the first column and i need in the same seq of columns, for ex:
1stcolumn 2ndcolumn(concatenated)
100 ABC-123-PQR
101 ABC-123-PQR
102 ABC-123-PQR
104 ABC-123-STU
in the above example i need to select only ABC-123-PQR AND ABC-123-STU, i don't care on the first column values.
Why not just use the DISTINCT keyword?
SELECT DISTINCT col2 FROM mytable
SELECT MAX(col1), col2 FROM mytable GROUP BY col2