i have a sales table which contain customer_id time_id
i want to count the products bought by a customer at one time_id.
i am running query
select customer_id,time_id,count(time_id) as count from sales where customer_id=2094 group by time_id.
this query runs and give the result but i have different customer_ids in another customer table then i run this query
select customer_id,time_id,count(time_id) as count from sales where customer_id in (select customer_id from customer) group by time_id.
but it is not showing exact result.
Related
Three table are given:
customer(cust_id, name, address, sales_id)
orders(order_id, cust_id,date, sales_id)
salesman(sales_id,commision)
and you have to write an MySQL query to "count the salesman by their order_id and date". Is the question is correct? if yes, how can I solve this.
The question is not clear, but the codes below will give you an idea of how to get aggregated results and you can play around / adjust based on your needs
select
date,
sales_id,
count(order_id) as total_orders_per_date_and_sales_id
from orders
group by 1,2
if you need the total salesman per date then you can do this:
select
date,
count(sales_id) as total_sales_man_per_date
from orders
group by 1
if one order has multiple sales owners, then you can find total salesman per order id
select
orders_id,
count(salesman) as total_sales_man_involved_per_order
from orders
group by 1
if you need total commission per salesman per date:
select
orders.date,
orders.sales_id,
sum(salesman.comission) as total_comssion_per_salesman_per_date
from orders
left join salesman
on orders.sales_id = salesman.sales_id
group by 1,2
total number of distinct salesman
select
count(distinct sales_id) as total_unique_salesman
from salesman
I need to write SQL query for "average number of particular product sold by date. On each day is sold min one product".
SELECT AVG (COUNT (PID))
FROM SOLD
GROUP BY DATE, PID;
P.S. PID means Product ID.
Is this query okay?
Should this give right answer?
Consider Using distinct count of date columns
SELECT PID,
COUNT(PID)/COUNT(distinct date_) as "Avg.Product Sold By Days"
FROM SOLD
GROUP BY PID;
You can try this sql query below. Basically, it will return the average number of 'SALES' for each product you have. It will group by each distinct product ID. Please provide us the data structure your of table and etc.
SELECT product_ID, trans_date
Sum(sales_of_product) / COUNT(DISTINCT sold_transaction) AS 'avg'
FROM SOLD
GROUP BY product_ID
Customer table with purchases in a date range
Having some challenge doing this.
Select all customers that made more than 2 purchases in an hour
starting from a given datetime eg.2016-01-01.13:00:00
Select all customers that made more than 2 purchases in a a day(24
hours) starting from a given datetime eg. 2016-01-01.15:30:05
Select count(name) from cust_table where count(name)
IN (BETWEEN (2016-01-01.13:00:00, 2016-01-01.14:00:00))
You need to get the identifiers (usernames or ID or so) for all users who have made more than two purchases. Something like the following should work:
SELECT name FROM purchases GROUP BY name HAVING COUNT(name) > 2
To get such rows where the purchases were made between some dates, just add a WHERE clause, so it becomes something like this
SELECT name FROM purchases WHERE pdate BETWEEN (2016-01-01.13:00:00) AND (2016-01-01.14:00:00) GROUP BY name HAVING COUNT(name) > 2
What this does is group the table rows by name with purchases between the specified dates. It then filters the groups using the HAVING COUNT(name) > 2 clause to retrieve only rows that appear more than twice.
For an ecommerce site I'm trying to run a query on BigQuery that gives me the number of times a customer viewed the product that was finally purchased and get that number for each purchase.
So for one purchase an example:
Visitor X with uniqueId '567890abcdef' purchased product 'A1234', how many times was this product viewed by this visitor in the browsing history.
I could probably get this result for one specific purchase, but I don't how to do this for all purchases from e.g. yesterday. Below i added the query to get all of the purchases. So i would like to know product views per fullvisitorid per productcode.
The first query below will get all bookings for a specific date.
The second query will get the amount of times a specific visitorid has viewed a specific product.
Basically what i would like to see is a query to 'loop through' for all the combinations of visitorid and productid of the purchased product and get me the times the product was viewed.
SELECT
fullvisitorid,
visitId,
visitStartTime,
date,
MAX(CASE WHEN hits.customdimensions.index = 16 THEN hits.customdimensions.value END) productCode
FROM
TABLE_DATE_RANGE([xxxxx.ga_sessions_], TIMESTAMP('2016-4-4'), TIMESTAMP('2016-4-4'))
WHERE
hits.transaction.transactionId IS NOT NULL
GROUP BY
fullvisitorid,
visitId,
visitStartTime,
date
SELECT
fullvisitorid,
MAX(CASE WHEN hits.customdimensions.index = 16 THEN hits.customdimensions.value END) productCode,
count(totals.hits)
FROM
TABLE_DATE_RANGE([xxxxxx.ga_sessions_], TIMESTAMP('2016-4-4'), TIMESTAMP('2016-4-4'))
where
(hits.customdimensions.index = 16 and hits.customdimensions.value contains productCode) and
fullvisitorid contains 'xxxxxxx'
GROUP BY
fullvisitorid
I'm stuck on crafting a MySQL query to solve a problem. I'm trying to iterate through a list of "sales" where I'm trying to sort the Customer IDs listed by their total accumulated spend.
|Customer ID| Purchase price|
10 |1000
10 |1010
20 |2111
42 |9954
10 |9871
42 |6121
How would I iterate through the table where I sum up purchase price where the customer ID is the same?
Expecting a result like:
Customer ID|Purchase Total
10 |11881
20 |2111
42 |16075
I got to: select Customer ID, sum(PurchasePrice) as PurchaseTotal from sales where CustomerID=(select distinct(CustomerID) from sales) order by PurchaseTotal asc;
But it's not working because it doesn't iterate through the CustomerIDs, it just wants the single result value...
You need to GROUP BY your customer id:
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID;
Select CustomerID, sum(PurchasePrice) as PurchaseTotal FROM sales GROUP BY CustomerID ORDER BY PurchaseTotal ASC;
Just by having a little Google search, I managed to find a page doing exactly what you're doing (I think). I have tailored the query below to fit your circumstance.
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID
ORDER BY PurchaseTotal ASC
Link to Page with Tutorial on SQL Groups