I have a list of customer email addresses and dates of their orders. I need to provide a sequential number to each customer's order.
I can perform this task in Excel by using a countifs function. However, my data is getting too large to handle there and I'd prefer to set this up in access.
The data comes in all out of order, so first I order by Customer Email and then a second order by Date of Purchase. From there, I want to apply a sequential number to their order based on date. So if joe has three orders, I want to know which order was first, second, and third.
Following guidance by Allen Browne http://allenbrowne.com/ranking.html, consider:
SELECT Table1.*,
(SELECT Count(*) AS Rank FROM Table1 AS Dupe
WHERE Dupe.[Date of Purchase]<Table1.[Date of Purchase]
AND Dupe.[Customer Email] = Table1.[Customer Email])+1 AS Rank
FROM Table1
ORDER BY [Customer Email], [Date of Purchase];
Related
I am beginner at MS access. I am making an access form to log and maintain loan payment. I have a main contact form where all the details such as a Unique ID, name, Loan amount, payment duration etc. of the person taking the loan are recorded. Now I have created another sub-form with table to record information of the amount recovered and installment paid. This table is linked to my main contact form through the Unique ID. As can been seen in the screenshot, I want a query that will automatically calculate the amount paid by a person and show what their outstanding amount is. I basically want the amount paid column to be a cumulative total of all the installments paid up to latest date. I tried the sum function in query but that didn't work. please help
You would need to use a correlated subquery such as:
select
t.*,
(
select sum(u.[amount paid])
from YourTable u
where u.[u id] = t.[u id] and u.[payment no] <= t.[payment no]
)
as PaidSoFar
from
YourTable t
Change YourTable to the name of your table.
I have a Query that Groups by a column which is needed so I get the result that I need, and then I need to return results which should be done by Grouping the previous results by another field.
So basically I have a Survey table,
sql = SELECT * FROM Survey S
WHERE S.UserId = 79
Group By S.SurveyNumber
Having SUM (S.Counter) <> 0 ORDER BY S.SubmittedDate DESC
This returns the Survey grouped by the Number, and then I need to Group the result by SurveyName and return the Last Submitted Survey for that SurveyName ( Max(submittedDate).
Can I achieve this in using one query ? If I have
GroupBy S.SurveyNumber, S.SurveyName
Then it will try to find that have BOTH of the columns same.
How do I do this ?
i think it works:
SELECT
S2.SurveyName
,SUM(S2.Count) as SurveyCount
FROM (
select
SUM(S.SurveyNumber) as Count
,S.SurveyName
FROM Survey S
where S.SurveyNumber <> 0
Group By S.SurveyNumber,S.SurveyName
) as S2
Group By S2.SurveyName
This is how I understand this:
Every survey belongs to one user. You want the survey of one particular user.
The table is actually not a Survey table (with one record representing a survey), but a kind of survey chronology table. There are multiple records per survey.
You must look at all records per survey in order to know whether its paid.
For each paid survey you want the last chronology record.
You have already shown how to check whether a survey is paid. Now select the last date for them (the maximum date). Based on this get the related records from the table.
select *
from survey
where (surveynumber, submitteddate) in
(
select surveynumber, max(submitteddate)
from survey
where userid = 79
group by surveynumber
having sum(counter) <> 0
);
I may be wrong though, because you make it sound like a survey number is somehow independent from the survey name. (One survey number with various names? The same survey name for multiple survey numbers?)
You would certaily benefit from a better data model with at least two separate tables for survey and survey details.
I am trying to retrieve the the first row among the duplicate row, THE FIRST OCCURED ***
--Table--
Order_No Product User
1 Book Student
2 Book Student
3 Book Student
I want to get the Order_No of the first duplicate row in JAVA, I have used DISTINCT and DISTINCT TOP 1 etc but nothing worked, NEED HELP
SELECT min(order_no), product, user
FROM 'table'
GROUP BY user, product
This is basic SQL?
SELECT min(order_no), product, user FROM table GROUP BY product, user
See also more information on GROUP BY
All fields not part of your group by must have some sort of way to determine which to pick of the n potentially different values. min() will pick the lowest value (even with strings and dates) while max() will pick the highest. You can also use First() and Last() to grab the value according to when they show up.
Supposing you had other values to pick from, you might see something like:
SELECT min(order_no), product, user, min(creation_date),
sum(quantity), first(billing_address)
FROM orders GROUP BY product, user
SELECT t.*
FROM table t
WHERE NOT EXISTS ( SELECT a
FROM table t2
WHERE t2.Product = t.Product
AND t2.User = t.User
AND t2.Order_No < t.Order_No
)
How to get Number of Employees Joined or Resigned by Quarterly in SQL
I have a Table called Mst_Employee
fields are
Emp_No, Emp_JoiningDate, Emp_ResignedDate, Emp_Status
Edit: Every half a year, not quarterly.
The easiest way to do analysis like this would be to use the DATEPART function in T-SQL. Assuming all you want is to know a specific quantity for all quarters in all years on your table, an example code would be:
SELECT
DATEPART(YEAR,Emp_JoiningDate) as [Year],
DATEPART(QUARTER,Emp_JoiningDate) as [Quarter],
COUNT(1) as [Emp Count]
FROM Mst_Employee
GROUP BY DATEPART(YEAR,Emp_JoiningDate),DATEPART(QUARTER,Emp_JoiningDate)
ORDER BY 1,2
This will show all the numbers of employee's joined in the quarter. The query can easily be modified to also show resigned employee's in that quarter, or you could use a separate query to show this data.
Just an additional comment, as you're in the Employee Table, you don't need to directly state "Emp_" under all of your attributes.
I am working on an employee management/reward system and need to be able to show a single "transaction history" page that shows in chronological order the different events that the employee has experienced in one list. (Sort of like how in facebook you can goto your history/action section and see a chronological list of all the stuff that you have done and affects you, even though they are unrelated to eachother and just have you as a common user)
I have different tables for the different events, each table has an employee_id key and an "occured" timestamp, some table examples:
bonuses
customers
raise
complaints
feedback
So whenever an event occurs (ie a new customer is assigned to the employee, or the employee gets a complaint or raise) a new row is added to the appropriate table with the employee ID it affects and a timestamp of when it occured.
I need a single query to pull all records (upto 50 for example) that include the employee and return a history view of that employee. The field names are different in each table (ie the bonus includes an amount with a note, the customer includes customer info etc).
I need the output to be a summary view using column names such as:
event_type = (new customer, bonus, feedback etc)
date
title (a brief worded title of the type of event, specified in sql based on the table its referencing)
description (verbiage about the action, such as if its event_type bonus display the bonus amount here, if its a complain show the first 50 characters of the complaint message or the ID of the user that filed the complaint from the complaints table. All done in SQL using if statements and building the value of this field output based on which table it comes from. Such as if its from the customers table IF current_table=customers description='A customer was assigned to you by'.customers.assigner_id).
Ideally,
Is there any way to do this?
Another option I have considered, is I could do 5-6 different queries pulling the records each from their own table, then use a mysql command to "mesh/interleave" the results from all the queries into one list by chronological order. That would be acceptable too
You could use a UNION query to merge all the information together and use the ORDER BY clause to order the actions chronologically. Each query must have the same number of fields. Your ORDER BY clause should be last.
The examples below assume you have a field called customer_name in the customers table and bonus_amount in the bonuses table.
It would look something like this:
SELECT 'New Customer' as event_type, date,
'New customer was assigned' as title,
CONCAT('New Customer: ', customer_name, ' was assigned') as description
FROM customers
WHERE employee_id = 1
UNION
SELECT 'Bonus' as event_type, date,
'Received a bonue' as title,
CONCAT('Received a bonus of $', FORMAT(bonus_amount, 2), '.') as description
FROM bonuses
WHERE employee_id = 1
UNION
...
ORDER BY date DESC;