I use this query:
SELECT POPOLAZ.REG, POPOLAZ.REGIONE, Sum(POPOLAZ.TOT), Count(POPOLAZ.ISTAT), Count(POPOLAZ.PR)
FROM POPOLAZ
WHERE POPOLAZ.REG='01'
GROUP BY POPOLAZ.REG, POPOLAZ.REGIONE
this code work.
but filed PR have a duplicates value, i need a distinct
SQL with duplicates
-
- PlatinumLounger
- Posts: 4377
- Joined: 26 Apr 2010, 17:36
SQL with duplicates
You do not have the required permissions to view the files attached to this post.
Last edited by sal21 on 02 May 2024, 11:54, edited 1 time in total.
-
- Administrator
- Posts: 78671
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4377
- Joined: 26 Apr 2010, 17:36
Re: SQL with duplicates
ISTAT are only unique value.
-
- Administrator
- Posts: 78671
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SQL with duplicates
Please attach a small sample database (it doesn't have to be complete, but it should contain a few regions and provinces)
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4377
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78671
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SQL with duplicates
See Query3.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4377
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78671
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4377
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78671
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SQL with duplicates
Dim SQL As String
SQL = "SELECT Query2.REG, Query2.REGIONE, Query2.SummaDiTOT, Query2.ContaDiISTAT, Query1.ContaDiPR FROM (SELECT REG, Count(PR) AS ContaDiPR FROM (SELECT REG, PR FROM POPOLAZ GROUP BY REG, PR) AS Q GROUP BY REG) AS Query1 INNER JOIN (SELECT REG, REGIONE, Sum(TOT) AS SummaDiTOT, Count(ISTAT) AS ContaDiISTAT FROM POPOLAZ GROUP BY REG, REGIONE) AS Query2 ON Query1.REG = Query2.REG"
SQL = "SELECT Query2.REG, Query2.REGIONE, Query2.SummaDiTOT, Query2.ContaDiISTAT, Query1.ContaDiPR FROM (SELECT REG, Count(PR) AS ContaDiPR FROM (SELECT REG, PR FROM POPOLAZ GROUP BY REG, PR) AS Q GROUP BY REG) AS Query1 INNER JOIN (SELECT REG, REGIONE, Sum(TOT) AS SummaDiTOT, Count(ISTAT) AS ContaDiISTAT FROM POPOLAZ GROUP BY REG, REGIONE) AS Query2 ON Query1.REG = Query2.REG"
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4377
- Joined: 26 Apr 2010, 17:36
Re: SQL with duplicates
OPS...HansV wrote: ↑02 May 2024, 14:16Dim SQL As String
SQL = "SELECT Query2.REG, Query2.REGIONE, Query2.SummaDiTOT, Query2.ContaDiISTAT, Query1.ContaDiPR FROM (SELECT REG, Count(PR) AS ContaDiPR FROM (SELECT REG, PR FROM POPOLAZ GROUP BY REG, PR) AS Q GROUP BY REG) AS Query1 INNER JOIN (SELECT REG, REGIONE, Sum(TOT) AS SummaDiTOT, Count(ISTAT) AS ContaDiISTAT FROM POPOLAZ GROUP BY REG, REGIONE) AS Query2 ON Query1.REG = Query2.REG"
I need also a where ANNO=2023
-
- Administrator
- Posts: 78671
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SQL with duplicates
In your sample database, that returns 0 records because all records have ANNO = 2022. But it would be:
Dim SQL As String
SQL = "SELECT Query2.REG, Query2.REGIONE, Query2.SummaDiTOT, Query2.ContaDiISTAT, Query1.ContaDiPR FROM (SELECT REG, Count(PR) AS ContaDiPR FROM (SELECT REG, PR FROM POPOLAZ WHERE ANNO = 2023 GROUP BY REG, PR) AS Q GROUP BY REG) AS Query1 INNER JOIN (SELECT REG, REGIONE, Sum(TOT) AS SummaDiTOT, Count(ISTAT) AS ContaDiISTAT FROM POPOLAZ WHERE ANNO = 2023 GROUP BY REG, REGIONE) AS Query2 ON Query1.REG = Query2.REG"
Dim SQL As String
SQL = "SELECT Query2.REG, Query2.REGIONE, Query2.SummaDiTOT, Query2.ContaDiISTAT, Query1.ContaDiPR FROM (SELECT REG, Count(PR) AS ContaDiPR FROM (SELECT REG, PR FROM POPOLAZ WHERE ANNO = 2023 GROUP BY REG, PR) AS Q GROUP BY REG) AS Query1 INNER JOIN (SELECT REG, REGIONE, Sum(TOT) AS SummaDiTOT, Count(ISTAT) AS ContaDiISTAT FROM POPOLAZ WHERE ANNO = 2023 GROUP BY REG, REGIONE) AS Query2 ON Query1.REG = Query2.REG"
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4377
- Joined: 26 Apr 2010, 17:36
Re: SQL with duplicates
Ifact ANNO not Is in demo database.HansV wrote: ↑06 May 2024, 09:20In your sample database, that returns 0 records because all records have ANNO = 2022. But it would be:
Dim SQL As String
SQL = "SELECT Query2.REG, Query2.REGIONE, Query2.SummaDiTOT, Query2.ContaDiISTAT, Query1.ContaDiPR FROM (SELECT REG, Count(PR) AS ContaDiPR FROM (SELECT REG, PR FROM POPOLAZ WHERE ANNO = 2023 GROUP BY REG, PR) AS Q GROUP BY REG) AS Query1 INNER JOIN (SELECT REG, REGIONE, Sum(TOT) AS SummaDiTOT, Count(ISTAT) AS ContaDiISTAT FROM POPOLAZ WHERE ANNO = 2023 GROUP BY REG, REGIONE) AS Query2 ON Query1.REG = Query2.REG"
Modified in original DB and work perfect!
Tks bro