I have a problem in writing a MySQL query from my table INVOICES
I have 1 line per item purchased (ID, Date, Time, First, Last_Name, ITEM_PURCH, PRICE)
I would like to use this table to extract the customers who came into the shop.
For example, I have Zoe MARTIN who purchased 2 items the same day
Also, Cammille VELAZQUEZ who also purchase 2 items the same day
I would like to be able to SELECT with the question: PLEASE SORT THE CUSTOMERS LIST WHO CAME in the 30/01/2019 regardless the number of products purchased
With a result like this
46681 30/01/2019 09:09:24 Zoe MARTIN
46685 30/01/2019 09:39:08 Carmen SMITH
46686 30/01/2019 09:58:14 Valery SIMON
46689 30/01/2019 10:17:11 Camille VELAZQUEZ
46691 30/01/2019 10:24:30 Alexia SERR
46694 30/01/2019 10:49:40 Olivia ANABEL
46697 30/01/2019 11:13:18 Jennifer JOGHIN
Eliminating the MARTIN duplicate and the VELAZQUEZ
Original table with 2 duplicates
ID Date Time First Last_Name ITEM PURCH Price
46681 30/01/2019 09:09:24 Zoe MARTIN First purchase 175.00
46682 30/01/2019 09:09:57 Zoe MARTIN New proetct 175.00
46685 30/01/2019 09:39:08 Carmen SMITH Product 1 104.17
46686 30/01/2019 09:58:14 Valery SIMON Product 3 145.83
46689 30/01/2019 10:17:11 Camille VELAZQUEZ Product1 104.17
46690 30/01/2019 10:17:25 Camille VELAZQUEZ Product2 104.17
46691 30/01/2019 10:24:30 Alexia SERR Product 555 129.17
46694 30/01/2019 10:49:40 Olivia ANABEL Product 23 33.33
46697 30/01/2019 11:13:18 Jennifer JOGHIN Product 345 250.00
ANy help with building the table would be fantastic
I'm giving more information about this question.
I need to delete the duplicate lines of a customer came twice or more during the same day. But I want to keep the lines if the customer has come yesterday or one month ago (just removing the duplicates each day he came).
I would like to ask MySQL
"please make a list of customers who came at least once a day in the shop, from what you have in the invoice table" - I only need 1 line per customer per day (even if he purchased 2 or 3 items).
I want to be able to say:
From the beginning of this database until now, I got XXX different costumers who bought at least 1 thing. And here is the list of their names.
Try this
select a.ID, a.Date, a.Time, a.First, a.Last_Name from invoices a join ( select id,Date,concat(First, Last_Name) as fullname from INVOICES group by Date,fullname ) b on a.id=b.id;
Related
Book Name Author Name No. of Copies Entry Date
Web Development Er. Gurbax Singh 7 2015-10-07
PHP Mr. Yash Arora 5 2015-10-06
DBMS Mr. Subhash Singla 2 2015-10-30
Data Structure Mr. Balwinder Singh 6 2015-11-14
Multimedia Dr. Latila Bhutani 3 2015-11-23
Graphics Designing Er. Gurpreet Kaur 6 2015-11-23
Note: I want sum of no. of copies
use SUM
SELECT SUM(no_of_copies) AS totalBooks FROM tablename
or Group by sum
SELECT SUM(no_of_copies) AS totalBooks FROM tablename group by BookName
I have 2 mysql tables :- entry and result
The structure of entry is as follows :-
Name Department Units_Sold Month
John Sales 3 January
John Sales 6 January
Ana Retail 11 January
Rick Marketing 1 February
Vicky Sales 4 March
Ana Retail 2 March
Vicky Sales 9 March
Vicky Sales 1 March
Each user enters the number of unit sold. Name, Department and Month are fetched from the server, when a user logs in and enters the "Units Sold".
When this entry is being made by the user I want another table to automatically update the monthly report. The 2nd table "result" is having the following structure :-
Name Department January February March
John Sales 9 0 0
Ana Retail 11 0 2
Rick Marketing 0 1 0
Vicky Sales 0 0 14
In this table, the fields:- Name, Department, January, February,... December are static. I want to update the sum of "Units Sold" by each user for month when he/she makes an entry into the 1st table.
Can anyone, please suggest on this ?
Thanks !!
What you are looking to do is produce what's called a Pivot Table, using the data stored in your first table. It's a relatively complex collection of aggregate functions that is well worth learning - especially if you have anything to do with financial reporting. This site has a good intro into doing what you are asking.
I am creating a report in SSRS 2008R2. Report looks like this:
NSN Learner Employer Completed Date
Qualification 1 123356 Tom Employer 1 12/2/2012
213134 Jerry Employer 2 25/2/2010
545456 Harry Employer 3 04/2/2011
Qualification 2 565557 Pete Employer 1 17/12/2001
154646 Susie Employer 5 12/13/2005
I want to calculate the total number of records in one qualification and put it as a row in the table after each qualification. Table is grouped by qualification field. Report should look like this:
NSN Learner Employer Completed Date
Qualification 1 123356 Tom Employer 1 12/2/2012
213134 Jerry Employer 2 25/2/2010
545456 Harry Employer 3 04/2/2011
**Total 3**
Qualification 2 565557 Pete Employer 1 17/12/2001
154646 Susie Employer 5 12/13/2005
**Total 2**
Thanks in advance.
You can add total after group and use expression CountDistinct(Completed_Date) to get desired value
I'm trying to retrieve data from tables and combine multiple rows into a single column, without repeating any information.
I have the following tables: profile, qualification, projects.
Profile
pro_id surname firstname
------ ------- ----------
1 John James
2 King Fred
3 Luxury-Yachts Raymond
Qualification
pro_id Degree School Year
------ ------ ------ -----
1 MBA Wharton university 2002
1 LLB Yale University 2001
2 BSc Covington University 1998
2 BEd Kellog University 1995
Projects
pro_id Title Year
------ ------ ------
1 Social Networking 2003
1 Excavation of aquatic debris 2007
2 Design of solar radios 1992
2 Development of expert systems 2011
I want to retrieve the all of the information for each person, with each person appearing only once in the result. The info on qualifications and projects should each be in their own column (one column for qualifications, another for projects), separated by commas. For example, the results for the above sample data should be:
1 John James MBA Wharton university 2002, LLB Yale University 2001 Social Networking 2003, Excavation of aquatic debris 2007, Design of Solar panels 2008
2 King Fred BSc Covington University 1998, BEd Kellog University 1995, Msc MIT 2011 Design of solar radios 1992, Development of expert systems 2011
3 Raymond Luxury-Yachts
Currently, I have the query:
SELECT pro_id,
surname,
firstname,
group_concat(degree,school,year) AS qual,
concat(Title,year) AS work
FROM profile,
LEFT JOIN qualification
ON qualification.pro_id = profile.pro_id
JOIN projects
ON projects.pro_id = profile.pro_id
GROUP BY pro_id
For the sample data, this query results in:
1 John James MBA Wharton university 2002, Social Networking 2003
1 John James LLB Yale University 2001, Excavation of aquatic debris 2007
1 John James MBA Wharton university 2002, Social Networking 2003, Excavation of aquatic debris 2007
etc
Note: Raymond Luxury-Yachts isn't present in the current result.
I don't want duplicate result records. Also if the surname does not have any entry in the qualification and projects table, I want the query to return the name and display an empty field in the qualification and projects table instead of omitting them altogether.
Replace LEFT JOIN with JOIN
Select pro_id, surname, firstname, group_concat(degree,school,year) as qual,concat(Title,year) as work
from profile
join qualification on qualification.pro_id = profile.pro_id
join projects on projects.pro_id = profile.pro_id group by pro_id
What is the difference between "INNER JOIN" and "OUTER JOIN"?
Using Join will fix the issue with displaying values even if there are no records in the projects table.
For the first question, you can try making a stored function and calling it from the select statement. This function will take pro_id as parameter, create the concatenated string and return it. That's the only solution for MySQL that I can think of at the moment.
I think you are close on your thoughts of group_concat. However, with possible No values (thus leaving nulls), can cause problems. I would have each secondary table pre-concatinated by person's ID and join to THAT result. Eliminates the problem of nulls
SELECT
p.pro_id,
p.surname,
p.firstname,
PreQConcat.UserQual,
PrePJConcat.UserWork
FROM
profile p
LEFT JOIN
( select q.pro_id,
group_concat( q.degree, q.school, q.year) AS UserQual
from
qualification q
group by
q.pro_id ) PreQConcat
ON p.Pro_ID = PreQConcat.pro_id
LEFT JOIN
( select pj.pro_id,
concat(pj.Title, pj.year) AS UserWork
from
projects pj
group by
pj.pro_id ) PrePJConcat
ON p.Pro_ID = PrePJConcat.pro_id
You are going through all people anyhow, and want all their respective elements (when they exist) grouped, so why group on a possibility it doesn't exist. Let the JOINED queries run once each, complete with a single result grouped by only those people it had data for, then join back to the original profile person.
I have a database two tables and a linking table that I need a JOIN query for:
Here are my Tables:
family (userid (int), loginName, etc)
member (memberid (int), loginName(this links member to a family),name, etc)
Linking Table:
user2member (userid,memberid)...would both be foreign keys?
I want to do two things:
1) Be able to have a family.loginName(12,Johnson) subscribe to another family.loginName (43,Smith) and record that into the linking table.
That would look like this:
12,43
2) When I do a query for all the member.name that are in the Johnson Family, I'll get all the Johnsons & all the Smiths.
If Johnson = Ted, Sue & Patty
IF Smith =Joe, Sue & Bob
my query results would be Johnson now = Ted,Sue,Patty,Joe,Sue,Bob
I asked this question a few days ago without good table names and I ended up confusing myself and the nice guy Ollie Jones who posted an answer similar to this for the query:
SELECT member.name
FROM family
JOIN user2member on family.userid = member.memberid
JOIN member on user2member.name = member.name
WHERE family.userid = '30'
ORDER BY member.name
I had to change Ollie's answer to match my tables but I'm getting a limit error 0,30 on line 5.
This is my first time doing JOINS and I have no idea if this is correct.
Thanks,
Here's the link to my first question: mySQL table linking , group linked to other members lists, the displaying all members
I am not sure, if the tables you suggested would solve your problem. If I understand your question correct, there are two relationships:
a relationship for all family members (Johnson with Ted, Sue, Patty, Smith with Joe, Sue, Bob)
a relationship for subscriptions (a family can subscribe to another family)
I would suggest following tables:
family (f_id, f_loginName, etc.)
member (m_id, m_f_id, m_name) (foreign key to family, many-to-one relationship)
subscription (s_f_id,s_to_f_id) (linking is based on both family keys)
This would result in following contents:
family:
f_id f_loginName
12 Johnson
43 Smith
member:
m_id m_f_id m_name
1 12 Ted
2 12 Sue
3 12 Patty
4 43 Joe
5 43 Sue
6 43 Bob
subscription
s_f_id s_to_f_id
12 43
Now, to get all possible members for a specific family and it's subscriptions, I would use following SQL query. It has a simple join for family and it's family members. In the WHERE clause, the family Johnson is fetched (f_id = 12) and to get all family members from the subscriptions, it's easier to use a subquery.
SELECT f_loginName, m_name
FROM family
INNER JOIN member ON m_f_id = f_id
WHERE f_id = 12
OR f_id IN (SELECT s_to_f_id FROM subscription WHERE s_f_id = 12)
ORDER BY f_loginName, m_name;