stuttsdc
stuttsdc

Reputation: 111

SQL Statement to Remove Duplicates and get Counts

My continuing SQL headache...

If I run this paraphrased code:

SELECT State, Name, ID
FROM ...
WHERE Lost=False

I will get something like the following back:

State        Name        ID
NY            A          123
NY            A          123
NY            B          234
NY            C          345
MD            X          356
MD            Y          668

What I really need is a sum of the number of resources in each state. The problem here is that If I just do something like the following, it won't take into account that there are clearly duplicates (see the first two records above).

 SELECT state, Count(state) AS statecount
    FROM ....
   GROUP BY state, Lost
   HAVING Lost=false

How can I get a count of the # of records for each state where lost = false while only pulling distinct records? I thought maybe putting DISTINCT in the first query and then running a Query of a Query in ColdFusion but you can't do things like aggregates in subqueries.

Basically, using the above as an example, I'd ultimately want this:

            State      Count
              NY          3 (not 4!)
              MD          2

I should note... this is in Access

Upvotes: 1

Views: 5626

Answers (5)

xingjl
xingjl

Reputation: 1

This is a language named "SPL" can do this with one row code:

file("d:/table.xlsx").xlsimport@tx().group(State;~.icount(ID):count)

Showing in below attachment :

https://i.sstatic.net/Eola3.png

Upvotes: 0

user2427630
user2427630

Reputation:

First Add an Identity column in your table by using

alter table ATTENDANCE add AUTOID INT IDENTITY

Then run below query

Select State,count(*) as cnt
FROM TableName
WHERE AUTOID IN (SELECT MAX(AUTOID) FROM TableName GROUP BY State, Name, id)
group by State
having count(*) > 1

Upvotes: 0

Conrad Frix
Conrad Frix

Reputation: 52645

Apparently Access 2007 supports inline views. so you can do this. (I don't know about earlier versions)

SELECT 
   state,
   COUNT(CR_ID )

   FROM

   (


       SELECT  DISTINCT 
       lkuState.StateName AS state, 
       tblMain.CR_ID 
       FROM lkuState 
       INNER JOIN 
       (tblMain 
       INNER JOIN (locLink 
       INNER JOIN tblLoc 
       ON locLink.GEOMETRY_ID = tblLoc.GEOMETRY_ID) 
       ON tblMain.CR_ID = locLink.CR_ID) 
       ON lkuState.FIPS_State = tblLoc.FIPS_State 

       WHERE tblMain.Lost=False) t

GROUP BY
State

Update

As David-W-Fenton Notes below in regards to my uncertainty about which versions supported derived tables/inline views

Derived tables have long been supported by Jet/ACE, though the syntax used to be quite different from the simple (SELECT...) As Alias -- it was [SELECT...]. Note the trailing period on the close square bracket, and that this precluded a SQL SELECT where internal square brackets were required

Upvotes: 2

Larry Lustig
Larry Lustig

Reputation: 50970

First create a view:

SELECT DISTINCT State, Name FROM OriginalTable

Then, in your larger query, substitute OriginalTable for the table you draw this information from.

Upvotes: 0

Michael Pakhantsov
Michael Pakhantsov

Reputation: 25370

SELECT state, Count(distinct ID) AS statecount
FROM ....
GROUP BY state, Lost
HAVING Lost=false

Upvotes: -1

Related Questions