count 1s in access 2016

PeterBaileyUk
PeterBaileyUk used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

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

Jim.
Top Expert 2016

Commented:
Hi,

pls try something like this

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

Open in new window

Regards
Test your restores, not your backups...
Top Expert 2016
Commented:
Couldn't this just be:

Len(Replace([ResultString],"0",""))


~bp
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Thank you was a very good solution with minimal coding needed.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Welcome.

~bp
Distinguished Expert 2017

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial