How do I used distinct to remove duplicates in this query? - sql

I have the following table: Table
I am trying to write a query (that I will include in another query) to display how many account numbers there are per symbol.
I wrote the following query:
SELECT Symbol,
(SELECT DISTINCT COUNT([Account Number]) FROM [Open] T2 WHERE T2.Symbol = T1.Symbol) AS Accounts
FROM Open T1
GROUP BY [Symbol];
The query displays like this but it counts the same account number multiple times per symbol. EURUSD should have 3 and USDJPY should only have 1 next to it.
It should display like this.
I am trying to include this as part of another big table that has other information next to each symbol too.
I will appreciate any assistance.

Access doesn't support count(DISTINCT ...). You can try
SELECT Symbol, count(*) AS Accounts
FROM (SELECT DISTINCT Symbol, Account FROM Open)
GROUP BY Symbol;

Related

Need to list the rows contained in a count(*) Access

Hello everyone and thank you in advance for your help.
I'm having troubles with an SQL query in access.
I have
Database
I need the following output ( show and list the Store Number and PaidMoney ONLY where Paid money is the same amount 2 or more times only)
desired output
I already have 2 queries which kind of solve the problem, but each of the queries I have only solve 50% of the answer I need, the first lists all the results not only the duplicates , and the second query tells me how many duplicates there are but doesnt list and show the duplicates to me.
First Query
SELECT StoreNumber, PaidMoney
FROM Stores
Second query
SELECT StoreNumber, PaidMoney, COUNT(*)
FROM Stores
GROUP BY StoreNumber, PaidMoney
HAVING COUNT(*) > 1
Thank you all for your time and help!
You can join the 2 queries together and only "select" the columns from the first query. The second query will just filter out the rows from the first query that only appear once. Rows from the first query that appear more than once should appear as many times as they appear in the first query.
SELECT q1.StoreNumber, q1.PaidMoney
FROM (SELECT StoreNumber, PaidMoney
FROM Stores) q1
INNER JOIN (SELECT StoreNumber, PaidMoney, COUNT(*) ct2
FROM Stores
GROUP BY StoreNumber, PaidMoney
HAVING COUNT(*) > 1) q2
ON q1.StoreNumber = q2.StoreNumber
AND q1.PaidMoney = q2.PaidMoney

Join query in Access 2013

Currently have a single table with large amount of data in access, due to the size I couldn't easily work with it in Excel any more.
I'm partially there on a query to pull data from this table.
7 Column table
One column GL_GL_NUM contains a transaction number. ~ 75% of these numbers are pairs. I'm trying to pull the records (all columns information) for each unique transaction number in this column.
I have put together some code from googling that hypothetically should work but I think I'm missing something on the syntax or simply asking access to do what it cannot.
See below:
SELECT SOURCE_FUND, GLType, Contract, Status, Debit, Credit, GL_GL_NUM
FROM Suspense
JOIN (
SELECT TC_TXN_NUM TXN_NUM, COUNT(GL_GL_NUM) GL_NUM
FROM Suspense
GROUP BY TC_TXN_NUM HAVING COUNT(GL_GL_NUM) > 1 ) SUB ON GL_GL_NUM = GL_NUM
Hey Beth is this the suggested code? It says there is a syntax error in the FROM clause. Thanks.
SELECT * from SuspenseGL
JOIN (
SELECT TC_TXN_NUM, COUNT(GL_GL_NUM) GL_NUM
FROM Suspense
GROUP BY TC_TXN_NUM
HAVING COUNT(GL_GL_NUM) > 1
Do you want detailed results (all rows and columns) or aggregate results, with one row per tx number?
If you want an aggregate result, like the count of distinct transaction numbers, then you need to apply one or more aggregate functions to any other columns you include.
If you run
SELECT TC_TXN_NUM, COUNT(GL_GL_NUM) GL_NUM
FROM Suspense
GROUP BY TC_TXN_NUM
HAVING COUNT(GL_GL_NUM) > 1
you'll get one row for each distinct txn, but if you then join those results back with your original table, you'll have the same number of rows as if you didn't join them with distinct txns at all.
Is there a column you don't want included in your results? If not, then the only query you need to work with is
select * from suspense
Considering your column names, what you may want is:
SELECT SOURCE_FUND, GLType, Contract, Status, sum(Debit) as sum_debit,
sum(Credit) as sum_credit, count(*) as txCount
FROM Suspense
group by
SOURCE_FUND, GLType, Contract, Status
based on your comments, if you can't work with aggregate results, you need to work with them all:
Select * from suspense
What's not working? It doesn't matter if 75% of the txns are duplicates, you need to send out every column in every row.
OK, let's say
Select * from suspense
returns 8 rows, and
select GL_GL_NUM from suspense group by GL_GL_NUM
returns 5 rows, because 3 of them have duplicate GL_GL_NUMs and 2 of them don't.
How many rows do you want in your result set? if you want less than 8 rows back, you need to perform some sort of aggregate function on each column you want returned.
You could do something like the following:
SELECT S.* FROM
SUSPENSE AS S
INNER JOIN (SELECT DISTINCT GL_GL_NUM, MIN(ID) AS ID FROM SUSPENSE
GROUP BY GL_GL_NUM) AS S2
ON S.ID = S2.ID
AND S.GL_GL_NUM = S2.GL_GL_NUM
Which would return a single row for a unique gl_gl_num. However if the other rows have different data it will not be shown. You would have to either aggregate that data up using SUM(Credit), SUM(Debit) and then GROUP BY the gl_gl_num.
I have attached a SQL Fiddle to demonstrate my results and make this clearer.
http://sqlfiddle.com/#!3/8284f/2

SQL - Where Field has Changed Over Time

I'm running SQL server management studio and my table/dataset contains approximately 700K rows. The table is a list of accounts each month. So at the begining of each month, a snapshot is taken of all the accounts (and who owns them), etc. etc. etc. and that is used to update the data-set. The 2 fields in question are AccountID and Rep (and I guess you could say month). This query really should be pretty easy but TBH, I have to move-on to other things so I thought I'd throw it up here to get some help.
Essentially, I need to extract distinct AccountIDs that at some point changed reps. See a screenshot below of what I'm thinking:
Thoughts?
--- I should note for instance that AccountID ABC1159 is not included in the results b/c it appears only once and is never handled by any other rep.
--- Also, another parameter is if the first time an account appears and the rep name appears in a certain list and then moves to another rep, that's fine. For instance, if the first instance of a Rep was say "Friendly Account Manager" or "Support Specialist" and then moves to another name, those can be excluded from the result field. So we essentially need a where statement or something that eliminates those results if the first instance appears in this list, then there is an instance where the name changed but non after that. The goal is to see if after the rep received a human rep (so they didn't have a name in that list), did they then change human reps at a certain point in time.
Try this:
SELECT t.AccountID
FROM [table] t
WHERE NOT EXISTS(SELECT * FROM [reps table] r WHERE r.Rep = t.Rep AND r.[is not human])
GROUP BY t.AccountID
HAVING COUNT(DISTINCT t.Rep) > 1;
You want to first isolate the distinct AccountID and Rep combinations, then you want to use GROUP BY and HAVING to find AccountID values that have multiple Rep values:
SELECT AccountID
FROM (SELECT DISTINCT AccountID, Rep
FROM YourTable
WHERE Rep NOT IN ('Support Specialist','Friendly Account Manager')
)sub
GROUP BY AccountID
HAVING COUNT(*) > 1
Try a SELECT DISTINCT on the table and join the table to itself - something like this - with Account being the name I gave your table:
SELECT DISTINCT A1.AccountID, A1.Rep, A1.ReportMonth
FROM AccountTable.AccountID A1
INNER JOIN AccountTable A2
ON A1.AccountID = A2.AccountID
AND A1.Rep <> A2.Rep
ORDER BY A1.AccountD

Count and Join using access database

Sorry if this seems simple to you guys but I've been struggling for ages:
I have 2 tables Companies and Quotes. The companies table contains the Rep for the company and I'm trying to get the number of quotes for each company by Rep. The 2 tables are linked by 'Ref' in the Companies table and 'CompanyRef' in the Quotes table.
In a perfect world the list would look something like:
Rep, Company, (Quote Count)
The closest I think I've come is:
SELECT Companies.Rep, Companies.Company, COUNT(Quotes) AS [Quote Count]
FROM Companies
INNER JOIN Quotes ON Companies.Ref = Quotes.CompanyRef
GROUP BY Companies.Rep, Companies.Company
ORDER by Count(Quotes) Desc
But I'm just getting
Too Few Parameters, Expected 1
I should also mention, not every company has a rep allocated.
Any help gratefully appreciated. Steve.
Don't use the Table name in your Count clause but indicate which Columns you want to count (use * for 'don't care'). If you have the name of the table in the Count function access tries to find a field with that name...
SELECT Companies.Rep, Companies.Company, COUNT(*) AS [Quote Count]
FROM Companies
INNER JOIN Quotes ON Companies.Ref = Quotes.CompanyRef
GROUP BY Companies.Rep, Companies.Company
ORDER by Count(*) Desc

how to query access to select entire records given a distinct criteria

I want to select the entire first row of each record where a promo code is unique. I am trying to create a samples table, in this table will be one record (the first record) from each distinct promo code. I have asked all of my co-workers and they usually go though the data by hand and select one from each. the problem is that the number of promo codes grows each time and the codes change. so I want to write a query that will select the first record found to have each distinct code. so for I have something like this:
SELECT DISTINCT Customer.promo1 FROM Customer AS promo;
SELECT * FROM Customer, promo
WHERE Customer.promo1 = promo.promo1;
But this obviously give the original table. I do have a ID field called AutoID in Customer.
Thanks in advance.
I'm assuming you want the first Customer.AutoId associated with each Customer.Promo
SELECT
c.*
FROM
Customer c
INNER JOIN
(
SELECT
c.promo1,
MIN(c.AutoID) AutoID
FROM
Customer c
GROUP BY
c.promo1) FirstCusomterWithPromo
ON c.AutoID = FirstCusomterWithPromo.AutoID
Something like that:
SELECT * FROM Customer
GROUP BY Customer.promo1