i have following database
=================================
**id(PK) orderid email_content**
----------------------------------------------
1 6544 complain regarding service
2 6544 request for replacement
3 6544 complain for late delievery
4 9822 thanking note
5 5762 faulty product
6 5762 complain for
what i need is no of time interaaction per order
that is for each orderid , how many time email is present
for example for 6544 order4 id we have 3 emails..and like wise
orderid no_of_iteraction
-------------------------------
6544 3
9822 1
5762 2
please suggest appropriate mysql query
SELECT count(id) as no_of_iterations, orderid from mytable group by orderid
Try this:
SELECT orderid, COUNT(orderid) no_of_iteraction
FROM tblTemp
GROUP BY orderid
OR
As per your request using SUM function
SELECT orderid, SUM(1) no_of_iteraction
FROM tblTemp
GROUP BY orderid
OR
SELECT orderid, SUM(cnt)
FROM (SELECT orderid, 1 cnt FROM tblTemp ORDER BY orderid) AS A
GROUP BY orderid
Related
I have a table named emails which looks like this:
customer_id
email_template_id
send_time
order_type
1
1
2021-01-10
1
2
1
2021-01-10
1
1
2
2021-02-10
2
3
1
2021-03-10
1
2
2
2021-03-10
2
1
3
2021-04-10
1
I want to order the data by email_template_id which will be given on the web page (can be 1,2,3...). Whenever a person clicks on sort by (template_id = 2), I want to retrieve the data order by template_id then by date, but still get the unique customer_id of course. The result should look something like this:
customer_id
email_template_id
send_time
order_type
1
2
2021-02-10
2
2
2
2021-03-10
2
3
1
2021-03-10
1
I have tried this but I am getting duplicate rows.
select distinct customer_id as cust,order_type,email_template_id,send_time
from email_order
ORDER by FIELD(email_template_id,2,1,3,4,5,6),send_time desc
I tried using subquery to select distinct customer_id
select distinct(t1.customer_id) from
(select distinct customer_id as cust,email_template_id
from email_order
ORDER by FIELD(email_template_id,2,1,3),send_time desc) as t1
It works when I only select (t1.customer_id), when ever I select order_type or send_time from t1, it no longer shows unique customer_id, but shows the data according to send_time desc etc
Your help and time will be highly appreciated!
If your have an older version you can use the query below:
select customer_id, email_template_id, send_time, order_type
from ( select customer_id, email_template_id, send_time,order_type
from emails order by case when email_template_id=2 then -1 else email_template_id end ) as e
group by customer_id;
SQLfiddle working on MySQL 5.6 : http://sqlfiddle.com/#!9/d55f4d/1
But if the version is MariaDB, i think not in your case you should apply limit inside the subquery.
The images below are example in 10.4.17-MariaDB version
ROW_NUMBER should work well here:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_id
ORDER BY FIELD(email_template_id, 2, 1, 3, 4, 5, 6)) rn
FROM email_order
)
SELECT customer_id, email_template_id, send_time, order_type
FROM cte
WHERE rn = 1;
Given the following table transactions, which records the IDs of sellers and buyers who had a transaction, I would like to determine the user who was involved in the highest number of transactions and the number of transactions that user engaged in.
seller_id
buyer_id
date
1
4
2020-01-02
2
1
2020-01-03
3
2
2020-02-16
4
2
2020-02-22
4
3
2020-03-05
The desired output is this:
ID
n_trans
2
3
4
3
Because user 2 had a total of 3 transactions (1 as seller, 2 as buyer) and user 4 also had 3 transactions (2 as seller, 1 as buyer). It can be assumed that a user cannot be a buyer and seller in the same transaction, and that each buyer-seller combination is not duplicated.
What SQL query will get me this? I would not find any similar questions online. Thanks in advance!
You can unpivot, aggregate, and use window functions:
select id, cnt
from (select id, count(*) as cnt,
rank() over (order by count(*) desc) as seqnum
from ((select seller_id as id, date from t) union all
(select buyer_id, date from t)
) i
group by id
) i
where seqnum = 1;
I have an SQL table that stores reports. Each row has a customer_id and a building_id and when I have the customer_id, I need to select the latest row (most recent create_date) for each building with that customer_id.
report_id customer_id building_id create_date
1 1 4 1553561789
2 2 5 1553561958
3 1 4 1553561999
4 2 5 1553562108
5 3 7 1553562755
6 3 8 1553570000
I would expect to get report id's 3, 4, 5 and 6 back.
How do I query this? I have tried a few sub-selects and group by and not gotten it to work.
If you are using MySQL 8+, then ROW_NUMBER is a good approach here:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_id, building_id
ORDER BY create_date DESC) rn
FROM yourTable
)
SELECT
report_id,
customer_id,
building_id,
create_date
FROM cte
WHERE rn = 1;
If there could be more than one customer/building pair tied for the latest creation date, and you want to capture all ties, then replace ROW_NUMBER with RANK, and use the same query.
Another variation:
SELECT a.*
FROM myTable a
WHERE a.create_date = (SELECT MAX(create_date)
FROM myTable b
WHERE b.customer_id = a.customer_id
AND b.building_id = a.building_id)
Can try doing a search for "effective dated records" to see various approaches.
I have a table with following format -
Customer_id Purchase_date
c1 2015-01-11
c2 2015-02-12
c3 2015-11-12
c1 2016-01-01
c2 2016-12-29
c4 2016-11-28
c4 2015-03-15
... ...
The table essentially contains customer_id with their purchase_date. The customer_id is repetitive based on the purchase made on purchase_date. The above is just a sample data and the table contains about 100,000 records.
Is there a way to partition the customer based on pre-defined category data
Category Partitioning
- Category-1: Customer who has not made purchase in last 10 weeks, but made a purchase before that
- Category-2: Customer who as not made a purchase in last 5 weeks, but made purchase before that
- Category-3: Customer who has made one or more purchase in last 4 weeks or it has been 8 weeks since the first purchase
- Category-4: Customer who has made only one purchase in the last 1 week
- Category-5: Customer who has made only one purchase
What I'm looking for is a query that tells customer and their category -
Customer_id Category
C1 Category-1
... ...
The query can adhere to - oracle, postgres, sqlserver
From your question it seems that a customer can fall in multiple categories. So lets find out the customers in each category and then take UNION of the results.
SELECT DISTINCT Customer_Id, 'CATEGORY-1' AS Category FROM mytable GROUP BY
Customer_Id HAVING DATEDIFF(ww,MAX(Purchase_date),GETDATE()) > 10
UNION
SELECT DISTINCT Customer_Id, 'CATEGORY-2' AS Category FROM mytable GROUP BY
Customer_Id HAVING DATEDIFF(ww,MAX(Purchase_date),GETDATE()) > 5
UNION
SELECT DISTINCT Customer_Id, 'CATEGORY-3' AS Category FROM mytable GROUP BY
Customer_Id HAVING DATEDIFF(ww,MAX(Purchase_date),GETDATE()) < 4 OR
DATEDIFF(ww,MIN(Purchase_date),GETDATE()) =8
UNION
SELECT DISTINCT Customer_Id, 'CATEGORY-4' AS Category FROM mytable WHERE
DATEDIFF(ww,Purchase_date,GETDATE())<=1 GROUP BY Customer_Id having
COUNT(*) =1
UNION
SELECT DISTINCT Customer_Id, 'CATEGORY-5' AS Category FROM mytable GROUP BY
Customer_Id HAVING COUNT(*) =1
ORDER BY Category
Hope this serves your purpose.
Thanks
you can use something like this
with myTab as (
SELECT Customer_id ,MIN(Purchase_date) AS Min_Purchase_date,MAX(Purchase_date) AS Max_Purchase_date
, SUM(CASE WHEN Purchase_date>= DATEADD(WEEk ,-1,GETDATE()) THEN 1 ELSE 0 END ) AS Count_LastWeek
, COUNT(*) AS Count_All
FROM Purchases_Table
GROUP BY Customer_id
)
SELECT Customer_id
, CASE WHEN Max_Purchase_date < DATEADD(WEEK,-10,GETDATE()) THEN 'Category-1'
WHEN Max_Purchase_date < DATEADD(WEEK,-5,GETDATE()) THEN 'Category-2'
WHEN Max_Purchase_date >= DATEADD(WEEK,-4,GETDATE())
OR DATEDIFF(WEEK, Min_Purchase_date,Max_Purchase_date) >= 8 THEN 'Category-3'
WHEN Count_LastWeek = 1 THEN 'Category-4'
WHEN Count_All = 1 THEN 'Category-5'
ELSE 'No Category'
END
FROM myTab
Ok so i have this query
SELECT *
FROM configure_system
WHERE EstimateID='EDB95PY0Z2' AND StepID=1
which will return the following 8 rows...i need a query that only returns all the distinct productID and the total price and the quantity of them ...so for example
i want something like this
ProductID ProductPrice TotalPrice quantity
2 575 1175 2
3 839 1678 2
9 1349 4047 3
12 1699 1699 2
I tried using the distinct query and count but have not been successful
SELECT ProductID ,
ProductPrice ,
COUNT(*) * ProductPrice AS TotalPrice,
COUNT(*) AS quantity
FROM configure_system
WHERE EstimateID='EDB95PY0Z2'
AND StepID =1
GROUP BY ProductID,
ProductPrice
You need to look at the "GROUP BY" clause, and "aggregate functions"
SELECT ProductID,ProductPrice,SUM(ProductPrice),COUNT(*)
FROM configure_system
WHERE EstimateID='EDB95PY0Z2' AND StepID=1
GROUP BY ProductID,ProductPrice
It will be sth like this
SELECT productId
,count(*)
,count(*)*price
FROM [Table_1]
GROUP BY productId,Price
HAVING productId = 'blabla'