• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 81
  • Last Modified:

Reoccurring Access Query

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
bsjj2727
Asked:
bsjj2727
  • 10
  • 6
  • 6
  • +1
3 Solutions
 
PatHartmanCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
bsjj2727Author Commented:
getting Syntax error (missing operator) in query expression '[Account Number} Count(*)',
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
HainKurtSr. System AnalystCommented:
it should be

Select [Account Number], Count(*) As RecCount
0
 
bsjj2727Author Commented:
Also tried Huseyin and am getting Syntax error (missing operator) in query express 'account number'.
0
 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
bsjj2727Author Commented:
Thanks for the help, playing with this now
0
 
bsjj2727Author Commented:
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
 
HainKurtSr. System AnalystCommented:
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
 
bsjj2727Author Commented:
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
 
HainKurtSr. System AnalystCommented:
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
 
PatHartmanCommented:
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
 
bsjj2727Author Commented:
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
 
PatHartmanCommented:
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
 
bsjj2727Author Commented:
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
 
bsjj2727Author Commented:
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
 
Rey Obrero (Capricorn1)Commented:
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
 
bsjj2727Author Commented:
Rey this will work in Access?
0
 
Rey Obrero (Capricorn1)Commented:
yes, just change "yourTable"  with the name of your table
0
 
PatHartmanCommented:
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
 
PatHartmanCommented:
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
 
bsjj2727Author Commented:
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
 
PatHartmanCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 6
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now