Join Fragment CTE to construct our proper table - sql

This is my query
with cte as (
select mail
from dbo.reseau
where mail in
( select mail from dbo.reseau
group by mail
having count(1) > 1
) )
select * from (
select trig
from dbo.reseau
where trig in
( select trig from dbo.reseau
group by trig
having count(1) > 1
))t
select * from
(
select nom from reseau
where nom in (
( select nom from dbo.reseau
group by nom
having count(1) > 1
) ) )c
select * from (
select prenom from reseau
where prenom in (
( select prenom from dbo.reseau
group by prenom
having count(1) > 1)))r
I have 4 columns and I wrote this CTE to show duplicate data. My query shows them separately. I want to join them. How can I do this?

If you want rows where any of the columns contain duplicate values, you can use window functions multiple times:
select *
from (
select
r.*,
count(*) over(partition by mail) cnt_mail,
count(*) over(partition by trig) cnt_trig,
count(*) over(partition by nom) cnt_nom,
count(*) over(partition by prenom) cnt_prenom
from reseau
) t
where cnt_mail > 1 or cnt_trig > 1 and cnt_nom > 1 end cnt_prenom > 1
The where clause can be simplified as:
where cnt_email + cnt_trig + cnt_nom + cnt_prenom > 4

Related

How do I use MIN on a union column SQL

I'm having problems with using the MIN function in sql. I want to get a list of all the rows with the minimum value from my count function.
Here is my code:
SELECT land, MIN(count) as lowest
FROM
(
SELECT temp.land, count(*)
FROM
(
SELECT grans.land FROM Grans
UNION ALL
SELECT grans.aland FROM Grans
) as temp
GROUP BY land
ORDER BY land
) as subQuery
GROUP BY land
ORDER BY land
At the moment I just get a table listing land and count, although count is renamed to lowest.
I would use window functions:
SELECT land, cnt
FROM (SELECT temp.land, count(*) as cnt,
MIN(count(*)) OVER () as min_cnt
FROM (SELECT grans.land FROM Grans
UNION ALL
SELECT grans.aland FROM Grans
) temp
GROUP BY land
) l
WHERE cnt = min_cnt;
remove group by if you just want min because if you put group by it will return all the land count that you got in your sub-query, as in count it already made group and that is distinct
SELECT *
FROM
(
SELECT temp.land, count(*) as cnt
FROM
(
SELECT grans.land FROM Grans
UNION ALL
SELECT grans.aland FROM Grans
) as temp
GROUP BY land
ORDER BY land
) as subQuery
order by cnt asc
Limit 1
another way is
SELECT temp.land, count(*) as cnt
FROM
(
SELECT grans.land FROM Grans
UNION ALL
SELECT grans.aland FROM Grans
) as temp
GROUP BY land
having cnt in(
SELECT min(cnt)
FROM
(
SELECT temp.land, count(*) as cnt
FROM
(
SELECT grans.land FROM Grans
UNION ALL
SELECT grans.aland FROM Grans
) as temp
GROUP BY land
ORDER BY land
) as subQuery
)
and it also work
select * from
(
SELECT * ,row_number() over(partition by land order by cnt) as rn
FROM
(
SELECT temp.land, count(*) as cnt
FROM
(
SELECT grans.land FROM Grans
UNION ALL
SELECT grans.aland FROM Grans
) as temp
GROUP BY land
ORDER BY land
) as subQuery
) t where t.rn=1

How to call a sql query and pass a parameter from another table?

I have a complex sql query, named qryARAT2B_EXT.
SELECT
*
FROM
(
SELECT
*
FROM
(
SELECT
*,
firstStudy,
ABS(DATEDIFF('d', firstStudy, Check_Date)) as diff
FROM
(
SELECT
*,
(
SELECT
TOP 1 Check_Date
FROM
qryARAT2B
WHERE
PATNR = [PАРАМ]
ORDER BY
Check_Date
)
AS firstStudy
FROM
(
SELECT
*
FROM
qryARAT2B
WHERE
PATNR = [PАРАМ]
)
AS myPatientsWithStudy
)
AS myPatientsFirstStudy
)
WHERE
diff = 0
)
AS T1
LEFT JOIN
(
SELECT
*
FROM
(
SELECT
*,
firstStudy,
ABS(DATEDIFF('d', firstStudy, Check_Date)) as diff
FROM
(
SELECT
*,
(
SELECT
TOP 1 Check_Date
FROM
qryARAT2B
WHERE
PATNR = [PАРАМ]
ORDER BY
Check_Date
)
AS firstStudy
FROM
(
SELECT
*
FROM
qryARAT2B
WHERE
PATNR = [PАРАМ]
)
AS myPatientsWithStudy
)
AS myPatientsFirstStudy
)
WHERE
diff = 4
)
As T2
ON T1.PATNR = T2.PATNR
When I open it in ms-access, it asks for the value of the [PARAM] and produces the result.
I have a table of patients.
tblPatient with the columns:
PATNR, and s.o.
That contains the PATNR's of patients:
000001
000002
...
XXXXXX
I need to write sql to calculate data for all PATNR's at once.
something like this:
SELECT (SELECT * FROM qryARAT2B_EXT WHERE [PARAM] = PATNR) from tblPatient
But it is not accepted from ms-access. I'm not able to pass parameter to qryARAT2B_EXT from the SQL. Is there any specific syntax for it in ms-access?

Group by and Count to select repeated rows

I wrote this query but it does not work as I expected.
1st Goal: select rows that have repeated in certain columns and return whole columns.
2nd Goal: Update a flag (a column) to identify which records have repeated.
Could you please help me?
SELECT
*
FROM AvvalV2NS AS M
WHERE EXISTS
(SELECT
M.Astate,
M.Acity,
M.Azone,
M.Abvillage,
M.Avillage,
COUNT(*)
FROM AvvalV2NS AS M
GROUP BY M.Astate,
M.Acity,
M.Azone,
M.Abvillage,
M.Avillage
HAVING COUNT(*) > 1)
If you want to get the rows that are duplicated, window functions are probably the easiest way:
select a.*
from (select a.*,
count(*) over (partition by M.Astate, M.Acity, M.Azone, M.Abvillage, M.Avillage) as cnt
from AvvalV2NS a
) a
where cnt > 1;
You can update a flag by doing something like this:
with toupdate as (
select a.*
from (select a.*,
count(*) over (partition by M.Astate, M.Acity, M.Azone, M.Abvillage, M.Avillage) as cnt
from AvvalV2NS a
) a
)
update toupdate
set isduplicate = (case when cnt > 1 then 1 else 0 end);
Suppose your table have an id column:
SELECT * FROM THE_TABLE WHERE ID IN (
SELECT ID FROM
(SELECT ID, REPEATING_COLUMNS, COUNT(*) FROM THE_TABLE GROUP BY REPEATING_COLUMNS HAVING COUNT(*) > 1)
)
UPDATE THE_TABLE SET THE_FLAG = "HERE WE GO" WHERE ID IN (
SELECT ID FROM
(SELECT ID, REPEATING_COLUMNS, COUNT(*) FROM THE_TABLE GROUP BY REPEATING_COLUMNS HAVING COUNT(*) > 1)
)
Hope this helps.

Finding duplicates with two similar columns and one distinct

I am in a situation where I need to select rows that have the same content in two specific columns, AND distinct content in a third one. So far I got this for the two similar columns:
SELECT id, Title,
COUNT(*) AS NumOccurrences
FROM Table
GROUP BY id, Title
HAVING ( COUNT(*) > 1 )
I now need to specify a third distinct column in this query. Let's call it Ralph. This obviously does not work:
SELECT id, Title, DISTINCT Ralph,
COUNT(*) AS NumOccurrences
FROM Table
GROUP BY id, Title
HAVING ( COUNT(*) > 1 )
So what will?
select * from (
SELECT id, Title, COUNT(*) AS NumOccurrences
FROM Table t
GROUP BY id, Title
HAVING ( COUNT(*) > 1 )
) t
cross apply (
select distinct Ralph
from Table
where id = t.id and Title = t.Title
) t2
You can use COUNT(*) with OVER() clause
;WITH cte AS
(
SELECT id, Title, Ralph, COUNT(*) OVER (PARTITION BY id, Title) AS cnt
FROM dbo.test11
GROUP BY id, Title, Ralph
)
SELECT *
FROM cte
WHERE cnt > 1
Demo on SQLFiddle

Add results from several COUNT queries

I am trying to fetch the sum of several counts in one query:
SELECT(
SELECT COUNT( * )
FROM comments +
SELECT COUNT( * )
FROM tags +
SELECT COUNT( * )
FROM search
)
I am missing something here. I get syntax error.
SELECT ( SELECT COUNT(*) FROM comments )
+ ( SELECT COUNT(*) FROM tags )
+ ( SELECT COUNT(*) FROM search )
One more (not sure if supported with MySQL, though - works in SQL Server):
SELECT SUM(Counts) FROM
(SELECT COUNT(*) AS Counts FROM COMMENTS UNION ALL
SELECT COUNT(*) FROM Tags UNION ALL
SELECT COUNT(*) FROM Search) s
SELECT (
SELECT COUNT(*)
FROM comments
) +
(
SELECT COUNT(*)
FROM tags
) +
(
SELECT COUNT(*)
FROM search
)
SELECT SUM(ThisCount)
FROM (
SELECT COUNT(*) AS ThisCount
FROM comments
UNION ALL
SELECT COUNT(*) AS ThisCount
FROM tags
UNION ALL
SELECT COUNT(*) AS ThisCount
FROM search
)