Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Have two tables one is customer and other prize amount
**Customer** **PrizeAmount**
*CustomerId Amount* *CustomerId PrizeAmt*
1 500 1 2000
1 2000
Resultant query should be like this
CustomerId Amount PrizeAmt
1 500 2000
1 (NULL) 2000
So how to write query for above result
With left join you can do it
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
so
select customer_id as custo_id,amount,prize_amt
from customer
left join prize_amount on customer.customer_id=prize_amount.customer_id
Left Join
Perhaps this
DROP TABLE IF EXISTS T,T1;
CREATE TABLE T (CID INT , AMOUNT_INVESTED INT);
CREATE TABLE T1 (CID INT, AMOUNT_WON INT);
INSERT INTO T VALUES (1,500);
INSERT INTO T1 VALUES (1,2000),(1,2000);
SELECT CID,AMOUNT_INVESTED,AMOUNT_WON
FROM
(
SELECT T.CID,IF(T.CID <> #P, T.AMOUNT_INVESTED,NULL) AMOUNT_INVESTED, T1.AMOUNT_WON,
#P:=T.CID
FROM T
JOIN T1 ON T.CID = T1.CID
CROSS JOIN (SELECT #P:=0) P
) S
+------+-----------------+------------+
| CID | AMOUNT_INVESTED | AMOUNT_WON |
+------+-----------------+------------+
| 1 | 500 | 2000 |
| 1 | NULL | 2000 |
+------+-----------------+------------+
2 rows in set (0.00 sec)
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
My table looks like this:
Event_id
Species
1
Dog
1
Horse
2
Dog
3
Cat
4
Fish
4
Bird
5
Cat
I dont want to keep any of the rows which have a duplicated event_id, as I cant be sure about the species type of the event. How do I remove both rows of the table in mysql? I dont have a unique id for each row.
The output should look like this:
Event_id
Species
2
Dog
3
Cat
5
Cat
Thanks in advance!
Here's a solution I tested on MySQL 8.0 (required for the use of with):
mysql> create table mytable (event_id int, species varchar(20));
mysql> insert into mytable (Event_id,Species) values (1,'Dog'), (1,'Horse'),
(2,'Dog'), (3,'Cat'), (4,'Fish'), (4,'Bird'), (5,'Cat');
mysql> with cte as (select event_id from mytable group by event_id having count(*)>1)
delete mytable from mytable join cte using (event_id);
mysql> select * from mytable;
+----------+---------+
| event_id | species |
+----------+---------+
| 2 | Dog |
| 3 | Cat |
| 5 | Cat |
+----------+---------+
An easy approach would be:
delete t1
from my_tbl as t1
inner join ( select event_id
from my_tbl
group by event_id
having count(*) >1
)as t2
on t1.event_id=t2.event_id;
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/155
Or with sybquery:
delete from my_tbl
where event_id not in ( select t1.event_id from (select event_id
from my_tbl
group by event_id
having count(*) =1) as t1
) ;
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/151
Below query return the event_id that exist only once. So you can delete with the condition event_id not in.
select event_id
from my_tbl
group by event_id
having count(*) =1
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a table as shown below
id | t_id | u_id
---+------+------
1 | 2 | 2
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 1 | 1
I am trying to get all t_id with u_id of 2 but once without the t_id ever having a u_id of 1 in the history of the whole table.
I tried
SELECT
C_Name, count(*) as count
FROM tenter
WHERE C_Date = '20200127' AND L_TID = '2';
But this gives me the record of all L_TID = 2 and does not filter out those with previous record of L_tid = 1.
Expected result: get all U_ID without the previous history of L_TID = 1, it should get only those without ever having L_tid =1.
Thanks in advance.
One method is aggregation and a having clause:
select t_id
from tenter t
group by t_id
having sum(u_id = 2) > 0 and -- has "2"
sum(u_id = 1) = 0; -- does not have "1"
If you have another table of t values, then exists/not exists might be more efficient:
select t.t_id
from t
where exists (select 1 from tenter tt where tt.t_id = t.t_id and tt.u_id = 2) and
not exists (select 1 from tenter tt where tt.t_id = t.t_id and tt.u_id = 1);
I think you want not exists:
select t.*
from tenter t
where
u_id = 2
and not exists (
select 1 from tenter t1 where t1.t_id = t.t_id and t1.u_id = 1
)
You can also use aggregation, if you just want a list of t_ids. If 1 and 2 are the only possible values, you can just do:
select t_id
from tenter t
group by t_id
having min(u_id) = 2
If there are other possible values:
having max(u_id = 1) = 0 and max(u_id = 2) = 1
I want to fetch data from two table and apply arithmetic operation on the column.
This is wha I tried :
String sql = "SELECT SUM(S.san_recover-C.amount) as total
FROM sanction S
LEFT JOIN collection C ON S.client_id = C.client_id
WHERE S.client_id=?";
This code is working only when there is value in both tables, but if there is no value in one of two tables there is no result.
SELECT SUM(S.san_recover - C.amount) as total
FROM sanction S
LEFT JOIN collection C ON S.client_id = C.client_id
WHERE S.client_id = ?
The problem with your query lies in the SUM() function. When the left join does not bring back records, then c.amount is NULL. When substracting NULL from something, you get a NULL result, which then propagates across the computation, and you end up with a NULL result for the SUM().
You probably want COALESCE(), like so:
SELECT SUM(S.san_recover - COALESCE(C.amount, 0)) as total
FROM sanction S
LEFT JOIN collection C ON S.client_id = C.client_id
WHERE S.client_id = ?
Where there is a possibility that a client may exist in one table but no another a full join would be appropriate but since mysql does not have such a thing then a union in a sub query will do
drop table if exists sanctions,collections;
create table sanctions(client_id int, amount int);
create table collections(client_id int, amount int);
insert into sanctions values
(1,10),(1,10),(2,10);
insert into collections values
(1,5),(3,10);
Select sum(Samount - camount)
From
(Select sum(amount) Samount, 0 as camount from sanctions where client_id =3
Union all
Select 0,sum(amount) as camount from collections where client_id =3
) s
;
+------------------------+
| sum(Samount - camount) |
+------------------------+
| -10 |
+------------------------+
1 row in set (0.00 sec)
If you want to do this for all clients
Select client_id,sum(Samount - camount) net
From
(Select client_id,sum(amount) Samount, 0 as camount from sanctions group by client_id
Union all
Select client_id,0,sum(amount) as camount from collections group by client_id
) s
group by client_id
;
+-----------+------+
| client_id | net |
+-----------+------+
| 1 | 15 |
| 2 | 10 |
| 3 | -10 |
+-----------+------+
3 rows in set (0.00 sec)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have two tables.
Table1: First
--------------------------
| iso | WeldNo |
--------------------------
| AMD-11201-01 | 02 |
| RFG-11203-01 | 01 |
| AMD-11201-01 | 03 |
| RFG-11203-01 | 05 |
Table2: Second
--------------------------
| iso | WeldNo |
--------------------------
| AMD-11201-01 | 02 |
| RFG-11203-01 | 01 |
| RFG-11203-01 | 05 |
Expected Result:
--------------------------
| iso | WeldNo |
--------------------------
| AMD-11201-01 | 03 |
i tried query but wont work
select a.iso, a.WeldNo
from first a
where a.WeldNo, a.iso not in (select b.iso,b.WeldNo from second b);
Kindly give solution to my scenario
From the tags on the question it is not clear which RDBMS you are using. If you are using SQL Server you can use EXCEPT:
select a.iso, a.WeldNo
from first a
EXCEPT
select b.iso, b.WeldNo
from second b
This selects all items from the SELECT statement, except for the ones that are found in the second SELECT statement.
If you are using MySQL you could slightly fix your query (i.e. add parenthesis) and you should get a working query:
select a.iso, a.WeldNo
from first a
where (a.WeldNo, a.iso) not in (select b.WeldNo, b.iso from second b)
select f.*
from first_table f
left join second_table s on s.iso = f.iso
and s.WeldNo = f.WeldNo
where s.iso is null
Also see this great explanation of joins
You can use either a LEFT JOIN or an EXCEPT.
Left Join:
SELECT t1.iso, t1.WeldNo
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.iso = t2.iso AND t1.WeldNo = t2.WeldNo
WHERE t2.iso IS NULL
Except:
SELECT iso, WeldNo
FROM Table1
EXECEPT
SELECT iso, WeldNo
FROM Table2
There are some differences, though. In the case of except, both results have to have the same columns, while the LEFT JOIN lets you specify what to select from each table. Also, in most cases, the LEFT JOIN is going to be more performant, as it is better optimized. However, the EXCEPT will only return unique rows from the first query that are missing from the second query, so it might be more applicable to the situation. Of course, you could add a DISTINCT To the first query for a similar result:
SELECT DISTINCT t1.iso, t1.WeldNo
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.iso = t2.iso AND t1.WeldNo = t2.WeldNo
WHERE t2.iso IS NULL
All that said, it's probably better to use the LEFT JOIN, however it may vary depending on your database.
I would use not exists because it expresses the intent clearly i.e. "select all rows from table1 where a row in table2 with the same iso and WeldNo does not exist"
select * from table1 t1
where not exists (
select 1 from table2 t2
where t2.iso = t1.iso
and t2.WeldNo = t1.WeldNo
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Let me try and re-explain what I'm trying to do. First off I have a database or items that is huge, 50k and more, growing daily. I need to pull information from this database based on what it does not have. (I'm using SQL; pSQL or MySql doesn't matter.)
So the database looks like this (roughly)
SELECT * FROM my_table
+---------+--------+
| ITEM | TYPE |
+---------+--------+
| 12EU | P |
| 12EU | R |
| 12EU | T |
| 34RE | P |
| 34RE | R |
| 34RE | T |
| 54TR | R |
| 54TR | T |
+---------+--------+
Not all items have "P" but they all have "T" and "R." I need to pull the ITEM that do not have a TYPE "P," the ITEM's with other letters do not matter to me.
In this example I would want ITEM "54TR" because it does not have a TYPE "P."
I hope this helps better explain what I am trying to do.
EXAMPLE of what I have done:
SELECT distinct ITEM
FROM (SELECT distinct ITEM FROM my_table WHERE TYPE='P') q
WHERE TYPE!='P'
AND ITEM != q.ITEM
ORDER BY ITEM
This does not work, it still returns other types, not the right track...
select distinct item from yourtablename where type!='P'
minus
select distinct item from yourtablename where type='P'
example :
create table myt (item varchar2(10),atype varchar2(10),piece varchar2(10));
insert into myt values ('12EU','P','ext');
insert into myt values ('12EU','R','ext');
insert into myt values ('34RE','T','ext');
insert into myt values ('54RT','P','ext');
insert into myt values ('34EU','R','ext');
insert into myt values ('54TR','P','ext');
commit;
SQL> select * from myt;
ITEM ATYPE PIECE
---------- ---------- ----------
12EU P ext
12EU R ext
34RE T ext
54RT P ext
34EU R ext
54TR P ext
6 rows selected.
SQL> select distinct item from myt where atype!='P'
2 minus
3 select distinct item from myt where atype='P';
ITEM
----------
34EU
34RE
select * from my_table t
where type != 'P'
AND not exists
(select 1 from my_table t2 where t2.item = t.item and t2.type = 'P')
Try wih following SQL query
select * from yourtablename where Type not like '%P%'
Select * from tablename where type != 'p'