Solved

Reoccurring Access Query

Posted on 2017-05-16
24
43 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 37

Expert Comment

by:PatHartman
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 53

Expert Comment

by:Huseyin KAHRAMAN
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
getting Syntax error (missing operator) in query expression '[Account Number} Count(*)',
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
it should be

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

Author Comment

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

Expert Comment

by:Huseyin KAHRAMAN
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 53

Expert Comment

by:Huseyin KAHRAMAN
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
Thanks for the help, playing with this now
0
 

Author Comment

by:bsjj2727
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 53

Expert Comment

by:Huseyin KAHRAMAN
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
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 53

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 125 total points
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 37

Expert Comment

by:PatHartman
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
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 37

Expert Comment

by:PatHartman
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
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
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 125 total points
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
Rey this will work in Access?
0
 
LVL 120

Expert Comment

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

Accepted Solution

by:
PatHartman earned 250 total points
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 37

Expert Comment

by:PatHartman
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
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 37

Expert Comment

by:PatHartman
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Join & Write a Comment

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

737 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