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.
Related
I'm new to SQL and am a bit confused on how I would write a query in order to get the count of state in a different table.
Ie i have this table [student]
id
school_code
0
0123
1
2345
2
2345
And this other table [school]
school_code
name
State
0123
xxyy
New Jersey
2345
xyxy
Washington
3456
yxyx
Colarado
I want to find out how I would get this table which tells me the entries for state by checking each student and making a count of how often that state occurs, ordered by most occurrences in student table.
State
No. times occured (iterating through student)
Washington
2
New Jersey
1
SELECT school.state, count(school.state)
FROM student, school
WHERE student.school_code = school.school_code
GROUP BY school.state
ORDER BY count(school.state)`
I'm not sure whether this would be iterating through each student and counting them?
Or just natural-joinging student and school and then counting all the states
When I run this on data supplied, the numbers of times occurred is a really low number which doesn't seem right?
We can simply JOIN the two tables and COUNT the school code in the students table, with GROUP BY state:
SELECT
sc.state, COUNT(st.school_code)
FROM
school sc
JOIN student st
ON sc.school_code = st.school_code
GROUP BY sc.state;
We can try out here: db<>fiddle
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
I have 2 tables: Customers and Actions, where each customer has uniqe ID (which can be found in each table).
Part of the customers became club members at a specific date (change between the customers). I'm trying to summarize their purchases until that date, and to get those who purchase more than (for example) 200 until they become club members.
For example, I can have the following customer:
custID purchDate purchAmount
1 2015-05-12 100
1 2015-07-12 150
1 2015-12-29 320
Now, assume that custID=1 became a club member at 2015-12-25; in that case, I'd like to get SUM(purchAmount)=250 (pay attention that I'd like to get this customer because 250>200).
I tried the following:
SELECT cust.custID, SUM(purchAmount)totAmount
FROM customers cust
JOIN actions act
ON cust.custID=act.custID
WHERE act.clubMember=1
AND cust.purchDate<act.clubMemberDate
GROUP BY cust.custID
HAVING totAmount>200;
Is it the right way to "attack" this question, or should I use something like while loop over the clubMemberDate (which telling the truth-I don't know how to do)?
I'm working with Teradata.
Your help will be appreciated.
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;
SQL 2012
Cannot change tables
Have two tables in question, both tables are identical
(I didn't design this)
Table A has application data. We accept applications and at the time of application we require a minimum of information - many values here can be null (or some placeholder like NA etc). It is assigned an application number (application id). If the application is accepted, we require more information - and we verify it.
Leading us to Table B - a copy of A except it is only populated with accepted applications - and verified data. So an application id will only exist in Table B if the manager accepts it. Sometimes, at the application process, the data is correct and no changes were made - leading to an application id having the exact values in both tables. Sometimes, the data changes or is added (we require all fields upon acceptance).
I would like to an easy way to capture all applications and the most current/verified data.
For instance:
Table A (Active Applications)
ApplicationID Phone State
1234 123-456-7890 AK
5678 246-802-4680 NULL
Table B (Approved/Accepted Applications)
ApplicationID Phone State
5678 246-802-4680 NY
Application 5678 was approved and, for demonstration only, the state was verified to be NY.
Application 1234 was not approved to date (but maybe in the future).
I would like to write a query which gives the following result:
ApplicationID Phone State
1234 123-456-7890 AK
5678 246-802-4680 NY
Desired behavior is essentially...return Table A unless the ApplicationID exists in Table then give me Table B instead.
Table A does contain every application expired or not, but there is an expiry date (applications good for 10 days) and it will be easy to cull those out based on that date.
Just flummoxed by needing approved and active applications.
Any help greatly appreciated.
--EDIT--
Thank you...but how do I handle the placeholders (like NA or XX) or when the verified data is deifferent from the application data? Say there is a third active application as so:
Table A (Active Applications)
ApplicationID Phone State
9876 234-432-1234 NY
Table B (Approved Applications)
ApplicationID Phone State
9876 234-432-1234 TX
The application was accepted by virtue of its existance in Table B but the state was verified to be TX and not NY.
I would like to see an output as such...Table A data if not in Table B. IF In Table B then Table B data.
ApplicationID Phone State
1234 123-456-7890 AK
5678 246-802-4680 NY
9876 234-432-1234 TX
Select a.applicationid, case when b.phone is null then a.phone else b.phone end as phone, case when b.state is null then a.state else b.state end as state
From a
Left outer join b on a.applicationid = b.applicationid
select
a.applicationid as a1, a.phone as a2, a.state as a3,
b.applicationid as b1, b.phone as b2, b.state as b3,
isnull(b.applicationid, a.applicationid) as applicationid,
isnull(b.phone, a.phone) as phone,
isnull(b.state, a.state) as state
from a
left outer join b on a.applicationid=b.applicationid
(omit line 2 and 3, these are just to show what the column entries are before isnull())