id getting duplicate PV.RFMID [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how can i use distinct INNER JOIN RFM ON RFM.RFMID=PV.RFMID
Table MMASTER
MID col1
1 abc
2 xyx
3 pqr
Table AMM
AMMID MID col1 col2
1 1 bnb mfk
2 1 def rwr
3 2 re wrwr
Table PS
UID VTID AMMID SMID col1 col2
1 1 1 1 rkk jdj
2 2 3 3 kdf lfl
6 2 2 4 rgr rtr
Table PV
VTID PMID RFMID
1 2 1
2 2 3
7 2 2
Table RFM
RFMID title name
1 mr john
2 mr jack
3 mr jim
Table PM
PMID col1 col2
1 df ere
2 rwe rwer
3 rwr fwr
Table SM
SMID MMID col1 col2
1 1 fdf efe
2 1 ddf dfdf
3 2 df ef
I get result like this after fire above query
PMID title name PV.RFMID
2 mr. jim 3
2 mr. jim 3
2 mr. jim 3
***BUT RESULT SHOULD BE LIKE THIS***
PMID title name PV.RFMID
2 mr. john 1
2 mr. jim 3
2 mr. jack 2
In my query PV.RFMID getting duplicate value when join with RFM and due to that title and name getting duplicate

This will give you your desired Output:
select pm.pmid, rfm.title, rfm.name, pv.rfmid from PM as PM
inner join PV on pm.pmid = pv.pmid
inner join RFM as rfm on pv.rfmid = rfm.rfmid
WHERE PM.PMID='2'
EDIT
Considering that you need to Join all tables, it's something tricky.
I mean, checking RFMtable, you can see the RFMId, related to PV table, and see that the VTID has a 7 value.. which is not related to anything ! So, if you need to join all tables, you won't get that result

I Am assuming that table "PM" is your transaction table where one "PMID" can have multiple Entries, thus Adding a Distinct Clause Before your Query will Get you the Desired Out Put.
Try This:
SELECT DISTINCT PM.PMID,RFM.TITLE,RFM.NAME ,PV.RFMID FROM MMASTER
INNER JOIN AMM ON MMASTER.MID=AMM.MID
INNER JOIN PS ON AMM.AMMID=PS.AMMID
INNER JOIN PV ON PV.VTID=PS.VTID
INNER JOIN RFM ON RFM.RFMID=PV.RFMID
INNER JOIN PM ON PV.PMID=PM.PMID
INNER JOIN SM ON PS.SMID=SM.SMID
WHERE PM.PMID='2'

Related

SQL select with three tables and foreign keys

I have three tables :
field:
f_id
f_start
f_end
1
10
20
2
15
25
3
5
10
person :
p_id
p_name
1
Roger
2
John
3
Alicia
affect :
id
fk_field
fk_person
1
2
1
2
1
2
3
3
3
And I would like to select the dates and the names associated to. Like this
p_name
f_start
f_end
Roger
15
25
John
10
20
Alicia
5
10
I'm new to SQL and I don't know if i have to use JOIN or not... Thanks
You must join all 3 tables on their related columns:
SELECT p.p_name, f.f_start, f.f_end
FROM person p
INNER JOIN affect a ON a.fk_person = p.p_id
INNER JOIN field f ON f.f_id = a.fk_field;
Depending on your requirement you may need LEFT instead of INNER joins, but for this sample data the INNER joins will do.

Select rows with specific multiple values from the same column?

I have database where 2 roles can't be associated with each other, and I need to display any users who have conflicting roles.
For example: an (id 2) accountant can't also be a (id 5) trainer
this has to be done without using CTE's
Table a Table b table c
--------------- ------------------- ------------
userID | roleID roleID | conflictID roleID | Role Name
1 2 2 5 1 chef
1 3 2 accountant
1 5 3 driver
2 3 4 barmaid
2 1 5 trainer
3 2
3 3
the result should contain only the userID who has both roles 2 and 5
userID
------
1
Join the b table with the a table twice, to get userID's with conflicting combinations:
select distinct a1.userid
from tableb b
join tablea a1 on b.roleID = a1.roleID
join tablea a2 on b.conflictID = a2.roleID
and a1.userID = a2.userID

How to join 2 tables with no common column in sql

eg
Names
id | name
1 abc
2 efg
Area
id | areaName
3 area1
4 area2
The query should return
id | name | areaid
1 abc 3
1 abc 4
2 efg 3
2 efg 4
This should give the ecpected result:
select a.id, a.name, b.id from names,area
When you want to join all records from table A to all records from table B (i.e. get a Cartesian product, you can use CROSS JOIN:
SELECT Names.id, Names.name, Area.areaid
FROM Names
CROSS JOIN Area
ORDER BY Names.id, Area.areaid

SQL Select values associated with keys from other table

I have a mapping table tableA
key value
-----------
1 "John"
2 "George"
3 "Kate"
4 "loves"
5 "hates"
and another tableB that contains rows based on keys of tableA
col1 col2 col3
------------------
1 5 2
2 4 3
3 4 1
I want to write a selection query which will return rows from table B but replaced with their appropriate values.
e.g. the output has to be:
John | hates | George
George | loves | Kate
Kate | loves | John
Thank you.
SELECT A1.value, A2.value, A3.value
FROM tableB
JOIN tableA as A1 ON tableB.col1 = A1.key
JOIN tableA as A2 ON tableB.col2 = A2.key
JOIN tableA as A3 ON tableB.col3 = A3.key;
You should probably put the last 2 items, 'loves' and 'hates', into a separate table as they represent a different type of data than the other 3.
Here are the tables:
users:
id name
----------
1 John
2 Edward
3 Kate
feelings:
id type
----------
1 love
2 hate
feelings_users:
id user1_id feeling_id user2_id
-----------------------------------
1 1 2 2
2 2 1 3
3 3 1 1
and here's the query:
select `Ua`.`name`, `F`.`type`, `Ub`.`name` from `feelings_users` as `X`
left join `users` as `Ua` on `X`.`user1_id` = `Ua`.`id`
left join `feelings` as `F` on `X`.`feeling_id` = `F`.`id`
left join `users` as `Ub` on `X`.`user2_id` = `Ub`.`id`
and it will output:
name type name
--------------------
John hate Edward
Edward love Kate
Kate love John

how can i use distinct INNER JOIN RFM ON RFM.RFMID=PV.RFMID [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get data from multiple table
SELECT PM.PMID,RFM.TITLE,RFM.NAME ,PV.RFMID FROM MMASTER
INNER JOIN AMM ON MMASTER.MID=AMM.MID
INNER JOIN PS ON AMM.AMMID=PS.AMMID
INNER JOIN PV ON PV.VTID=PS.VTID
INNER JOIN RFM ON RFM.RFMID=PV.RFMID
INNER JOIN PM ON PV.PMID==PM.PMID
INNER JOIN SM ON PS.SMID=SM.SMID
WHERE PM.PMID='2'
Table MMASTER
MID col1
1 abc
2 xyx
3 pqr
Table AMM
AMMID MID col1 col2
1 1 bnb mfk
2 1 def rwr
3 2 re wrwr
Table PS
UID VTID AMMID SMID col1 col2
1 1 1 1 rkk jdj
2 2 3 3 kdf lfl
6 2 2 4 rgr rtr
Table PV
VTID PMID RFMID
1 2 1
2 2 3
7 2 2
Table RFM
RFMID title name
1 mr john
2 mr jack
3 mr jim
Table PM
PMID col1 col2
1 df ere
2 rwe rwer
3 rwr fwr
Table SM
SMID MMID col1 col2
1 1 fdf efe
2 1 ddf dfdf
3 2 df ef
I get result like this after fire above query
PMID title name PV.RFMID
2 mr. jim 3
2 mr. jim 3
2 mr. jim 3
I have to use distinct in inner join RFM ON RFM.RFMID=PV.RFMID.
In my query PV.RFMID getting duplicate value when join with RFM and due to that title and name getting duplicate
How can I use distinct in this line
INNER JOIN RFM ON RFM.RFMID = PV.RFMID
You can make a subquery with that INNER JOIN and use DISTINCT
SELECT MID,NAME,SEX,AGE,RFM.TITLE,RFM.NAME ,PV.RFMID
FROM MMASTER
INNER JOIN AMM ON MMASTER.MID=AMM.MID
INNER JOIN PS ON AMM.AMMID=PS.AMMID
INNER JOIN PV ON PV.VTID=PS.VTID
INNER JOIN
(
SELECT DISTINCT RFMID, NAME
FROM RFM
) RFM
ON RFM.RFMID=PV.RFMID
INNER JOIN PM ON PV.PMID==PM.PMID
INNER JOIN SM ON PS.SMID=SM.SMID
WHERE PM.PMID='13'
use distinct after select. no need to use at inner join line..
SELECT DISTINCT MID,NAME,SEX,AGE,RFM.TITLE,RFM.NAME
FROM MMASTER
INNER JOIN AMM ON MMASTER.MID=AMM.MID
INNER JOIN PS ON AMM.AMMID=PS.AMMID
INNER JOIN PV ON PV.VTID=PS.VTID
INNER JOIN RFM ON RFM.RFMID=PV.RFMID
INNER JOIN PM ON PV.PMID==PM.PMID
INNER JOIN SM ON PS.SMID=SM.SMID
WHERE PM.PMID='13'
if you want all unique data.