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
Related
I have two tables.
Order table - this table shows the orders completed in 5 delivery days (Thur, Fri, Sat, Sun, Mon). There are fixed 5 delivery Days. This table has order_id, cust_id, DeliveryDay, status (completed, processing, etc)
2nd tables - selection tables- It shows the number of products a subscriber selected to be delivered in that particular week. They can select from any of 20 products in that week. The number of products are the same but, products get changed every week. Now in this table, we do have a selection for all the people who might have selected 4-5 weeks before as well but now they might have canceled the subscription.
Selection table have - Cust_id, Product_id, ProductSlot, startDate(Thursday, as weeks, starts from Thursday).
So I am interested in knowing the actual number of selections for only those people who had actually got their order delivered (status in order table - Completed)in that particular week. How can I add a condition that if the order is completed within those 5 days(week in selection table) and then count the selections by product Id?
Select product_id, cust_id, startDate, Number
from selections S
Group by product_id
join orders O on S.cust_id = O.cust_id
where orders.status in('completed', processing)
this query is giving the wrong result because it's not taking the delivery dates into consideration.
Selection table -
Order Table -
Output -
only count the completed orders within delivered in those 5 days(Thu, Fri, Sat, Sun, Mon) and ignore any failed orders and its product selections. IN selection table a customer with cust_id 1239 has selected 2 products but his order has failed status in order table so don't count his products.
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
I have a table of sales and a table of sales items. Each sale can have multiple items. The sale also has a sales agency and its the agency ID that is used to search (in this example agency id 8).
So the pseudo query is count how many of a specific product type (in the items table are found for a specific agency between 2 dates.
Table layout
agency --->sale--->item1
|-->item2
My query is
SELECT (SELECT COUNT(DISTINCT(sale_id))
FROM sales_order_items AS si
WHERE si.sale_id = s.id AND si.product_type ='1')as COUNT
FROM sales as s
JOIN agency as a ON s.sales_person = a.agency_id
WHERE s.created >= '2018-01-01 00:00:00' AND s.created <= '2018-01-08 23:59:59' AND a.agency_id = '8'
GROUP BY s.sales_person
If I run this, I get
COUNT
1
and if I remove the GROUP BY I get
COUNT
1
1
1
0
1
0
BUT I want the SUM of the numbers so in this example above ...4 items found!
I am using the DISTINCT because there could be many items on the same sale of the product type i am searching for but I just want to find out how many sales have THAT PRODUCT TYPE rather than a count of items on the order. Where am I going wrong?
Help appreciated
Keith
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.
I have a product that sells monthly and I have the user's email
I want to see the total sum purchased per month per email.
Also the total amount purchased per email for all months combined
Try Like This :
I understand question like this
select email,sum(amount),'2' as ord from (
select email,sum(amount) as amount ,month(d1) from t1 group by
month(d1),email) as t group by email
union all
select email,sum(amount) as amount,
'1' as ord from t1 group by month(d1),email
order by ord
This query return first show sum of amount per month and email after that show sum of amount per email