I have a orders table as follows:
id | cust_id| status
1 25 1
2 25 1
3 25 0
4 26 0
5 26 1
6 26 0
7 27 1
8 27 1
9 27 1
I need two queries:
1. fetch all the 'cust_id' having at least one status 0.
eg. in above table I should get cust_id with two rows 25 and 26 because they have at least one 0 in 'status' column.
2. fetch all the 'cust_id' having all status 1.
eg. in above table I should get cust_id with one row 27 because it has all 1's in the 'status' column.
Assuming status is numeric
select distinct cust_id
from my_table where status = 0
select disctint cust_id
from my_table
where cust_id NOT in (select distinct cust_id
from my_table where status = 0 )
these are the two queries ..
if you need both result in same select you can use union
select distinct cust_id
from my_table where status = 0
union
select disctint cust_id
from my_table
where cust_id NOT in (select distinct cust_id
from my_table where status = 0 )
SELECT DISTINCT cust_id FROM orders WHERE status=0
UNION
SELECT DISTINCT cust_id FROM orders WHERE status=1
AND cust_id NOT IN (SELECT cust_id FROM orders WHERE status<>1)
Related
This is my table
id
c_id
number
3444
34
3377752
3446
35
3473747
3447
35
3532061
3454
37
3379243
3455
38
3464467
3456
38
3377493
I want to create a table which is show me not repeated value in a c_id column (repeat just 1 time). the result should be:
id
c_id
number
3444
34
3377752
3454
37
3379243
GROUP BY c_id, number
HAVING COUNT(cart_id) = 1
i tried this but it shows me repeated value again
SELECT ANY_VALUE(id) id,
c_id,
ANY_VALUE(`number`) `number`
FROM tablename
GROUP BY c_id
HAVING COUNT(*) = 1;
You can do it as follows :
select t.*
from _table t
inner join (
select c_id
from _table
group by c_id
having count(1) = 1
) as s on s.c_id = t.c_id;
Check it here : https://dbfiddle.uk/RlxxQ_05
Have three columns
ID User Quantity Date
1 x 0 2016-01-01
2 x 2 2016-01-02
3 x 0 2016-01-03
4 x 0 2016-01-04
5 xx 0 2016-01-01
6 xx 2 2016-01-02
7 xx 0 2016-01-03
8 xx 8 2016-01-04
9 xx 0 2016-01-06
10 xx 0 2016-01-04
Now How do i get user wise first of the latest sequence of 0 entry for x user ID=3,xx ID=9.
It's a bit complex, because we need to find the last not 0 entry for a name. (I would naturally tend to sort by date, but you said ID is what to go for.)
SELECT t1.name, max(t1.id) as check_id FROM table t1 WHERE quantity > 0 GROUP BY name
Now you can get the lowest number for each name that is higher than the given check_id:
SELECT name, min(t2.id) FROM table t2
JOIN (SELECT t1.name, max(t1.id) as check_id FROM table t1 WHERE quantity > 0 GROUP BY name) as a ON a.name=t2.name
WHERE t2.quantity = 0 AND t2.id > a.id
GROUP BY t2.name
There's one problem. It will exclude everyone with only 0 quantity. If this could happen you need another query with the following code (to get only 0 values)
SELECT name, min(id) as check_id, sum(quantity) as qty_sum FROM table
GROUP BY name
HAVING qty_sum = 0
As already mentioned, the first step is to identify the last dataset with a positive quantity by each user. The lowest ID of all following datasets would be what you seek. In other words, you need to look for MAX(ID) of all positive entries and apply MIN(ID) on all entries with a higher ID. In one query:
SELECT `User`, MIN(`ID`) FROM t `mainquery`
WHERE `ID` > (
SELECT MAX(`ID`) FROM t `subquery`
WHERE `Quantity` > 0 AND `mainquery`.`User` = `subquery`.`User`
GROUP BY `User`
)
GROUP BY `User`
With the data from your example, this query returns:
User MIN(`ID`)
x 3
xx 9
Test it here: http://sqlfiddle.com/#!9/3c487/1/0
Try This:
SELECT t1.user,min(t1.id) as ID,quantity,date from tablename t1
JOIN
(SELECT MAX(id) as qid,user FROM tablename WHERE Quantity>0 GROUP BY user) t2
ON t2.user=t1.user
WHERE t1.id>t2.qid
AND t1.Quantity=0
GROUP BY t1.user
I'm running a query that selects details of orders, and I want to see only the orders that have gone through multiple stages. My data looks like:
id | order_id | action
1 100 1
2 100 2
3 100 4
4 101 1
5 102 2
6 103 1
7 103 2
So that only the rows for order_id 100 and 103 will be selected. This needs to be nested in a larger query.
You can use a subquery to get the orders that had multiple stages:
SELECT order_id
FROM your_table
GROUP BY order_id
HAVING COUNT(*)>1
then you can join this result back to your table:
SELECT o.*
FROM yourtable AS o INNER JOIN (
SELECT order_id
FROM your_table
GROUP BY order_id
HAVING COUNT(*)>1
) dup ON o.order_id = dup.order_id
Use group by with count and having
select *,count(order_id) as total from table
group by order_id
having total > 1
you can try this query:
select * from your_table
where ( select count(*) from your_table internal_table
where your_table .order_id = internal_table.order_id
) > 1
Table A
Status
1
1
1
2
2
3
Table B
Status
1
1
2
2
2
2
3
3
How to Get following Result Table
Result Table
Status Count
1 5
2 6
3 3
Help me to build SQL Query to get Count in Result Table.
Try this
SELECT Status, COUNT(*) FROM
(SELECT a.Status FROM TableA AS a
UNION ALL
SELECT b.Status FROM TableB AS b) UN
GROUP BY Status
SELECT STATUS, COUNT(*) FROM
(SELECT STATUS FROM TABLE1
UNION ALL
SELECT STATUS FROM TABLE2) un
GROUP BY STATUS
FIDDLE
need to filter duplicate record from a particular selected records
SQL table site_metatag and i am using MySQL client version: 5.5.30
metatag_id | store_id | name
1 0 copyright
2 0 author
3 0 robots
4 0 googlebot
5 0 revisit-after
6 0 google-site-verification
9 1 google-site-verification
8 1 revisit-after
10 1 googlebot
11 1 robots
12 2 googlebot
13 2 robots
14 2 google-site-verification
need those record from store_id in 1 and 0 but name will be unquie like
distinct() function give uniqe all record , but i need record that not used more then 1 time
metatag_id | store_id | name
1 0 copyright
2 0 author
3 0 robots
i try
SELECT * FROM site_metatag v
where metatag_id NOT IN
(
select metatag_id from site_metatag p where v.name=p.name
)
AND v.store_id in (0,1)
but not working..
I think you want this:
SELECT
metatag_id, store_id, `name`
FROM
site_metatag v
group by `name`
Or this, if you want to group by number of occurrences(in this case 1 -> cnt = 1).
SELECT
metatag_id, store_id, v.`name`, a.cnt
FROM
site_metatag v
inner join
(select
`name`, count(*) as cnt
from
site_metatag
group by `name`
having cnt = 1) as a ON (v.`name` = a.`name`)
group by v.`name`
use distinct() function. see Distinct MYSQL
what about this:
SELECT * FROM site_metatag WHERE v.store_id in (0,1) GROUP BY name