i have 2 tables, this is my fiddle https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=7009f83d39d688e38aceb781b7fdc903
CREATE TABLE users (
ID int(10) PRIMARY KEY NOT NULL,
email varchar(255));
CREATE TABLE order_match_detail (ID int(10) PRIMARY KEY NOT NULL,
createdBy int(11),
price decimal(10,2));
INSERT INTO users(ID, email)
SELECT 1, 'testing1#gmail.com' UNION ALL
SELECT 2, 'testing2#gmail.com' UNION ALL
SELECT 3, 'testing3#gmail.com' UNION ALL
SELECT 4, 'testing1#gmail.com' UNION ALL
SELECT 5, 'testing3#gmail.com';
INSERT INTO order_match_detail (ID, createdby, price)
SELECT 1, 1, 2000 UNION ALL
SELECT 2, 1, 2000 UNION ALL
SELECT 3, 2, 2000 UNION ALL
SELECT 4, 2, 3000;
select * from users;
select * from order_match_detail;
with users.id = order_match_detail.createdby
i want to find out the users_id where have same email, with this query
SELECT * FROM users INNER JOIN(
SELECT email FROM users GROUP BY email HAVING COUNT(email) > 1 order by email)
temp ON users.email = temp.email;
after i had this query, i want to separate each users_id with same email above where doing transaction, and not doing transaction based on order_match_detail tables, users_id with no transaction are not appear in order_match_detail table. how to separate each users_id become doing transaction and not doing transaction
expected results based on the fiddle
+--------------------------------+-------------------------------+
| users_Id doing transaction | users_id not doing transaction|
+--------------------------------+-------------------------------+
| 1 | 3 |
| | 4 |
| | 5 |
+--------------------------------+-------------------------------+
Related
I'm trying to write SQL (in BigQuery) the following in order to get a result that satisfies the following conditions.
for ex: my table contains the following data
id | value
___________|_____________
1 | p
1 | oo
2 | p
4 | p
4 | lop
5 | AA
5 | p
6 | p
6 | p
I want to filter out records where it contains only value as "p" from the table.
The expected result would be
2 | p
6 | p
I have tried with the following query but it returns other rows as well (1,p and 1,oo)
SELECT id,value
FROM `ggg.tt.table` where userid in
(
select id from (SELECT id,COUNT(distinct value )as cnt
from (select * FROM `ggg.tt.table` where trim(value) = 'p'
)group by 1 having cnt = 1))
can someone help how to achieve this using bigquery ?
Use below approach
select id
from `ggg.tt.table`
group by id
having countif(value != 'p') = 0
if applied to sample data in your question - output is
what if i want column "value" as well in result
In case if for some reason you need value in the output - even though it is always p as per definition of you case - consider below
select id, any_value(value) value
from `ggg.tt.table` t
group by id
having countif(t.value != 'p') = 0
with output
You can count the distinct values for every id
CREATE TABLE tab1
(`id` int, `value` varchar(3))
;
INSERT INTO tab1
(`id`, `value`)
VALUES
(1, 'p'),
(1, 'oo'),
(2, 'p'),
(4, 'p'),
(4, 'lop'),
(5, 'AA'),
(5, 'p'),
(6, 'p'),
(6, 'p')
;
SELECT DISTINCT id
FROm tab1 t1
WHERE `value` = 'p' AND (SELECT COUNT(DISTINCT `value`) FROM tab1 WHERE id = t1.id) = 1
| id |
| -: |
| 2 |
| 6 |
db<>fiddle here
Here is another possible solution:
SELECT DISTINCT id,value
FROM `ggg.tt.table`
WHERE TRIM(value) = 'p' AND
id NOT IN (SELECT id FROM `ggg.tt.table` WHERE TRIM(value) != 'p')
WITH
cte AS (
select 1 as Id ,'p' as value union all
select 1 as Id ,'p' as value union all
select 1 as Id ,'oo' as value union all
select 2 as Id ,'p' as value union all
select 4 as Id ,'p' as value union all
select 4 as Id ,'lop' as value union all
select 5 as Id ,'AA' as value union all
select 5 as Id ,'p' as value union all
select 6 as Id ,'p' as value union all
select 6 as Id ,'p'
),
cte2 AS (
SELECT
id,
value
FROM
cte
WHERE
value != 'p' )
SELECT
id,
count(distinct value)
FROM
cte
WHERE
id NOT IN (
SELECT
id
FROM
cte2)
group by id
The issue that we are trying to tackle is best shown with the following illustrative example:
CREATE TABLE table_1
(
id INT UNSIGNED AUTO_INCREMENT,
colA INT,
colB VARCHAR(10),
PRIMARY KEY(id)
);
CREATE TABLE table_2
(
id INT UNSIGNED AUTO_INCREMENT,
colY INT,
colZ VARCHAR(10),
PRIMARY KEY(id)
);
INSERT INTO table_1(colA, colB) VALUES(1, 'NPD5A6V9EI'), (2, 'ISO4IK42YQ'), (4, 'J12QAN4O42'), (6,'V8YTZFHCU4');
INSERT INTO table_2(colY, colZ) VALUES(3, 'RBUNWLO753'), (4, 'X2BCEY7O8B'), (5, 'BNUS7R4225'), (6, '72NOWCTH5G');
We would like to select our result based on the value of colA in table_1 but if that does not return a result , we would like to return our result based on the value of colY in table_2. In other words SELECTing from table_2 is the backup for SELECTing from table_1. The query returns NULL only if neither table satisfies the condition.
A pseudo SQL query could be:
SELECT colB FROM table_1 where colA = 3 OR SELECT colZ FROM table_2 where colY = 3;
The query should return output based on the following I/O table:
I O
= =
1 NPD5A6V9EI -- From table_1
2 ISO4IK42YQ -- From table_1
3 RBUNWLO753 -- From table_2
4 J12QAN4O42 -- From table_1 (has precedence over table_2 entry)
5 BNUS7R4225 -- From table_2
6 V8YTZFHCU4 -- From table_1 (has precedence over table_2 entry)
9 NULL
Kindly suggest solutions that:
make use of the latest DB features (for posterity)
work with MySQL version 5.6.51 (for our application)
Write a subquery that generates all the I rows that you want.
Then left join this with the two tables, and use IFNULL to take the matching value from table_1 in preference to table_2.
SELECT ids.id AS I, IFNULL(t1.colB, t2.colZ) AS O
FROM (SELECT 1 AS id UNION ALL SELECT 2 UNION ALL SELECT 3 ... UNION ALL SELECT 9) AS ids
LEFT JOIN table_1 AS t1 ON t1.colA = ids.id
LEFT JOIN table_2 AS t2 ON t2.colY = ids.id
ORDER BY ids.id
I simply don't kn ow where you get your last row.
also with Myql 8 you can ise the window function ROW_NUMBER
the rest is self explantory, the sorting comes from colA and Col1, when there are teh same numbers the second column orderby2 comes and sorts first for the first table
CREATE TABLE table_1
(
id INT UNSIGNED AUTO_INCREMENT,
colA INT,
colB VARCHAR(10),
PRIMARY KEY(id)
);
CREATE TABLE table_2
(
id INT UNSIGNED AUTO_INCREMENT,
colY INT,
colZ VARCHAR(10),
PRIMARY KEY(id)
);
INSERT INTO table_1(colA, colB) VALUES(1, 'NPD5A6V9EI'), (2, 'ISO4IK42YQ'), (4, 'J12QAN4O42'), (6,'V8YTZFHCU4');
INSERT INTO table_2(colY, colZ) VALUES(3, 'RBUNWLO753'), (4, 'X2BCEY7O8B'), (5, 'BNUS7R4225'), (6, '72NOWCTH5G');
SELECT #i := #i +1 AS I,
colB AS O
FROM
(SELECT colA as orderby1,colB,1 ordberby2 froM table_1
UNION
SELECT colY, colZ,2 froM table_2 ) t1,(SELECT #i := 0) t2
ORDER BY orderby1,ordberby2
I | O
-: | :---------
1 | NPD5A6V9EI
2 | ISO4IK42YQ
3 | RBUNWLO753
4 | J12QAN4O42
5 | X2BCEY7O8B
6 | BNUS7R4225
7 | V8YTZFHCU4
8 | 72NOWCTH5G
db<>fiddle here
I know there are a number of questions here relating to what I am facing, but none of them are able to solve my situation.
I have two tables TABLE_1 and TABLE_2.
Table TABLE_1 has columns:
ID,
NAME
Table TABLE_2 has columns:
CODE,
AMOUNT,
QUANTITY
The two tables have a different number of columns but the row count is same.
Is possible to write an SQL query wherein I can retrieve all the columns from both the table in a single result set.
I am working on MySQL server.
Note: Both the tables have no common column. Any help is appreciated.
This is how I wish to retrieve:
| ID | NAME | CODE | AMOUNT | QUANTITY |
| | | | | |
| | | | | |
| | | | | |
Refer below query
SELECT a.ID, a.NAME, b.CODE,b.AMOUNT ,b.QUANTITY
FROM (SELECT
ROW_NUMBER() OVER(ORDER BY name ASC) AS RowNo, * FROM TABLE_1 )a
inner join (SELECT
ROW_NUMBER() OVER(ORDER BY CODE ASC) AS RowNo, * FROM TABLE_2 )b
On a.RowNo= b.RowNo
#Allan, here is my solution. Hope it helps.
CREATE TABLE t1(
ID INTEGER,
NAME VARCHAR(10)
);
CREATE TABLE t2(
CODE INTEGER,
AMOUNT INTEGER,
QUANTITY INTEGER
);
INSERT INTO t1 VALUES(91, 'Name1');
INSERT INTO t1 VALUES(92, 'Name2');
INSERT INTO t1 VALUES(93, 'Name3');
INSERT INTO t2 VALUES(1, 123, 2);
INSERT INTO t2 VALUES(2, 233, 4);
INSERT INTO t2 VALUES(3, 433, 1);
Query:
SET #rank=0;
SET #rank2=0;
select id,name,code,amount,quantity
from
(SELECT #rank:=#rank+1 AS rank, id, name
from t1) a,
(SELECT #rank2:=#rank2+1 AS rank, code, amount, quantity
from t2) b
where a.rank=b.rank;
I am new to sql and am trying to figure out the following..
Imagine the following table:
user_id, category_id
1, 12344
1, 12344
1, 12345
2, 12345
2, 12345
3, 12344
3, 12344
and so on..
I want to find number of repeated users each category got..
so, in example above..
12344, 2 (because user_id 1 and 3 are repeated users)
12345, 1 (user_id 2 is repeated user.. 1 is not as that user visited just once)
How do i figure this out in sql/hive?
E.g.:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,user_id INT NOT NULL
,category_id INT NOT NULL
);
INSERT INTO my_table (user_id,category_id) VALUES
(1, 12344),
(1, 12344),
(1, 12345),
(2, 12345),
(2, 12345),
(3, 12344),
(3, 12344);
SELECT category_id
, COUNT(*) total
FROM
( SELECT x.*
FROM my_table x
JOIN my_table y
ON y.user_id = x.user_id
AND y.category_id = x.category_id
AND y.id < x.id
) a
GROUP
BY category_id;
+-------------+-------+
| category_id | total |
+-------------+-------+
| 12344 | 2 |
| 12345 | 1 |
+-------------+-------+
It's a little hard to follow what you're looking for, but test this:
select category_id, count(user_id) from (Select category_id, user_id, count(table_primary_id) as 'total' from tablename group by category_id, user_id) a where total > 1 group by category_id
The subquery counts the number of times a user visited a category, and the outside query should count the number of users who visited a category more than once.
How to update (change from first select table value second) second_table.first_table_id if first_table.email match in both select.
If it even possible. With one query!
----------------------------------------- UPDATE -----------------------------------------
EXAMPLE:
I need to update foreign key of second table if email field match in first table. I need to compare two query results with different parent_id (parents are in in same table with children)
table_1
-------------------------
| id | parent_id | email |
-------------------------
1 NULL NULL
2 NULL NULL
3 1 joe#m.ru
4 2 bob#f.ly
5 1 bob#f.ly
6 2 kira#.us
table_2
----------------
| id | first_id |
----------------
1 3
2 4
3 5
4 6
I have two parents with ids 1 and 2 and some children (ids: 3,4,5,6).
Also, keep in mind: 1 - old, 2 - new
Task: change foreign key in second table if children email with parent_id = 1 and chilren email with parent_id = 2 match (are the same).
In our example in second table row with id = 3 its foreign key field - first_id has to change from 5 to 4.
Following might get you started
UPDATE Table_2 t2u
SET first_id = (
SELECT t2.first_id
FROM Table_2 t2
INNER JOIN Table_1 t1 ON t1.id = t2.first_id
INNER JOIN (
SELECT parent_id = MAX(parent_id), email
FROM Table_1
GROUP BY
email
) t1p ON t1p.email = t1.email
INNER JOIN Table_1 t1i ON t1i.email = t1p.email
AND t1i.parent_id = t1p.parent_id
WHERE t2u.first_id <> t1i.id)
Test script (SQL Server)
;WITH Table_1 (id, parent_id, email) AS (
SELECT 1, NULL, NULL
UNION ALL SELECT 2, NULL, NULL
UNION ALL SELECT 3, 1, 'joe#m.ru'
UNION ALL SELECT 4, 2, 'bob#f.ly'
UNION ALL SELECT 5, 1, 'bob#f.ly'
UNION ALL SELECT 6, 2, 'kira#.us'
)
, Table_2 (id, first_id) AS (
SELECT 1, 3
UNION ALL SELECT 2, 4
UNION ALL SELECT 3, 5
UNION ALL SELECT 4, 6
)
SELECT t2.*, t1i.id as [update with]
FROM Table_2 t2
INNER JOIN Table_1 t1 ON t1.id = t2.first_id
INNER JOIN (
SELECT parent_id = MAX(parent_id), email
FROM Table_1
GROUP BY
email
) t1p ON t1p.email = t1.email
INNER JOIN Table_1 t1i ON t1i.email = t1p.email
AND t1i.parent_id = t1p.parent_id
WHERE t2.first_id <> t1i.id
Output
id first_id update with
----------- ----------- -----------
3 5 4