I have three tables tblClients, tblClientsActivities and tblActivities
tblClients and tblClientsActivities are joined together to produce Table A below
SELECT * from tblClientsActivities
left join tblclients on tblClientsActivities.fk_cid=tblclients.pk_cid
PK_AID FK_CID FK_AID
1 4 1
2 4 2
3 4 4
4 5 1
5 5 3
6 5 5
7 6 1
8 6 2
9 7 6
10 8 3
The problem is how do i remove all clients where FK_AID <> 1
Description
Simple use a WHERE clause.
Sample
SELECT * FROM tblClientsActivities
LEFT JOIN tblclients on tblClientsActivities.fk_cid=tblclients.pk_cid
WHERE FK_AID <> 1
With a restriction used in a WHERE clause:
SELECT * from tblClientsActivities
left join tblclients on tblClientsActivities.fk_cid=tblclients.pk_cid
where FK_AID != 1
Related
I am trying to use LEFT JOIN, NOT EXISTS and NOT IN, where I want records from pursuit table, expect matching records from condition table using foreign key instance = id. When I execute the query, it shows empty (no records).
LEFT JOIN
SELECT p.id, p.dept
FROM
pursuit p LEFT JOIN condition c USING (dept)
WHERE
p.id = c.instance
NOT EXISTS
SELECT p.id, p.dept
FROM
pursuit p
WHERE
p.id NOT EXISTS IN (SELECT c.instance FROM condiiton c)
NOT IN
SELECT p.id, p.dept
FROM
pursuit p
WHERE
p.id NOT IN (SELECT c.instance FROM condiiton c)
TABLES
condition
id instance dept
1 3 5
5 2 5
2 7 5
3 4 5
4 10 5
5 2 3
6 15 6
pursuit
id name dept
3 C1 5
2 C9 5
7 C77 5
4 C9 5
10 C6 5
19 C23 5
17 C45 5
15 C12 5
23 C33 5
3,2,7,4 = matching records of dept 5
19,17,15,23 = not matching records of dept 5
Output
id name dept
19 C23 5
17 C45 5
15 C12 5
23 C33 5
Edit2:
SELECT p.id, p.dept
FROM pursuit p LEFT JOIN condition c
on (p.id = c.instance and p.dep=c.dep)
WHERE c.instance IS NULL
this returns rows of pursuit not present in condition table
I would use NOT EXISTS instead :
SELECT P.*
FROM pursuit p
WHERE NOT EXISTS (SELECT 1
FROM condition c
WHERE p.id = c.instance
);
The only problem were with NOT EXISTS you haven't established any relation between them.
I have the following two tables:
table_A
eID
1
2
3
4
table_B
eID dID
1 7
1 5
2 9
2 3
4 8
I want the result to look like this:
eID dID
1 1
1 7
1 5
2 2
2 9
2 3
4 4
4 8
3 3
I was wondering if there is any different way to achieve this than the following:
(SELECT A.eID, A.eID
FROM table_A AS A)
UNION
(SELECT A.eID, B.dID
FROM table_A AS A
INNER JOIN table_B AS B
ON A.eID = B.eID)
I think this is more simply written as:
SELECT A.eID, A.eID as dID
FROM table_A
UNION ALL
SELECT B.eID, B.dID
FROM table B;
But that still isn't allowed for a subquery. I would recommend using a view (or switching to a database that is more ANSI-compliant ;).
id address retailer
1 A 11
2 A 11
3 A 11
4 A 12
5 A 13
6 B 12
7 B 12
8 B 13
My output should be
id address retailer
1 A 11
4 A 12
5 A 13
6 B 12
8 B 13
i.e my query should return id's which have same address but not same retailer.
How toget this?
Try to use group by clause as below:
select min(id), address, retailer
from tab
group by address, retailer
Assuming you're joining on columns with no duplicates, which is by far the most common case:
An inner join of A and B gives the result of A intersect B, i.e. the inner part of a venn diagram intersection.
An outer join of A and B gives the results of A union B, i.e. the outer parts of a venn diagram union.
Examples:
Suppose you have two Tables, with a single column each, and data as follows:
A B
- -
1 3
2 4
3 5
4 6
Note that (1,2) are unique to A, (3,4) are common, and (5,6) are unique to B.
Inner join:
An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
select *
from a
INNER JOIN b on a.a = b.b;
select a.*,b.*
from a,b
where a.a = b.b;
a | b
--+--
3 | 3
4 | 4
Left outer join:
A left outer join will give all rows in A, plus any common rows in B.
select *
from a
LEFT OUTER JOIN b on a.a = b.b;
select a.*,b.*
from a,b
where a.a = b.b(+);
a | b
--+-----
1 | null
2 | null
3 | 3
4 | 4
Full outer join:
A full outer join will give you the union of A and B, i.e. All the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa.
select *
from a
FULL OUTER JOIN b on a.a = b.b;
a | b
-----+-----
1 | null
2 | null
3 | 3
4 | 4
null | 6
null | 5
select min(id) as id,address, retailer
from table1
group by address, retailer
order by id
The query you need is:
SELECT min(id), address, retailer
FROM table1 AS t1
group by address, retailer
order by address
Here's the source
Use This: It's working:
SELECT * FROM `sampletable` GROUP BY address, retailer
Hi i have 3 tables and i am trying to join them to get a desire table. I have tried group by and temp tables options to get the desired table but no help. I want to avoid duplicates for every instance of a value in one table from another table.
Table 1 Customer Table:
CstId CstDetails CstType
---------- --------------- ------------
1 address 1 1
2 address 2 1
3 address 3 1
4 address 4 2
5 address 5 2
Table 2 Customer Relationship:
CstId CstGroupId
---------- ----------------
1 4 (this is same as CustomerId)
2 5 (this is same as CustomerId)
3 4 (this is same as CustomerId)
Table 3 Customer Notes:
CstId NotesId NoteTxt
----------- --------- ---------
1 1 note11
1 2 note12
1 3 note13
3 1 note31
4 1 note41
4 2 note42
4 3 note43
4 4 note44
4 5 note45
Now i want the result to be in the below format
Table result:
(NoteId) (Notetxt) (NoteId) (Notetxt)
CstId CstDetails CstGroupId CstNoteId CstNote CstGroupNoteId CstGroupNote
1 address1 4 1 note11 1 note41
1 address1 4 2 note12 2 note42
1 address1 4 3 note13 3 note43
1 address1 4 null null 4 note44
1 address1 4 null null 5 note45
But i am getting CstGroupNote repeated for all the CstNote, which i am trying to avoid.
Is there a way i could achieve this result?
Below is the code i use:
select c.cstid, c.cstdetails, cn.cstnotesid, cn.cstnotetxt
insert into temp1
from customer c
left outer join customernotes cn
on c.cstid = cn.cstid
where c.customertypeid = 1
select cr.cstid, cr.cstgroupid, cn.cstgroupnoteid, cn.cstnotetxt
insert into temp2
from customerrelationship cr
left outer join customernotes cn
on cr.cstgroupid = cn.customerid
select t1.cstid, t1.cstdetails, t1.cstnotesid, t1.cstnotetxt, t2.cstgroupnoteid, t2.cstnotetext
from temp1 t1
left outer join t2
on t1.cstid = t2.cstid
Try:
select CstId,
max(CstDetails) CstDetails,
max(CstGroupId) CstGroupId,
max(CstNoteId) CstNoteId,
max(CstNote) CstNote,
max(CstGroupNoteId) CstGroupNoteId,
max(CstGroupNote) CstGroupNote
from
(select c.CstId,
c.CstDetails,
0 CstGroupId,
n.NotesId CmbNotesId,
n.NotesId CstNoteId,
n.NoteTxt CstNote,
0 CstGroupNoteId,
'' CstGroupNote
from customer c
left outer join customernotes n on c.cstid = n.cstid
where c.customertypeid = 1
union all
select c.CstId,
c.CstDetails,
r.CstGroupId,
n.NotesId CmbNotesId,
0 CstNoteId,
'' CstNote,
n.NotesId CstGroupNoteId,
n.NoteTxt CstGroupNote
from customer c
left outer join customerrelationship r on c.cstid = r.cstid
left outer join customernotes n on r.CstGroupId = n.cstid
where c.customertypeid = 1) u
group by CstId, CmbNotesId
Use a derived table and an outer join
The trick there is the
and cn.cstnotesid = cG.cstnotesid
to link those two on one row
select c.cstid, c.cstdetails, cn.cstnotesid, cn.cstnotetxt
,cG.CstGroupId, cG.cstnotesid, cG.cstnotetxt
from customer c
join customernotes cn
on cn.cstid = c.cstid
outer join (select c.cstid, c.CstGroupId, cn.cstnotesid, cn.cstnotetxt
from customer c
join customernotes cn
on cn.cstid = c.CstGroupId) as cG
on c.cstid = cG.cstid
and cn.cstnotesid = cG.cstnotesid
order by c.cstid, cn.cstnotesid, cG.cstnotesid
I've been struggling with this one for a while.
[sample table]
ITEM GROUP
---- -----
4 A
7 A
3 A
8 A
7 B
6 B
9 B
0 C
4 C
2 C
5 C
4 C
7 C
5 D
9 D
2 E
7 E
1 E
4 E
7 F
3 F
9 F
6 F
8 G
4 H
5 H
3 H
9 H
0 H
8 H
I need an sql query that will tell me how many times each "ITEM", on a range of SELECT DISTINCT ITEM appeared with one another in a particular group.
IE:
Items 4 and 8 appeared 2 times (groups A and H).
Items 0 and 4 appeared 2 times (groups C and H).
Items 7, 6 and 9 appeared appeared 2 times (groups B and F).
..And do on. It's ok to ignore "rogue" items that only appear in one group.
Can it be done? Thanks
This will work for pairs of items:
SELECT T1.item, T2.item, COUNT(*)
FROM yourTable T1
JOIN yourTable T2
ON T1.item < T2.item
AND T1.group = T2.group
GROUP BY T1.item, T2.item