Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
PeterBaileyUk

asked on

count 1s in access 2016

I have a query that tells me where the data has changed over a period the fields represent the data element

the last column gives me the representation of what changed. I would also like a numerical count so i can sort by number of changes.

so 10000000 =1
      00100111=4


and so on but not sure how to do that, i wonder if maybe regular expressions can do that or maybe a function

SELECT TblUnionCodes.Capcode, Count(QryLongModelDescEvents.CapCode) AS LongModelDesc, Count(QryManfEvents.CapCode) AS Manf, Count(QryNomEvents.CapCode) AS Nom, Count(QrySeatsEvents.CapCode) AS Seats, Count(QryShortModelDescEvents.CapCode) AS ShortModelDesc, Count(QryTransmissionEvents.CapCode) AS Transmission, Count(QryValvesEvents.CapCode) AS Valves, Count(QryWBaseTypeEvents.CapCode) AS WBaseType, IIf(Count([QryLongModelDescEvents].[capcode])>0,"1",0) & IIf(Count([QryManfEvents].[capcode])>0,"1",0) & IIf(Count([QryNomEvents].[capcode])>0,"1",0) & IIf(Count([QrySeatsEvents].[capcode])>0,"1",0) & IIf(Count([QryShortModelDescEvents].[capcode])>0,"1",0) & IIf(Count([QryTransmissionEvents].[capcode])>0,"1",0) & IIf(Count([QryValvesEvents].[capcode])>0,"1",0) & IIf(Count([QryWBaseTypeEvents].[capcode])>0,"1",0) AS ResultString
FROM (((((((TblUnionCodes LEFT JOIN QryLongModelDescEvents ON TblUnionCodes.Capcode = QryLongModelDescEvents.CapCode) LEFT JOIN QryManfEvents ON TblUnionCodes.Capcode = QryManfEvents.CapCode) LEFT JOIN QryNomEvents ON TblUnionCodes.Capcode = QryNomEvents.CapCode) LEFT JOIN QrySeatsEvents ON TblUnionCodes.Capcode = QrySeatsEvents.CapCode) LEFT JOIN QryShortModelDescEvents ON TblUnionCodes.Capcode = QryShortModelDescEvents.CapCode) LEFT JOIN QryTransmissionEvents ON TblUnionCodes.Capcode = QryTransmissionEvents.CapCode) LEFT JOIN QryValvesEvents ON TblUnionCodes.Capcode = QryValvesEvents.CapCode) LEFT JOIN QryWBaseTypeEvents ON TblUnionCodes.Capcode = QryWBaseTypeEvents.CapCode
GROUP BY TblUnionCodes.Capcode
HAVING (((Count(QryLongModelDescEvents.CapCode))>0)) OR (((Count(QryManfEvents.CapCode))>0)) OR (((Count(QryNomEvents.CapCode))>0)) OR (((Count(QrySeatsEvents.CapCode))>0)) OR (((Count(QryShortModelDescEvents.CapCode))>0)) OR (((Count(QryTransmissionEvents.CapCode))>0)) OR (((Count(QryValvesEvents.CapCode))>0)) OR (((Count(QryWBaseTypeEvents.CapCode))>0))
ORDER BY IIf(Count([QryLongModelDescEvents].[capcode])>0,"1",0) & IIf(Count([QryManfEvents].[capcode])>0,"1",0) & IIf(Count([QryNomEvents].[capcode])>0,"1",0) & IIf(Count([QrySeatsEvents].[capcode])>0,"1",0) & IIf(Count([QryShortModelDescEvents].[capcode])>0,"1",0) & IIf(Count([QryTransmissionEvents].[capcode])>0,"1",0) & IIf(Count([QryValvesEvents].[capcode])>0,"1",0) & IIf(Count([QryWBaseTypeEvents].[capcode])>0,"1",0) DESC;

Open in new window

ee.PNG
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Just a function to loop through the string and count the 1's

Jim.
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try something like this

Len([ResultString])-Len(Replace([ResultString],"1",""))

Open in new window

Regards
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterBaileyUk

ASKER

Thank you was a very good solution with minimal coding needed.
Welcome.

~bp
Just FYI, the aggregation functions like Count(), Avg(), Sum() aggregate over the entire recordset so  having multiple Count() functions doesn't really make sense unless what you are really counting is non-null values.

Count(*) counts the rows selected
Count(somefield) counts the non-null values of somefield for the rows selected.  If all rows are populated, the two counts would return the same number.

Also, keep in mind that the query engine can optimize Count(*) but it cannot optimize Count(somefield).  The second expression will most likely force a full table scan whereas the first can frequently use indexes.