De-Dupe Query in MS Access - sql

I'm using Access to create a report. I have several duplicate records in my Avail_Amt field. I used the Query Wizard to de-dup records from a table, like this.
In (SELECT [DEALLOCALBAL] FROM [TBL_DATA] As Tmp GROUP BY [DEALLOCALBAL] HAVING Count(*)=1 )
What I really want to do, though, is de-dupe the records in my Query, not in my Table. I think it should look like the script below, but it's not working.
In (SELECT [DEALLOCALBAL] FROM [dbo_LIMIT_HIST.Avail_Amt] As Tmp GROUP BY [DEALLOCALBAL] HAVING Count(*)=1 )
dbo_LIMIT_HIST is the Table in SQL Server and Avail_Amt is the Field that I am trying to de-dupe. Any idea what could be wrong here?
My data set looks like this:
Basically, for each Contact ID I could have multiple DEALLOCALBAL amounts. I want to capture each unique amount for each Contact ID, but I don't want dupes.
Thanks!!

Related

How to sum a column from a query created table

I created a SELECT query, after I execute it, it shows me a new table as I wanted (you can see it in the screenshot).
The problem is that I'm trying to add a new line to the query which supposes to sum() the column "Price" in the table that I got from the query
SELECT SUM(PRICE) AS totalPay
FROM Meals
You can see the tables here, and also the queries that worked at that, didn't.
I want it to be like that:
Like this
Just add the SUM(price) as new field in your first SELECT.
SELECT SUM(Meals.Price) as 'TotalPay', tableN01.waiterID
FROM tableN01 INNER JOIN Meals etc.

Oracle Apex count and display number of entries in a column

I have a table in Oracle Apex populated with contacts and their information. On the dashboard of my app, I'd like it to say how many contacts are in the database using the big list of values plug-in or something like that. I'm using the following code:
select contact_ID, count(*)
from
contacts
group by contact_id
However all I'm getting is the number of times each specific contact appears in the table (which is 1), instead of the total number of contacts. I understand what I did wrong in the code, but I'm not sure how to fix it.
For further information, the table columns are very basic, just 'Name', 'contact_id' (the primary key), 'Phone_Number', and so on. The table is called CONTACTS.
I'm also looking to eventually have the total number of organizations and comments on the dashboard as well.
Thanks!
Try
SELECT
COUNT(mycolumn) AS d,
COUNT(mycolumn) AS r
FROM
mytable
I think you just want count(*):
select count(*)
from contacts;
Presumably, contactID is unique in a table called contacts.

SQL Select distinct rows with duplicate values in one column and choose one row for each duplicate based on value in primary key field

I have an SQL table with duplicate records on FacilityID. FacilityKey is unique. I need to select rows that have duplicates on FacilityID but I only want to show one record for each and I want to choose the one with the most recent (highest) FacilityKey. Can anybody help me figure out how to write my query? I've tried everything I could think of and searched the internet for something similar to no avail. All I can find are examples of identifying the duplicate records.
Something like this should work:
select FacilityID, max(FacilityKey)
from Facilities
group by FacilityID
having count(FacilityID)>1
And then if you want to get all of the fields, something like this:
select *
from facilities
inner join (
select FacilityID, max(FacilityKey) as maxkey
from Facilities
group by FacilityID
having count(FacilityID)>1
) t on t.FacilityID = facilities.FacilityID and t.maxkey=facilities.FacilityKey
try this:
select
FacilityKey , MAX(FacilityID) AS FacilityID
From YourTable
GROUP BY FacilityKey
HAVING COUNT(*)>1

How do I identify like sets using SQL?

Using SQL Server, I have a table that looks like this:
What I need to do is write a query to identify scenarios where the Name and Permissions field are equal so that I can give give them a unique Set ID.
For instance, rows 2 and 4 would be a set I can give a SetID as well as rows 6 and 7 are a set that I can give another SetID. But rows 2 and 3 are NOT a set.
So far I have tried using DENSE_RANK () Over(Order by Name) which helps to add an id based on like Names but doesn't take into account matching permissions. And have tried joining the table on itself but with millions of rows of data I end up with unwanted duplicates.
The logic I am following is this:
If (Name and Permissions) of one row = (Name and Permissions) of another row give them a SetID to share.
Please help I have been banging my head against the wall with this one. Ideally a SQL query would accomplish this but am open to anything.
Thank you!
You could do it for example like this:
select
Name,
Permission,
row_number() over (order by Name, Permission) as RN
from (
select distinct
Name,
Permission
from
permissions
) TMP
order by Name, Permission
The inner select gets the distinct combinations, and the outer one assigns the numbers.
SQL Fiddle: http://sqlfiddle.com/#!6/c8319/3
This will probably do something similar to what you want.
SELECT
name,
permissions,
accountname,
ROW_NUMBER() OVER (PARTITION BY name,permissions ORDER By name,permissions) as SetID
FROM table;

SQL query: HAVING, GROUP BY

I have two tables. One with a list of shops and their ID's (shop_id)
and one with a list of employees with the ID (shop_id) of the shop they work at.
I have to print out each employee with a certain position form a certain shop.
My query is normally correct but I seem to get an error like tblEmployees.
Normally my query would look something like.
SELECT tblEmployees.Name, tblEmployees.Surname, tblShops.shop_id
FROM tblEmployees, tblShops
GROUP BY tblEmployees.shop_id
HAVING tblEmployees.shop_id = tblShops.shop_id;
Normally I get an error saying something like:
tblEmployees.Name is not part of an aggregate function.
What I want to know is if it would solve my problem if I put every column that gives me this error under the GROUP BY statement. Or is there another way of fixing this error without it affecting the result I need to get from this query.
Drop the GROUP BY and HAVING clauses. You aren't aggregating here. You want to be joining your tables.
SELECT tblEmployees.Name, Surname, tblShops.shop_id
FROM tblEmployees JOIN tblShops
ON tblEmployees.shop_id=tblShops.shop_id