I have a table like this for customer order and feedback some questions.table_name:customer_database
----------
ID NAME PHONE ORDER_NO ORDER_VALUE ANSWER DATE
----------
1 RAM 873 5 500 Super 1/1/2019
2 RAJ 876 1 400 super 1/1/2019
3 RAM 873 5 500 Bad 1/1/2019
4 RAM 873 2 100 Good 30/12/2018
I want the result as counting the same set of unique date row as 1 like
----------
ID NAME PHONE Total_visit total_order
----------
1 RAM 873 2 600
2 RAJ 876 1 100
i want to count as 1 for a set of answer like 1 and 3.
It is actually very simple group by query
select NAME, Phone count(*) total_visit, sum(order_value) total_order
from your_Table
group by NAME, Phone
Finally, I found the answer to my own by an inner query
SELECT NAME,PHONE,count(PHONE) as total_visit,SUM(ORDER_VALUE) as total_order_value
from (SELECT NAME,PHONE,ORDER_VALUE FROM customer_database GROUP BY PHONE,DATE) as qry
GROUP BY PHONE
Related
I have a table of Books, that contains the following columns:
Book_Id User_Id
001 1
002 2
001 1
004 2
005 3
006 3
007 2
008 2
009 1
Where :
Book_Id - identifier of a book that a user read; User_Id - identifier
of a reader/user.
Let's assume that User1 read books three times, but 2 of them were same, so the user 1 read 2 distinct books (001 and 009). User 2 read 4 distinct books, while user 3 read 2 distinct books.
In overall, there are 2 users that read 2 distinct books, and 1 user that read 4 distinct books.
The output expected is as below:
Distinct_Books_Count --- User_Count
2 2
4 1
I tried the following:
SELECT COUNT(DISTINCT Book_Id), COUNT(User_Id) FROM Books GROUP BY
User_Id
But I receive the following table:
Distinct_Books_Count User_Count
2 3
4 4
2 2
So any alternative solution or changes?
I call this a "histogram of histograms" query. You can do it using two group bys:
SELECT num_books, COUNT(*)
FROM (SELECT b.User_Id, COUNT(DISTINCT Book_Id) as num_books
FROM Books b
GROUP BY User_Id
) b
GROUP BY num_books
ORDER BY num_books;
I have a table like this
id productName amount
1 Abc 10
2 xyz 20
3 Abc 10
4 Abc 10
5 Abc 10
6 Abc 10
7 xyz 20
8 xyz 20
9 xyz 20
10 xyz 20
I want to sum Amount and Count No. of rows for each products in mysql. Output like below
productName noQty totalAmount
ABC 5 50
xyz 5 100
select product_name,sum(amount)as totalamount,count(productName)as noQty
from table_name
group by productname
by using count you can get COUNT from your table and using SUM you can get sum of all same product name.
SELECT productName,
count(productName) AS noQty,
SUM(amount) AS totalAmount
FROM tableName
GROUP BY productName
select productname,count(productname) as qty,sum(amount)
from table_name
group by productname
I need to count unique number of items in the order.
Please take a look at the following example:
tablel
Order item
1 111
1 111
1 123
1 341
1 341
1 861
2 999
2 831
3 731
3 731
3 721
3 777
Expected result:
Order | Item Count
-------------------
1 | 4
2 | 2
3 | 3
SELECT order, COUNT(DISTINCT item)
FROM table1
GROUP BY order
A bit or word play.
I guess you mean the number of unique items in an order.
select Order, count(item)
from table
group by order
Imagine you have a members with distinct member_ids and dates of service
you now need to order the dates of service in ascending order and return the order of these dates in another column (date_count). the final result will look like this:
memberid name date date_count
122 matt 2/8/12 1
122 matt 3/9/13 2
122 matt 5/2/14 3
120 luke 11/15/11 1
120 luke 12/28/14 2
100 john 1/12/10 1
100 john 3/2/12 2
100 john 5/30/12 3
150 ore 5/8/14 1
150 ore 9/9/14 2
here is the query that works but does not return the date_count in ranking (1,2,3) order. This instead returns the same number for date_count, not sure why the num
memberid name date_count
122 matt 3
122 matt 3
122 matt 3
120 luke 5
120 luke 5
120 luke 5
100 john 6
100 john 6
150 ore 2
150 ore 2
SELECT A.MEMBERID, A.NAME,A.DATE, COUNT(B.DATE) AS DATE_COUNT FROM #WCV_COUNTS A
INNER JOIN #WCV_COUNTS B
ON A.MEMBERID <= B.MEMBERID
AND A.MEMBERID= B.MEMBERID
GROUP BY A.MEMBERID, A.NAME, A.DATE
ORDER BY A.MEMBERID
Thanks for help in advance!
Use ROW_NUMBER()
SELECT memberid, name, date,
ROW_NUMBER() OVER (PARTITION BY memberid ORDER BY date) AS date_count
FROM #WCV_COUNTS
ORDER BY memberid, date
I have the table like this
id rate_user_id content_id points category_id
1 100 1 5 1
2 101 1 3 1
3 100 2 8 1
4 103 2 11 1
So I want to looking highest point of content in this category 1
Content_id 2 = 19 points .
U can do this by
select content_id,sum(points) from user group by content_id order by sum(points) desc limit 1;
Thanks
have a look at the command MAX.
http://technet.microsoft.com/en-us/library/ms187751.aspx
Something like
SELECT MAX(Pts) FROM
(SELECT SUM(points) Pts GROUP BY content_id) A