By see below tables how to (write query) get Table A data and status = InActive with no Data in Table B
Example : 4 Comm4 InActive
Table A
AID Name Status
-- --- --
1 comm1 Active
2 comm2 Active
3 Comm3 InActive
4 Comm4 InActive
5 Comm5 InActive
Table B
BID Name AID
--- ---- ---
11 James 1
12 Kris 2
13 Dan 3
14 Steve 3
15 Brian 5
It's quite simple
select * from tableA
where status = 'InActive'
and not exists (select * from tableB where tableA.AID = tableB.AID)
select tableA.*
from tableA
left join tableB
on tableA.AID = tableB.AID
and tableA.status = 'InActive'
where tableB.AID is null
The not exists from Szymon is correct and may be more efficient
Here you try.
select *
from #table_one
where Status = 'InActive'
and not exists
(
select 1 from #table_two where AID = #table_one.AID
);
Related
I have a Table that is joined from other tables. I want to update a null value in this Table by a specific number series. Below is the illustration:
The code for the Table which is called List_Codes
SELECT mlk.MLK_CODE
FROM zpt
LEFT OUTER JOIN mes ON mes.ZPT_ID = zpt.ZPT_ID
LEFT OUTER JOIN zmlk ON zpt.ZPT_ID = zmlk.ZPT_ID
LEFT OUTER JOIN mlk ON zmlk.MLK_ID = mlk.MLK_ID
WHERE zpt.zpt_id IS NOT NULL
and zpt.zpt_meteringcode = '123456'
ORDER BY mes.MES_STATUS DESC
Now I want to update this specific row's mlk.MLK_CODE from null to '789'. I have located this row based on the zpt.zpt_meteringcode. Any suggestions plz?
The tables look like this, and the List_Code Table is the result of the above code
Mlk Table
Mlk_id Mlk_code
1 123
2 456
Zpt Table
Zpt_id Zpt_meteringcode
10 123456
20 987654
30 654321
40 147852
Zmlk Table
Zpt_id Mlk_id
20 1
30 2
List_Code Table
Zpt_id Zpt_meteringcode Mlk_id Mlk_code
10 123456
20 987654 1 123
30 654321 2 456
40 147852
I think you need two inserts and one update statements like this.
INSERT INTO mlk (mlk_code)
VALUES ( '789' ); -- Primary key is generated
.....
INSERT INTO zmlk (zpt_id,
mlk_id)
SELECT (SELECT zpt_id
FROM zpt
WHERE zpt_meteringcode = '123456'),
(SELECT mlk_id
FROM mlk
WHERE mlk_code = '789')
FROM dual;
....
UPDATE list_code
SET ( mlk_id, mlk_code ) = (SELECT mlk_id,
mlk_code
FROM mlk
WHERE mlk_code = '789')
WHERE zpt_meteringcode = '123456';
I'm trying to build a query to give me some information doing an inner join on two tables, tableA and tableB. Here's what they look like:
TableA:
ClientID RevNo RevPurp
------------------------------
123 4557 1
124 4555 1
123 6574 2
123 7857 3
124 8987 2
TableB:
RevNo ClientID Active
------------------- -------------
4557 123 True
6574 123 True
7857 123 True
8987 124 True
4555 124 True
Here is what I'm trying to do. I'm trying to do...
Select Distinct ClientID
From TableB
Inner Join TableA On tableB.RevNo = tblB.RevNo
Where RevPurp is not 3.
RevPurp can be 1,2,3 - I'm only trying to select those distinct clients that can have either 1,2 but cannot have a 3.
Looking at the data in the table I should only have 1 record....
124
Because client 123 has RevPurp 3. So if a client has RevPurp 3, I do not want to see that client when I execute the query!
EDIT - I added a field in TableB called ACTIVE. I need to make sure Active = True when doing my query!
as #devlin mentioned in a comment, you do not need tableB to do this:
You want a list of unique (distinct) ClientIds in TableA where there is no record in tableA with a RevPurp value of 3.
EDIT: adding in the filter predicate on TableB.Active
Select distinct a.ClientId
from tableA a join tableb b
on b.RevNo = a.RevNo
Where b.Active = 1 -- <===== is Active a BIT field
and Not exists
(Select * from tableA
Where ClientId = a.ClientId
and RevPurp = 3)
You can try not in
Select Distinct ClientID
from TableB
inner join TableA on tableB.RevNo = tblB.RevNo
where tableA.ClientID not in (select ClienID from tableA where RevPurp = 3)
A simple group by with a having clause should work:
select clientId
from tableA
group by clientId
having count(*) = count(case when RevPurp <> 3 then 'X' end)
I have to 2 tables:
table A:
pid|filename|date
-----------------
1 |file1 |2009
2 |file2 |2009
3 |file3 |2009
4 |file4 |2009
table B:
pid|filename
------------
1 |filex1
1 |filex2
2 |filex3
2 |filex4
the result I want to get is:
pid|filename|date
-----------------
3 |file3 |2009
4 |file4 |2009
thats mean it should show me only the rows of table A that don't have a relation with the second table.
I started with this code, but it did not work!
$do_q=mysql_query("SELECT *
FROM A LEFT JOIN B
ON (A.pid <> B.pid)");
while($row = mysql_fetch_array($do_q)){
echo $row['pid'];
echo $row['filename'];
echo $row['date'];
}
Thank you.
SELECT A.*
FROM A
LEFT JOIN B
ON A.pid = B.pid
WHERE b.pid IS NULL
I think this should work for you:
SELECT *
FROM A
WHERE pid NOT IN (SELECT pid from B)
SELECT
*
FROM tableA A
WHERE(NOT EXISTS(SELECT 1 FROM tableB B WHERE B.pid = A.pid))
I could really use some help with the following SQL Select statement scenario:
I need to select all rows from a table conditionally depending on whether a userID has already entered data into a second table with the same ID.
Example:
Select all rows from TABLE A for idNumber where idNumber not in
TABLE B
but for each idNumber that IS in TABLE B, still return row unless a
specific userID is in that row in TABLE B.
TABLE A
========
idNumber|type|Date
1 A 01/01/01
2 A 01/01/01
3 B 01/01/01
4 B 01/01/01
5 B 01/01/01
TABLE B
========
idNumber|type|userID
1 A 0000
3 B 0000
4 B 1111
userID to exclude records for = 1111
SQL Query should return:
idNumber|type|Date
1 A 01/01/01
2 A 01/01/01
3 B 01/01/01
5 B 01/01/01
Apologies for the long winded post but i hope it makes sense.
Many thanks in advance,
ukjezza.!!
Select idNumber, type, Date
From TableA
Where Not Exists (
Select 1
From TableB
Where TableB.idNumber = TableA.idNumber
And TableB.userID = 1111
)
Another choice:
Select TableA.idNumber, TableA.type, TableA.Date
From TableA
Left Join TableB
On TableB.idNumber = TableA.idNumber
And TableB.userId = 1111
Where TableB.idNumber Is Null
Looks like a LEFT JOIN and COALESCE could take care of it:
SELECT a.*
FROM TableA as a
LEFT JOIN TableB as b
ON a.idNumber = b.idNumber
WHERE COALESCE(b.userID, -1) != 1111
select A.*
from TableA as A
left outer join TableB as B
on A.idNumber = B.idNumber
where B.idNumber is null or
B.userID <> '1111'
Sorry for posting this question again. I rephrased my question a little bit.
I am trying to write a query to return rows from Table-A where multiple rows found in Table-B with STATUS = 1 for each CID column from Table-A.
So in this example CID 100 has two records found in Table-B and STATUS = 1. So I want to write a query to return this row from Table-A. I know this is a weird table design. Please help.
Here are the tables with sample data.
Table-A
-----------------------------------------
AID Name CID
---------------------------------------
10 test1 100
12 test1 100
13 test2 101
14 test2 101
15 test3 102
Table-B
------------------------------------
bID AID status
-----------------------------------
1 10 1
2 12 1
3 14 1
4 15 1
Try this query:
SELECT TableA.CID
FROM TableA
JOIN TableB ON TableA.AID = TableB.AID
WHERE TableB.status = 1
GROUP BY TableA.CID
HAVING COUNT(*) > 1
It returns 100 for your example data.
Something like this?
select aid,
status
from (select aid,
count(*) as cnt
from tableA
group by aid) as aggregated
left join tableB on tableB.aid = aggregated.aid
where aggregated.cnt > 1
If your using SQL:
WITH tableBView AS
(
SELECT AID AS xxxAID
FROM [Table-B]
WHERE status = 1
GROUP BY AID
HAVING COUNT(*) > 0
)
SELECT *
FROM [Table-A]
WHERE EXISTS (SELECT * FROM tableBView WHERE xxxAID = AID)
SELECT *
FROM Table-A a
WHERE a.CID IN
(
SELECT a.CID FROM Table-A a JOIN Table-B b USING (AID)
GROUP BY a.CID
WHERE b.status = 1
HAVING count(*) > 1
)
This is a very verbose way to do it.
Selects all columns from Table-A on rows where AID match between Table-A and Table-B and more than one row with the same CID exists in Table-A:
(Btw, I wouldn't use "-" in your table/column names. Use "_" instead.)
select
derived_table.AID,
derived_table.Name,
derived_table.CID
from
(select
table_A.AID,
table_A.Name,
table_A.CID,
count(table_A.CID) c
from
Table_A
inner join Table_B on (Table_A.AID = table_B.AID)
group by table_A.CID
) derived_table
where
c > 1