SQL HAVING COUNT WITH TWO COLUMNS - sql

I have the following tableA
COL1 COL2 COL3
A 1 10-1-2019
B 4 13-4-2019
A 1 13-4-2019
A 1 10-1-2019
A 1 10-1-2019
C 3 20-4-2020
A 1 13-4-2019
I this is the sql code i wish to write but the count do not accept two elements :
select COL1,COL2,COL3
from TableA
group by COL1,COL2,COL3,
HAVING COUNT(COL1,COL2) > 2
And only the result A 1 10-1-2019 should appear because it is the only one that have the pair COL1 COL3 with more than 2 results(in this case three times). The pair COL1 /COL3 is something like a composed primary key
How can i achieve this ?
My database is Sybase ASE
Thanks in advance.

I think this does what you want:
select COL1, COL2, COL3
from TableA
group by COL1, COL2, COL3,
HAVING COUNT(*) > 2;
Perhaps you have a bad example. But I don't see why you think you need to exclude col2.

Related

remove duplicated set of columns in table

I created co-occurrence table as follows.
col1 col2 count
a b 10
b a 10
c d 7
d c 7
I want to keep co-occurrence rows without duplication like this.
col1 col2 count
a b 10
c d 7
How can I do this?
One simple method is:
select col1, col2, count
from t
where col1 < col2;
If you actually want to change the table, you can do:
delete t from t
where col1 > col2;
This assumes that all pairs of columns are in the database.
When inserting or selecting, do something like this instead of col1, col2:
LEAST(col1, col2), GREATEST(col1, col2)

How to use and "in" clause in "having" in HIVE?

I have my data in sometable like this:
col1 col2 col3
A B 3
A B 1
A B 2
C B 1
And I want to get all of the unique groups of col1 and col2 that contain certain rows of col3. Like, all groups of col1 and col2 that contain a "2".
I wanted to do something like this:
select col1, col2 from sometable
group by col1, col2
having col3=1 and col3=2
But I want it to only return groups that have an instance of both 1 and 2 in col3. so, the result after the query should return this:
col1 col2
A B
How do I express this in HIVE? THANK YOU.
I don't know why others deleted answers that where correct and then almost correct but I will put their's back up.
SELECT col1, col2, COUNT(DISTINCT col3)
FROM
sometable
WHERE
col3 IN (1,2)
GROUP BY col1, col2
HAVING
COUNT(DISTINCT col3) > 1
If you actually want to return all of the records that meet your criteria you need to do a sub select and join back to the main table to get them.
SELECT s.*
FROM
sometable s
INNER JOIN (
SELECT col1, col2, COUNT(DISTINCT col3)
FROM
sometable
WHERE
col3 IN (1,2)
GROUP BY col1, col2
HAVING
COUNT(DISTINCT col3) > 1
) t
ON s.Col1 = t.Col1
AND s.Col2 = t.Col2
AND s.col3 IN (1,2)
The gist of this is narrow/filter your rowset to the rows that you want to test col3 IN (1,2) then count the DISTINCT values of col3 to make sure both 1 and 2 exist and not just 1 & 1 or 2 & 2.
I think below mentioned query will be useful for your question.
select col1,col2
from Abc
group by col1,col2
having count(col1) >1 AND COUNT(COL2)>2

Oracle SQL - Join 2 table columns in 1 row

I have 2 SQL's and the result come fine. They are no relation between those 2 queries but I want to see all the rows in single column.
e.g.
Select col1,col2,sum(col3) as col3 from table a
select col4,col5 from table b
I would like the result to be
col1 col2 col3 col4 col5
If there is no equivalent row for either table a or table b replace with zeroes.
Could some one help me with this. thanks.
Since, you didn't provided any information like table structure or data inside each tables. You can cross join both tables.
select t.col1,t.col2,t.col3,t1.col1,t1.col2 from tab1 t,tab2 t1;
SQLFiddle
In both select statements add column based on rownum or row_number() and then full join results using this column:
select nvl(col1, 0) col1, nvl(col2, 0) col2, nvl(col3, 0) col3,
nvl(col4, 0) col4, nvl(col5, 0) col5
from
(select rownum rn, col1, col2, col3 from (
select col1, col2, sum(col3) col3 from tableA group by col1, col2)) a
full join (select rownum rn, col4, col5 from tableB) b using (rn)
SQLFiddle demo
I guess a UNION could be a pragmatic solution since the 2 queries are not related. They are just 2 data sets that should be retrieved in one statement:
Select col1,col2,sum(col3) as col3 from table a
UNION
select col4,col5, to_number(null) col6 from table b
Be aware of col6 in the example. SQL insists on retrieving an equal set of columns in a UNION statement. It is a good practice to retrieve columns with exactly the same datatype. Since the sum(col3) will yield a number datatype column, col6 should too.
The outcome of col4 and col5 will be shown in col1 and col2.

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

select all columns with one column has different value

In my table,some records have all column values are the same, except one. I need write a query to get those records. what's the best way to do it? the table is like this:
colA colB colC
a b c
a b d
a b e
What's the best way to get all records with all the columns? Thanks for everyone's help.
Assuming you know that column3 will always be different, to get the rows that have more than one value:
SELECT Col1, Col2
FROM Table t
GROUP BY Col1, Col2
HAVING COUNT(distinct col3) > 1
If you need all the values in the three columns, then you can join this back to the original table:
SELECT t.*
FROM table t join
(SELECT Col1, Col2
FROM Table t
GROUP BY Col1, Col2
HAVING COUNT(distinct col3) > 1
) cols
on t.col1 = cols.col1 and t.col2 = cols.col2
Just select those rows that have the different values:
SELECT col1, col2
FROM myTable
WHERE colWanted != knownValue
If this is not what you are looking for, please post examples of the data in the table and the wanted output.
How about something like
SELECT Col1, Col2
FROM Table
GROUP BY Col1, Col2
HAVING COUNT(*) = 1
This will give you Col1, Col2 that have unique data.
Assuming col3 has the difs
SELECT Col1, Col2
FROM Table
GROUP BY Col1, Col2
HAVING COUNT(*) > 1
OR TO SHOW ALL 3 COLS
SELECT Col1, Col2, Col3
FROM Table1
GROUP BY Col1, Col2, Col3
HAVING COUNT(Col3) > 1