SQL Server 2005 - Updating table in a one to many relationship - sql

I have a staging table that was created from a flat file and modified before copying to a final destination. Some of the records will be inserted and the rest updated if needed.
The only issue I have is it is a one to many relationship. The table is a list of retailers and some of them are entered with the same store name and SS# more than once but with a different contact type.
Like this:
Store_ID SS# First_Name Last_Name Type Description
________________________________________________________________________
1234 123-12-1234 JP Crawford A Owner
1234 123-12-1234 JP Crawford D Other Contact 1
1234 987-76-9876 Aaron Nola E Other Contact 2
1236 321-12-3210 Mikael Franco A Manager
1236 321-12-3210 Mikael Franco J Other Contact 7
I need to be able to select one of the records when there is a duplicate store_id/SS#. There is no date available so I do not know which record was added last. In cases where one of the records is "Owner" and the other is "Other Contact" I can assume the correct one is "Owner". Same for if one of them is "Manager" for instance. But there are some examples where one record may be "Other Contact 5" and the next is "Other Contact 6".
Any suggestions are greatly appreciated.

Assuming the highest Type (eg A) is the one you would like to select in case of duplication, the following should work:
SELECT temp.store_id, temp.ss#, temp.first_name,
temp.last_name, temp.type, temp.description
FROM
(SELECT t.store_id, t.ss#, t.first_name,t.last_name,
t.type, t.description,
ROW_NUMBER() OVER(PARTITION BY t.store_id,ss# ORDER BY t.type) num
FROM YourTable t) temp
WHERE temp.num=1;

Related

When was “Checked” added and who added it - SQL

I'm beginning to study SQL queries and attempting to understand some more difficult ones. I have these 2 tables:
User
ID_user
Name
Tracking
ID_Track
Old_Value
New_Value
Date_Entered
ID_user
The data entry interface looks like this:
User Column Date Old Value New Value
David (assistant) Status 02/2022 Pending Processing
David (assistant) address 02/2022 Miami New York City
David (assistant) Type 02/2022 House Apartment
David(assistant) Size 02/2022 Small Big
Peter(QA) Size 06/2022 - Medium
Peter(QA) Status 06/2022 - Checked
I'm trying to figure out how to join User and Tracking tables in order to know when the word “Checked” was added and who added it.
know when the word Checked was added and who added it
You can filter the tracking table for the keyword, and then bring the user name with a join on the user table:
select t.*, u.name
from tracking t
inner join user u on u.id_user = t.id_user
where t.new_value = 'Checked'
You can add more conditions in the where clause if you need more filtering criteria.

Strict Match Many to One on Lookup Table

This has been driving me and my team up the wall. I cannot compose a query that will strict match a single record that has a specific permutation of look ups.
We have a single lookup table
room_member_lookup:
room | member
---------------
A | Michael
A | Josh
A | Kyle
B | Kyle
B | Monica
C | Michael
I need to match a room with an exact list of members but everything else I've tried on stack overflow will still match room A even if I ask for a room with ONLY Josh and Kyle
I've tried queries like
SELECT room FROM room_member_lookup
WHERE member IN (Josh, Michael)
GROUP BY room
HAVING COUNT(1) = 2
However this will still return room A even though that has 3 members I need a exact member permutation and that matches the room even not partials.
SELECT room
FROM room_member_lookup a
WHERE member IN ('Monica', 'Kyle')
-- Make sure that the room 'a' has exactly two members
and (select count(*)
from room_member_lookup b
where a.room=b.room)=2
GROUP BY room
-- and both members are in that room
HAVING COUNT(1) = 2
Depending on the SQL dialect, one can build a dynamic table (CTE or select .. union all) to hold the member set (Monica and Kyle, for example), and then look for set equivalence using MINUS/EXCEPT sql operators.

MS Access - Continuous Form Select with Dropdown from Another Table

I've been using Databasedevelopment.co.uk's excellent example on how to do a continuous form select with a invisible button overlaying a checkbox to assign employees to a specific shift. I'd like to make it so that said continuous form also has a dropdown of the different Paycodes so that when they are selected I can use a combobox to indicate "Regular Pay, Overtime, etc....". I'm running into a wall because with the query as-is from the example, the recordset for the Paycode field is not updateable.
Messing with the primary key for the employee's table fixes the paycode issue but prevents the selection code from working properly.
I'm a bit out of my depth here, what's the easiest way to accomplish this?
SELECT CAT.EmployeeID, CAT.FirstName, CAT.LastName, ASGN_TEMP.ShiftNum, ASGN.PayCode, IIf(ASGN_TEMP.[ShiftNum] Is Null,0,-1) AS IsSelected
FROM tblEmployees AS CAT
LEFT JOIN (SELECT ASGN.EmployeeID, ASGN.ShiftNum, ASGN.PayCode FROM tblAssignedEmployees AS ASGN
WHERE ASGN.ShiftNum = Forms!frmMainMenu![txtShiftNum]) AS ASGN_TEMP
ON CAT.EmployeeID = ASGN_TEMP.EmployeeID;
Paycode is a static table with an ID, a Paycode and a description and would only correspond with each record in "tblAssignedEmployee". That is to say, there is no relationship between the employee or the shift with what Paycodes are available, I'd just like a second table for ease of updates.
---EDIT---
Table: Employees
ID
EmployeeID
Firstname
LastName
1
1234
Bob
Jones
2
9999
Mary
Sue
Table: AssignedEmployees
ID
EmployeeID
ShiftNum
PayCode
1
1234
1
OT
2
9999
2
Regular
3
1234
2
OT
Table: PayCodes
ID
PayCode
Desc
1
Regular
Regular Pay
2
OT
Overtime

SQL: How to create a query to order database by order ID and products inside of it

I am currently in a situation where I cannot think of a way out. To explain my problem I will first post a very simplified version of the database I am using:
ID: Place: Product:
111111 1 Product A
222222 1 Product A
222222 2 Product B
333333 1 Product A
444444 1 Product A
444444 2 Product B
444444 3 Product C
555555 1 Product A
Above 3 columns are mentioned. The first column "ID:" represents the unique ID each order has. The second "Place:" represents the position of a certain item in an order. If the order is made up of only 1 product, there will be only one row corresponding to it in the database:
111111 1 Product A
If the order has two or more products in it, there will be two or more rows in the database with the same value in the "ID:" column and the "Place:" column will represent the position of the item in the order.
222222 1 Product A
222222 2 Product B
-------------------------
444444 1 Product A
444444 2 Product B
444444 3 Product C
The third column "Product:" is pretty much obvious- it represents the name of the product.
My question is the following:
Is it possible to make a query which would find the different types of orders and count them? For example, if the order is made up of only "Product A" this should be a unique type of order. If the order consists of "Product A" in place 1 and "Product B" in place 2 (or vice versa) then that would be a unique order as well. It should look like this:
Type: Count()
Product A 3
Product A,B 1
Product A,B,C 1
I tried numerous queries but the results that I get are far from what I am looking for. Any hints would be very appreciated!
I apologize if the answer is easy or obvious. I am not very experienced with SQL yet.
Thank you in advance, Petar
if you are using mysql, you can use the group_concat function, and run a query like this:
select type, count(1) from
(
SELECT group_concat(PRODUCT ORDER BY PRODUCT) type
FROM products
GROUP BY ID
) a
group by type;
but I must warn you, this query will be executed using temporary and filesort
(select count(Product) as cnt1,ID from test1
GROUP BY ID)
Kindly share what exactly output you want in display.May be this will help you.

oracle - sql query select max from each base

I'm trying to solve this query where i need to find the the top balance at each base. Balance is in one table and bases are in another table.
This is the existing query i have that returns all the results but i need to find a way to limit it to 1 top result per baseID.
SELECT o.names.name t.accounts.bidd.baseID, MAX(t.accounts.balance)
FROM order o, table(c.accounts) t
WHERE t.accounts.acctype = 'verified'
GROUP BY o.names.name, t.accounts.bidd.baseID;
accounts is a nested table.
this is the output
Name accounts.BIDD.baseID MAX(T.accounts.BALANCE)
--------------- ------------------------- ---------------------------
Jerard 010 1251.21
john 012 3122.2
susan 012 3022.2
fin 012 3022.2
dan 010 1751.21
What i want the result to display is calculate the highest balance for each baseID and only display one record for that baseID.
So the output would look only display john for baseID 012 because he has the highest.
Any pointers in the right direction would be fantastic.
I think the problem is cause of the "Name" column. since you have three names mapped to one base id(12), it is considering all three records as unique ones and grouping them individually and not together.
Try to ignore the "Name" column in select query and in the "Group-by" clause.
SELECT t.accounts.bidd.baseID, MAX(t.accounts.balance)
FROM order o, table(c.accounts) t
WHERE t.accounts.acctype = 'verified'
GROUP BY t.accounts.bidd.baseID;