SQL Having Clause with multiple selected fields - sql

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 );

Related

Select unique field1 that has >1 distinct instances of field2 associated with it?

How can I query for the distinct field1 instances that have multiple distinct corresponding field2 values?
field1
field2
a
apple
b
grape
c
banana
b
orange
a
apple
In this example I want to return "b", since there are at least 2 distinct values (grape and orange) for field2 that correspond to it. I don't wan't "a" since there is only 1 unique field2 value that corresponds, "apple".
I have tried
with all_unique_combos as (
select distinct field1, field2
from table
)
select field1
from all_unique_combos
group by field1
having count(field2) > 1
I actually think this is right and would give me what I need. But at the moment it's returning 0 rows so I kinda need a sanity check. Thanks for any input either way.
You can use aggregation:
select field1
from t
group by field1
having min(field2) <> max(field2);
A straight-forward approach uses group by and having:
select field1
from mytable
group by field1
having min(field2) <> max(field2)
Using COUNT(DISTINCT ...):
select field1
from tab
group by field1
having count(disitnct field2) > 1

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

Process TOP(n) records with an SQL CTE in an SQL WHILE Loop

First timer here, so if I am doing something wrong, please do not hesitate in telling me.
I have a situation where I know what to do, but I do not know how to do it. Situation is as follows:
Take records from one table - now each record has to be split into two parts into a new table. This means I take 1 record and it ends up being 2 in another table. For this purpose I have made a CTE with SQL. This works perfectly. It splits the record into two parts. This is what the CTE looks like :
WITH
cteFirstLine (Field1, Field2, Field3) AS
(
SELECT TOP 1000 T.Field1, T.Field2, T.Field3
FROM Table1 T
LEFT JOIN Table2 P ON T.Field1 = P.Field4
ORDER BY Field2 DESC
)
,
cteSecondLine(Field1, Field2, Field3) AS
(
SELECT TOP 1000 T.Field1, T.Field2, T.Field3
FROM Table1 T
LEFT JOIN Table2 P ON T.Field1 = P.Field4
ORDER BY Field2 DESC
)
,
cteUnified (Field1, Field2, Field3)
AS
(
SELECT Field1, Field2, Field3
FROM cteFirstLine
UNION
SELECT Field1, Field2, Field3
FROM cteSecondLine
)
INSERT INTO Table_TEST(Field1, Field2, Field3)
SELECT TOP 2000 Field1, Field2, Field3
FROM cteUnified
OK, so this works. The problem I now have is that The old table, the table from which I have to process these records contains almost a million records. I know it is a lot, but it used to be way more than that, hence this task.
I need to know: how can I use this CTE inside a loop? Will a loop suffice? How would I consturct the loop so that it can do everything automatically. By Automatically I mean instead of doing only 1000 records at a time, but more.
Can anyone please help?

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:how to find duplicated combinations in two adjacent columns

I have a table (field1, field 2, field3, field4),
How can I sift out only those rows which contain duplicated combinations in two adjacent columns - field3 and field4? i.e ->
Try this:
select *
from mytable t
join (
select field3, field4, count(*) from (
select field3, field4 from mytable where field3 <= field4
union all
select field4, field3 from mytable where field3 > field4) x
group by field3, field4
having count(*) > 1) y
on (t.field3 = y.field3 and t.field4 = y.field4)
or (t.field3 = y.field4 and t.field4 = y.field3)
The union all inner query lines up all the values, without removing duplicates (as union does) into consistent columns - the where clauses ensure that rows aren't selected twice.
The inner query is then grouped with a having clause to pick the duplicates.
The outer query joins to these both ways to get all the rows.