Query using Mysql Update - mysql

I have two tables T and T1
T
id p o
1 47 1
2 47 2
3 47 25
T1
id p
1 47
2 48
3 49
I am looking to find a way to get T as the following table
id p o
1 47 1
2 47 2
3 47 0
If T.p in (select T1.p from T1) and the value of the field o is the max then update o
into 0.
I try the following query but it didn't work
Update T
SET T.o=0
WHERE T.P IN (select T1.p from T INNER join select T.p from T ON T.p=T1.p)
AND T.o In (select Max(T.o) from T)
For more details Sqlfiddle
Many thanks in advance .

SQL Fiddle
Update T
SET T.o=0
WHERE T.P IN (select T1.p from t1 )
AND t.o IN (SELECT * FROM(SELECT MAX(t.o) FROM t)x)

try something like this:
UPDATE T
SET T.o=0
WHERE T.p in (select T1.p from T1)
AND T.o In (select Max(T.o) from T)

Related

I need help to find solution for this query in MySQL?

I have MySQL table as follow:
id p_id c_id
1 11 1
2 11 2
3 11 3
4 12 1
5 12 3
6 13 1
7 13 2
I need a query that when c_id is 1 and 2, it should return 11 and 13 of p_id.
I have tried the following query:
SELECT DISTINCT p_id FROM `Table Name` where c_id in (1,2)
which returns: 11, 12, 13.
but I only need it to return: 11 , 13.
You can write your query as:
SELECT DISTINCT a.p_id
FROM table_name AS a
JOIN table_name AS b ON a.p_id=b.p_id
WHERE a.c_id ='1' AND b.c_id ='2';
It's a self-join on the table itself
SELECT DISTINCT a.p_id
FROM table_name AS a
JOIN table_name AS b ON a.p_id=b.p_id
WHERE a.c_id ='1' AND b.c_id ='2';
it worked fo me
Your query is checking for p_ids related to c_is 1 or 2. But what you want is 1 and 2. You can write this like this:
SELECT DISTINCT t1.p_id
FROM table_name as t1
JOIN table_name as t2
ON t1.id = t2.id
WHERE t1.c_id = 1 AND t2.c_id = 2;
SELECT x.id
FROM my_table x
WHERE other_column IN('a','b')
GROUP
BY x.id
HAVING COUNT(*) = 2;

How to join same table to check if a>b with 10

Table mytable
id numbers whereonly
1 2 1
2 35 1
3 22 1
4 20 1
5 3 1
6 70 1
7 80.15925 1
8 60 7
9 50 7
I need to order by numbers and to take id 1 to search until I found an id that have numbers row bigger with 10.
Desired result: 2, 20, 35, 70, 80.15925
Only where column whereonly is 1
Is there a way to do this?
You can give this a try:
SELECT DISTINCT t1.id AS id, t1.numbers AS numbers
FROM table AS t1
INNER JOIN table AS t2 ON t1.numbers > t2.numbers - 10
WHERE t1.whereonly = 1
GROUP BY t2.numbers
ORDER BY t1.numbers;
Here is the sqlfiddle.
Edit 1: As strawberry suggested
SELECT DISTINCT x.*
FROM mytable x
JOIN
( SELECT t2.numbers t2n
, MIN(t1.id) id
FROM mytable t1
JOIN mytable t2
ON t1.numbers > t2.numbers - 10
GROUP
BY t2.numbers
) y
ON y.id = x.id
ORDER BY x.numbers
WHERE x.whereonly = 1;
Here is the sqlfiddle.

Get distinct values in union all in hive

I have a table in hive that looks something like this
cust_id prod_id timestamp
1 11 2011-01-01 03:30:23
2 22 2011-01-01 03:34:53
1 22 2011-01-01 04:21:03
2 33 2011-01-01 04:44:09
3 33 2011-01-01 04:54:49
so on and so forth.
For each record I want to check that how many unique products has this customer bought within the last 24 hrs excluding the current transaction. So the output should look something like this -
1 0
2 0
1 1
2 1
3 0
My hive query looks something like this
select * from(
select t1.cust_id, count(distinct t1.prod_id) as freq from temp_table t1
left outer join temp_table t2 on (t1.cust_id=t2.cust_id)
where t1.timestamp>=t2.timestamp
and unix_timestamp(t1.timestamp)-unix_timestamp(t2.timestamp) < 24*60*60
group by t1.cust_id
union all
select t.cust_id, 0 as freq from temp_table t2
)unioned;
Just get all the rows for last 24 hours do a group by on custid and count(distinct productid) -1 as the output. Overall query would look something like this.
select cust_id, COUNT(distinct prod_id) - 1 from table_name where
unix_timestamp(t1.timestamp)-unix_timestamp(t2.timestamp) < 24*60*60
GROUP BY cust_id
*I am subtracting 1 here to exclude the latest transactionid of the user. (hope this is what you meant)
You can join to a derived table that contains the distinct # of products purchased in the past 24 hours for each customer/timestamp pair.
select t1.cust_id, t1.prod_id, t1.timestamp, t2.count_distinct_prod_id - 1
from mytable t1
join (
select t2.cust_id, t2.timestamp, count(distinct t3.prod_id) count_distinct_prod_id
from mytable t2
join mytable t3 on t3.cust_id = t2.cust_id
where unix_timestamp(t2.timestamp) - unix_timestamp(t3.timestamp) < 24*60*60
group by t2.cust_id, t2.timestamp
) t2 on t1.cust_id = t2.cust_id and t1.timestamp = t2.timestamp

Mysql using count in query

Say that I have two tables T and T1
T
id p o
1 47 1
2 48 2
3 49 25
T1
id p o
1 47 1
2 42 2
3 47 25
I am looking to insert rows from T1 into T if count(T1.p)>1
T
id p o
1 47 1
2 48 2
3 49 25
1 47 1
3 47 25
I tried the following query but it didn't work
insert into T(id , p,o)(SELECT T1.id , T1.p1,T1.l FROM T1
where SELECT count(*) FROM t1
GROUP BY t1.p
HAVING COUNT(*)>1)
For more details .
Any help will be appreciated .
To get those values into T you will have to find out who they are in T1 and JOIN them with T1 again, to get the right number of rows:
INSERT INTO T (id, p, o)
SELECT TT.*
FROM T1 TT
INNER JOIN (
SELECT id, p1, l
FROM T1
GROUP BY p1
HAVING COUNT(*) > 1
) a ON a.p1 = TT.p1;
sqlfiddle demo
How this works:
SELECT id, p1, l
FROM T1
GROUP BY p1
HAVING COUNT(*) > 1
Returns the p1 that appears more than once in the table. This returns p1 = 47. GROUP BY p1 HAVING COUNT(*) > 1 makes sure that for each p1, we only want the results that appear more than once.
Then, we do an inner JOIN with T1 again, to get all rows that have P1 = 47:
ID P1 L
1 47 1
3 47 25
Then you just INSERT this result in the destination table.
You have a couple of errors in your select.
This should get you going:
SELECT T1.id , T1.p1,T1.l
FROM t1
GROUP BY t1.p1
HAVING COUNT(*)>1
SQL Fiddle
EDIT: Updated the SQL Fiddle to include the insert.
insert into T SELECT T1.id , T1.p1,T1.l FROM T1
GROUP BY t1.p1
HAVING COUNT(t1.p1)>1
http://www.sqlfiddle.com/#!2/75c8e/1
Use dml on the left side
The below SQL should do what your looking for:
INSERT INTO T (id, p, o)
SELECT id, p1, l
FROM T1
WHERE p1 IN (
SELECT p1
FROM T1
GROUP BY p1
HAVING COUNT(*) > 1
);
SQL Fiddle - Final result

how is this query done in mysql?

Given the following table ...
ID USER NUM
1 1 69
2 2 75
3 3 7
4 1 31
5 2 18
6 3 70
7 1 12
8 2 23
9 3 42
... which query would return rows with the lowest NUM for each USER?
The result should look like ...
ID USER NUM
7 1 12
5 2 18
3 3 7
Can't wrap my head around this one! Assuming it has a GROUP BY, but everything I try fails... Any pointers?
SELECT t.*
FROM tablename t
INNER JOIN (SELECT user, MIN(num) num
FROM tablename
GROUP BY user) x ON t.user = x.user AND t.num = x.num
or
SELECT t1.*
FROM tablename t1
LEFT JOIN tablename t2 ON t1.user = t2.user AND t1.num > t2.num
WHERE t2.id IS NULL
SELECT ID , MIN(NUM) as MIN_NUM , USER FROM usertable GROUP BY USER
Have a look at demo http://sqlfiddle.com/#!2/ce2fd/1
HEre is another method. UPDATED WITH THE CORRECT REREFERENCE
SQLFIDDLE
Query
select b.id, a.user, a.minnum from
mytable as b
join
(select user, min(num) as minnum
from mytable
group by user
) as a
on b.user = a.user
where b.num = a.minnum
order by a.minnum asc
limit 3
;
Results:
ID USER MINNUM
3 3 7
7 1 12
5 2 18