sql Summarising table columns by e-mail - mysql

I have a table (SQL) where customers can have different statuses. Each status is noted with a date in a separate column.
Currently, however, the customer record is not enriched with the new status including the time stamp. As soon as a customer receives a new status, it is entered in the table.
Now I would like to merge these different timestamps of the individual customers into one row in a sql query and export it as a csv.
The email address serves as the identifier here.
How can I write the sql query so that I have the statuses of the customer abc#gmx.net in one row?
I look forward to your feedback.

One way to do it is to join your table multiple times. here is an example fiddle
http://sqlfiddle.com/#!9/ca5f74/3
select t.email,t1.date1,t2.date2
from t
inner join t as t1 on t.email = t1.email and t1.date1 is not null
inner join t as t2 on t.email = t2.email and t2.date2 is not null
group by t.email,t1.date1,t2.date2
email date1 date2
a 1/1/2011 2/2/2022

You can use aggregation:
select email, max(col1), max(col2)
from t
group by email;

Related

Generating SQL Query

have the following DB structure in MySQL:
Table1: ORDER
With Attributes
ID (PRIMARY KEY)
DELIVERY_DATE
CUSTOMER_ID
RESTAURANT_ID
ORDER_VALUE
Table2 : CUSTOMER
With Attributes
ID (PRIMARY KEY)
NAME
LOGIN
Table 3: Restaurant
ID (PRIMARY KEY)
NAME
CITY
I want to generate a db query where I select the customer_ID, login, Delivery_date, Sequence number, And the number of days from the last order to the second to last order.
I am having trouble using Inner join and generating the last two columns of my query:
Sequence: Each user may have ordered any number of dishes, I want to show a number three if it is the third time he orders, I should be able to get it from his customer_ID and the Order_ID
DateDiff from last order: I am having trouble getting the last and the second to last order and doing a dateDIFF and populating the column
I have so far constructed the following query:
SELECT customer_id,
login,
delivery_date,
sequence,
Datediff(second_to_last_order, last_order)
FROM order AS t1
INNER JOIN customer AS t2
ON t1.customer_id = t2.id
INNER JOIN restaurant AS t3
ON t1.restaurant_id = t3.id;
But it is obviously incomplete but i am stuck, any suggestions?
If this is MySQL Server query as you mentioned then I'd suggest you give the following a go;
SELECT CUSTOMER_ID, LOGIN, DELIVERY_DATE, SEQUENCE, DATEDIFF((SELECT MAX(z.ORDER_DATE) FROM ORDER AS z WHERE z.CUSTOMER_ID = t1.CUSTOMER_ID), (SELECT MAX(x.ORDER_DATE) FROM ORDER AS x WHERE x.CUSTOMER_ID = t1.CUSTOMER_ID AND x.ID != (SELECT MAX(x.ID) FROM ORDER AS x2 WHERE x2.CUSTOMER_ID = t1.CUSTOMER_ID)))
FROM ORDER AS a
INNER JOIN CUSTOMER AS t2 ON t1.CUSTOMER_ID = t2.ID
INNER JOIN RESTAURANT AS t3 ON t1.RESTAURANT_ID = t3.ID
ORDER BY t1;
I would advice that it's good practise to not call a table "ORDER" as this is a keyword used by MySQL, you could instead simple call the table "ORDERS" to avoid the issue.
This should do what you are asking for, if you have any issue please post any error text so we can better understand what you are facing.

MYSQL Query select only records with latest status as Positive

Hi I have a table containing records of status of each member of an organization. I would like to find out who among the members are still active based on the latest status provided in the table.Hee is a sample of records in the table:
You can get the last status for each name using a subquery in the where clause:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.name = t.name);
You can use an additional conditional expression to determine who is active.
SELECT * FROM YourTable WHERE Status='Active';

MySQL delete duplicate records but keep one based on date

how to delete all the records with same email address but keep the latest one by date? the date formate in MySQL is "0000-00-00". I have the follow query:
delete from customer
where date not in (
select max(data)
from customer
group by email)
seems not working.
and can you please show how to just keep one record based on nothing, since my records don't have id.
Join with a subquery that gets the latest date for each customer.
DELETE c1 FROM customer AS c1
JOIN (SELECT email, MAX(date) AS maxdate
FROM customer
GROUP BY email) AS c2
ON c1.email = c2.email AND c1.date != c2.maxdate

SQL select all fields from one table along with the sum of all values from another table matching a given condition [duplicate]

This question already has answers here:
MySQL Sum column IF ID is in another table query
(1 answer)
select sum qty from 2 tables
(2 answers)
Closed 5 years ago.
I have two tables: customers and orders.
Both id and email are unique in customers whereas uid and price can have duplicates in orders.
I want to select all fields from customers where email = 'john#doe.com', along with the total price this user paid from orders. If john doe didn't order anything yet (i.e. there is 0 record in orders where uid matches user id), I still need to return the fields list for this user, but the sum of the prices paid must be zero.
Here is what I tried:
SELECT t1.id, t1.email,
t2.uid, (SELECT SUM(t2.price)) AS total_price
FROM customers t1, orders t2
WHERE t1.email = '$email' AND t2.uid = t1.id
Problem is this query only works if there is at least one record in orders because the check for t2.uid = t1.id is in the where clause.
If there is no records in orders having the same uid as the customer id, the query will return NULL, which is not what I want.
How can I always return all fields from customers along with the sum of all prices paid in orders even if there is no records in orders matching customers.id ?
Here is an example showing the desired output:
I simple method is a correlated subquery:
SELECT c.*,
(SELECT COALESCE(SUM(o.price), 0)
FROM orders o
WHERE o.u_id = c.id
) AS total_price
FROM customers c
WHERE c.email = '$email';
Hints for writing better queries:
Learn to use parameters instead of munging query strings with parameter values. Your WHERE should be more like WHERE c.email = ?.
Your table aliases should be abbreviations for table names.
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
When your data has no orders for a customer, it returns an empty set with no rows. That is different from NULL, which means an indeterminate value.

Joining two tables in mysql - One to many relationship

I have 2 tables in mysql - User (user_id, first_name ....) and login_history(user_id, login_time)
Every time an user loges in, system records the time in login_history.
I want to run a query to fetch all the fields from the users table and the latest login time from login_history . Can anyone help please?
You have to use a join then :
SELECT *, login_history.login_time
FROM User
INNER JOIN login_history
ON User.user_id=login_history.user_id;
And this query gonna give you, all the columns of User and the login_time.
SELECT t1.col1
,t1.col2
,[...repeat for all columns in User table]
,max(t2.login_time)
FROM user t1
INNER JOIN login_history t2 ON t1.user_id = t2.user_id
GROUP BY t1.col1
,t1.col2
,[..repeat for all columns in User table]
This should work, assuming login_time is stored in a sane data type and/or format.
Following are 2 queries that can help you out to select latest login time with user details
SELECT * FROM User C,login_history O where C.user_id=O.user_id order by O.login_time desc limit 1
or
SELECT * FROM User C,login_history O where C.user_id=O.user_id and ROWNUM <=1 order by O.login_time desc