Currently I have the below query to count the number of rows with a distinct (customer username and store id) of the purchase of a certain item. How can I also count the total(all) rows where item_id = 3 in the same query?
SELECT count(*)
FROM (
SELECT DISTINCT customer_username, store_id
FROM transactions
WHERE item_id = 3)
You might try something like this:
SELECT COUNT(*), SUM(customer_cnt) FROM (
SELECT customer_username, store_id, COUNT(*) AS customer_cnt
FROM transactions
WHERE item_id = 3
GROUP BY customer_username, store_id
) t;
Try removing DISTINCT in your query:
SELECT count(*)
FROM (
SELECT customer_username, store_id
FROM transactions
WHERE item_id = 3)
Another way is a cross join of 2 inline views:
select x.distinct_rows, y.total_rows
from (select count(distinct concat(user_name, store_id)) as distinct_rows
from transactions
where item_id = 3) x
cross join (select count(*) as total_rows
from transactions
where item_id = 3) y
Related
Basically I have a table all customer will have a default row with customer_group_id=0
But some of this customer will belong to customer_group_id=1
When that happen a new row is created for customer with customer_group_id=1 therefore now I have 2 rows for same customer but different customer_group_id.
Now when I fetch the data I need first to select * from customer table where customer_group_id =1 but if doesn't exist give me then with customer_group_id = 0 which is the default, and continue until it returns all data.
Anyone know the best way to achieve this fast?
UPDATE: screen shoot show 2 rows with same customer_id different customer_group_id:
I need to one or the other no both so hierarchy is: if customer_group_id=1 exist then return that row and ignore the ohter otherwise return default which is customer_group_id=0
My full query:
SELECT `main_table`.*, `secondTable`.* FROM `customer` AS `main_table`
LEFT JOIN `customer_group` AS `secondTable` ON main_table.customer_id = secondTable.customer_id
WHERE (secondTable.customer_group_id = '1' )
AND (`secondTable`.`is_active` = '1')
If you have only two groups, then aggregation is simple:
select customer_id, max(customer_group_id)
from t
group by customer_id;
In MySQL 8+, you can implement a more customer prioritization using row_number():
select t.*
from (select t.*,
row_number() over (partition by customer_id order by customer_group_id desc) as seqnum
from t
) t
where seqnum = 1;
What you can do is fetch all the rows for the customers with customer_group_id=1 and then use UNION ALL to fetch the rows of the customers that do not have any row with customer_group_id=1 by using NOT EXISTS:
select * from tablename
where customer_group_id=1
union all
select * from tablename t
where t.customer_group_id=0
and not exists (
select 1 from tablename
where customer_id = t.customer_id and customer_group_id=1
)
For the rows with customer_group_id = 0 verify that there are no rows with customer_group_id = 1 for the same customer_id. You can use a NOT EXISTS subquery:
SELECT c.*, g.*
FROM customer AS c
JOIN customer_group AS g ON c.customer_id = g.customer_id
WHERE NOT EXISTS (
SELECT *
FROM customer_group AS g2
WHERE g2.customer_id = c.customer_id
AND g2.customer_group_id = 1
AND g.customer_group_id = 0
)
I am trying this query:
SELECT * FROM heath_check where cid = '1' and eid in('3','5','7','1','6')
My table structure:
I want distinct eid but all other data as it is. For example I have two entries with an eid of 1 my query fetched both, but I want one which is in the second column.
SELECT *
FROM heath_check AS hc
INNER JOIN (
SELECT MAX(id) AS lastId
FROM heath_check
WHERE cid = '1' and eid in('3','5','7','1','6')
GROUP BY eid) AS lastIDs
ON hc.id = lastIDs.lastId
;
You need a subquery, like the above, to find the records you want for each value. If you had wanted the first ones, you could use MIN(id) instead; if you cannot count on sequential ids, it becomes much more complex with use of potentially non-unique timestamps (if they are even available).
Create a RowNumber grouped by eid and filter the RowNumber = 1 to get the expected result.
SELECT id, eid, cid,weight, s_blood_pressure
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY eid ORDER BY id DESC) AS RowNumber
FROM heath_check
WHERE cid = '1' AND eid IN ('3','5','7','1','6')
) A
WHERE RowNumber = 1
I am trying to retrieve unique values from the table above (order_status_data2). I would like to get the most recent order with the following fields: id,order_id and status_id. High id field value signifies the most recent item i.e.
4 - 56 - 4
8 - 52 - 6
7 - 6 - 2
9 - 8 - 2
etc.
I have tried the following query but not getting the desired result, esp the status_id field:
select max(id) as id, order_id, status_id from order_status_data2 group by order_id
This is the result am getting:
How would i formulate the query to get the desired results?
SELECT o.id, o.order_id, o.status_id
FROM order_status_data2 o
JOIN (SELECT order_id, MAX(id) maxid
FROM order_status_data2
GROUP BY order_id) m
ON o.order_id = m.order_id AND o.id = m.maxid
SQL Fiddle
In your query, you didn't put any constraints on status_id, so it picked it from an arbitrary row in the group. Selecting max(id) doesn't make it choose status_id from the row that happens to have that value, you need a join to select a specific row for all the non-aggregated columns.
Like so:
select d.*
from order_status_data2 d
join (select max(id) mxid from order_status_data2 group by order_id) s
on d.id = s.mxid
Try this Query.This will help you
SELECT id ,orderid,statusid
FROM table_name
WHERE id IN
(
SELECT max(id) FROM table_name GROUP BY orderid
)
ORDER BY statusid
You can refer this Sql_Fiddle_link which uses your example.
This is structure of product table.
Currently have more 1 million records.
I have performance issue when I use query group by & order by.
Query:
SELECT product_name FROM vs_product GROUP BY store_id ORDER BY id DESC LIMIT 2
How to improve this query to perform faster? I indexed the store_id, ID is primary key.
SELECT x.*
FROM my_table x
JOIN (SELECT store_id, MAX(id) max_id FROM my_table GROUP BY store_id) y
ON y.store_id = x.store_id
AND y.max_id = x.id
ORDER
BY store_id DESC LIMIT 2;
A hacky (but fast) solution:
SELECT product_name
FROM (
SELECT id
FROM vs_product
GROUP BY store_id DESC
LIMIT 2) as ids
JOIN vs_product USING (id);
How it works:
Your index on store_id stores (store_id, id) pairs in ascending order. GROUP BY DESC will make MySQL read the index in reverse order, that is the subquery will fetch the maximum ids for each store_id. Then you just join them back to the whole table to fetch product names.
Take notice, that the query will fetch two product names for the store ids with the maximum values.
You want a query like this:
select p.*
from product p join
(select store_id, max(id) as maxid
from product p
group by store_id
) psum
on psum.store_id = p.store_id and p.id = maxid
You don't have date in any of the tables, so I'm assuming the largest id is the most recent.
I have the following table structure in my db (MySQL):
id group_id item_id project_id user_id
Users can have multiple entries withing the same project. How do I count unique users withing a particular project (minus project owner id)?
SELECT COUNT(user_id) AS cnt
FROM myTable
WHERE project_id = $myProject
AND user_id != 3
GROUP BY user_id
This looks right but I don't believe I'm getting the right results. Am I missing something?
Select Count(Distinct user_id)
From MyTable
Where project_id = $myProject
And user_id != 3
Add DISTINCT to your COUNT and eliminate the GROUP BY.
SELECT COUNT(DISTINCT user_id) AS cnt
FROM myTable
WHERE project_id = $myProject
AND user_id != 3
You don't need a GROUP BY clause for this.
SELECT COUNT(DISTINCT user_id) AS cnt
FROM myTable
WHERE project_id = $myProject
AND user_id != 3;
If you want to list the member count for each group in the same query, you can GROUP BY project_id:
SELECT COUNT(DISTINCT user_id) AS cnt
FROM myTable
GROUP BY project_id;
By grouping on user_id as you do now, every row in the resultset will contain 1.
Try Distinct?
SELECT DISTINCT COUNT(user_id) AS cnt FROM myTable WHERE project_id = $myProject AND user_id != 3