SQL Statement to Remove Duplicates and get Counts

允我心安 提交于 2019-12-24 10:27:02

问题


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


回答1:


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




回答2:


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.




回答3:


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



回答4:


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 :




回答5:


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


来源:https://stackoverflow.com/questions/4784191/sql-statement-to-remove-duplicates-and-get-counts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!