?
Solved

Reoccurring Access Query

Posted on 2017-05-16
24
Medium Priority
?
63 Views
Last Modified: 2017-05-17
I'm looking to run a query that can show me reoccurring account numbers.   I have a status column which can have a status such as New, Completed or In Process.  I have another column with account numbers in it.  In my database there are times where an account will have a status as completed and a status as New when their issue reappears.  I'm looking to run a query that can show me all account numbers that have a status of New and Completed.  I will attach a screen shot of my table in excel.  Appreciate any help anyone can offer.
EE.PNG
0
Comment
Question by:bsjj2727
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
  • 6
  • +1
24 Comments
 
LVL 38

Expert Comment

by:PatHartman
ID: 42137597
Select [Account Number] Count(*) As RecCount
From YourTable
Where [Status] In ("New", "Completed")
Group By [Account Number]
Having Count(*) > 1

The WHERE clause is applied before aggregation and the HAVING clause is applied after aggregation.
0
 
LVL 58

Expert Comment

by:HainKurt
ID: 42137631
this shows the result if pattern is always new/completed...

select acctNumber, case count(1) % 2 when 0 then "OK" when 1 then "Not OK" end status
from myTable
where  Status In ("New", "Completed")
group by acctNumber

Open in new window

0
 

Author Comment

by:bsjj2727
ID: 42137638
getting Syntax error (missing operator) in query expression '[Account Number} Count(*)',
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 58

Expert Comment

by:HainKurt
ID: 42137659
it should be

Select [Account Number], Count(*) As RecCount
0
 

Author Comment

by:bsjj2727
ID: 42137660
Also tried Huseyin and am getting Syntax error (missing operator) in query express 'account number'.
0
 
LVL 58

Expert Comment

by:HainKurt
ID: 42137668
Select [Account Number], Count(*) As RecCount
From YourTable
Where [Status] In ("New", "Completed")
Group By [Account Number]
Having Count(*) > 1

Open in new window


check column names and table name
0
 
LVL 58

Expert Comment

by:HainKurt
ID: 42137673
my solution for access

select [Account Number], iif(count(1) mod 2 = 0, "OK" , "Not OK") as status
  from myTable
 where Status In ("New", "Completed")
 group by [Account Number]

Open in new window

0
 

Author Comment

by:bsjj2727
ID: 42137696
Thanks for the help, playing with this now
0
 

Author Comment

by:bsjj2727
ID: 42137701
I also have a first and last name column, if I wanted that to show up in the query also next to the account number, is that simple enough to do?
0
 
LVL 58

Expert Comment

by:HainKurt
ID: 42137711
or maybe this

select distinct [Account Number],
       (select top 1 status
          from myTable b
         where a.[Account Number]=b.[Account Number]
         order by [date] desc
       ) as LatestStatus
from myTable a

Open in new window

0
 

Author Comment

by:bsjj2727
ID: 42137762
Huseyin can you assist with this

I also have a first and last name column, if I wanted that to show up in the query also next to the account number, is that simple enough to do?
0
 
LVL 58

Assisted Solution

by:HainKurt
HainKurt earned 500 total points
ID: 42137770
try this

select [Account Number], FirstName, LastName,
       (select top 1 status
          from myTable b
         where a.[Account Number]=b.[Account Number]
         order by [date] desc
       ) as LatestStatus
  from (
       select distinct [Account Number], FirstName, LastName      
         from myTable
       ) a

Open in new window

0
 
LVL 38

Expert Comment

by:PatHartman
ID: 42138008
1.  When you say you're getting a syntax error -- we need to see YOUR SQL string.
2.  You did not mentioned SQL Server so are you looking for an Access solution or a T-SQL solution because at least one poster is posting T-SQL which will not work in Access unless you run it as a pass-through query.
0
 

Author Comment

by:bsjj2727
ID: 42138015
I need Access query the one you sent worked, I was missing a comma which fixed it, do you know how I can get first and last name with it next to account number also?
0
 
LVL 38

Expert Comment

by:PatHartman
ID: 42138040
First and Last name are not in the recordset you posted.  You will need to add the table where they exist to the query and join on [Account Number] (assuming that is the foreign key.  First and Last name also get added to the Group By clause.
0
 

Author Comment

by:bsjj2727
ID: 42138043
There is a first name and last name in the data set, I just left it off in the data sample that I original posted, it would make the query look cleaner with that included
0
 

Author Comment

by:bsjj2727
ID: 42138638
Thanks for the help up until this point, here is a screen shot of the data in Excel.  I'm looking to do what you guys had already sent me but have it include the first and last name in the results for the query.  For instance the query results would show the account number for both Bill Smith and Matt Burke since both have a record for New and Completed.  So the results of the query would look like this

Account Number     First Name    Last Name
101                             Bill                  Smith
111                             Matt               Burke


Thanks again for the help on this, I am very new to Access
EE.PNG
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 500 total points
ID: 42138862
test this query
SELECT T.[Account Number], T.[First Name], T.[Last Name]
FROM yourTable AS T INNER JOIN (SELECT T1.[Account Number], Count(T1.[Account Number]) AS [CountOfAccount Number]
FROM yourTable AS T1
WHERE T1.Status In ("New","Completed")
GROUP BY T1.[Account Number]
HAVING Count(T1.[Account Number])>1
)  AS T2 ON T.[Account Number] = T2.[Account Number]
GROUP BY T.[Account Number], T.[First Name], T.[Last Name];

Open in new window

0
 

Author Comment

by:bsjj2727
ID: 42138987
Rey this will work in Access?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 42139014
yes, just change "yourTable"  with the name of your table
0
 
LVL 38

Accepted Solution

by:
PatHartman earned 1000 total points
ID: 42139022
As I already mentioned, You need to include the name fields in the Group By.

Select [Account Number], LastName, FirstName, Count(*) As RecCount
From YourTable
Where [Status] In ("New", "Completed")
Group By [Account Number], LastName, FirstName
Having Count(*) > 1

I think there was a missing comma in my original suggestion.
0
 
LVL 38

Expert Comment

by:PatHartman
ID: 42139393
I don't mean to sound ungrateful but awarding everyone "participation" points does not actually help.  It doesn't reward the person who actually solved your problem and in fact diminishes his contribution and it confuses people who find the thread later when looking to solve a similar problem.  You selected three different SQL strings, only one of which will work.  So in the future,, anyone looking for a similar answer will be confused because he won't know which answer actually worked.  My first post answered your original question.  Then you changed the question and I explained the change you would need to make in words rather than modifying the query directly because the only reason I answer questions is to teach.  If you need me to do work for you for which you are getting paid, you'll need to hire me.  Granted, my first answer had a typo because it was missing a comma.  That's the problem with having to type "air code".  There is no syntax checker.  There are cases where it makes sense to accept multiple answers but I don't see this as one of them.  The bottom line is - if my answer isn't your solution, please don't feel the need to award me "participation points".  I come from an era when we actually earned our trophies and the current practice of giving everyone who showed up a trophy diminishes all contributions.
0
 

Author Comment

by:bsjj2727
ID: 42139408
Pat to be honest, I come from the same era where trophies are earned and I didn't want to give participation to everyone involved.  With that being said I don't post on here often but anytime I do always seem to get some sort of comment from someone complaining about the way I handed out points.  I can guarantee if I would have just awarded you as the only solution I would have a moderator sending me a message today giving me the riot act about how i didn't hand out points to others that tried to assist as well.  That's why I use this service as a last resort because it gets pretty old.  So I apologize and I appreciate the help.
0
 
LVL 38

Expert Comment

by:PatHartman
ID: 42139435
Sorry about the complaint but as we are moving away from the stultifying Political Correctness society has been plagued with for 40 years, I'm doing my part to see that we are also moving away from the "everyone DESERVES a trophy" philosophy.   I don't post for points although many do.  The real problem with assigning points to all participants is that it diminishes the value of the Q&A database so for that reason alone, I would discourage it.  Always do what you think is right and ignore those who complain (like me).  As long as you are  thoughtful in your award, no one should feel slighted except the trolls who are only there for the "participation points" since that's all they ever get..
1

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question