The DATEDIFF function return 2 row - sql

I have the query that works properly and i get the following output :
9 1116 JOHN 0590056093 9106809105 3 A NULL D
9 1117 SARA 0015562451 9203410410 3 A NULL D
9 1118 DAVID 5560101753 9375115360 3 B NULL D
After adding the datediff column my output is as follows
9 1116 JOHN 0590056093 9106809105 3 A NULL D 10
9 1116 JOHN 0590056093 9106809105 3 A NULL D 1
9 1117 SARA 0015562451 9203410410 3 A NULL D 10
9 1117 SARA 0015562451 9203410410 3 A NULL D 1
9 1118 DAVID 5560101753 9375115360 3 B NULL D 10
9 1118 DAVID 5560101753 9375115360 3 B NULL D 1
What is the reason for displaying the 1 in the datediff column?
Query :
select distinct t1.*, fs.Name+' '+ed.Academic as takhasos, Articles.Title, MT.Name as paye,
datediff(m,Projects.StartDateProject, Projects.EndDateProject) as datedif
from
(select RC.ID, RCU.UserID, u.Name + ' ' + u.Family AS NameFamily, u.UserName, u.Mobile, u.Email, groupED.IdMaghtae
from ResearchersCores AS RC LEFT OUTER JOIN
ResearchersCoreUsers AS RCU ON RC.ID = RCU.ResearchersCoreID LEFT OUTER JOIN
Users AS u ON u.Id = RCU.UserID LEFT OUTER JOIN
(SELECT Eductionals.UserID , Max(Eductionals.MaghtaeID) AS IdMaghtae
FROM Eductionals
GROUP BY Eductionals.UserID) groupED
ON u.Id = groupED.UserID
WHERE (RC.IsEnable = 1) AND (RCU.isEnable = 1) AND (RCU.RoleID = 5) ) t1 left outer join
Eductionals as ED ON ED.UserID = t1.UserID AND t1.IdMaghtae = ed.MaghtaeID left outer join
FieldStudies as FS ON ed.FieldStudy_ID = FS.ID left outer join
Articles ON Articles.UserID = t1.UserID left outer join
Projects ON Projects.RecordID = t1.ID and Projects.ControllerID = 8 left outer join
MaghtaeTahsilis MT On MT.ID = t1.IdMaghtae
where t1.id = 9
I want the following output
9 1116 JOHN 0590056093 9106809105 3 A NULL D 10
9 1117 SARA 0015562451 9203410410 3 A NULL D 10
9 1118 DAVID 5560101753 9375115360 3 B NULL D 10

Your query uses distinct to remove duplicate rows. But the expression that you are adding has different values over these otherwise duplicate rows, so they are now showing in the resultset.
One option would be to use group by instead, and take the max() of datediff. This requires you to repeat all other columns in the select clause in the group by clause as well:
select
t1.col1,
t1.col2,
...,
max(datediff(m,Projects.StartDateProject, Projects.EndDateProject)) as datedif
from ...
where t1.id = 9
group by t1.col1, t1.col2, ...

Related

SQL Server : multiple rows single line

I would like to get the representation of one record based on the primary key value from multiple tables. As shown below, each table can have multiple values based on this primary key value.
TABLE-1
ID
NAME
1
AA
2
BB
3
CC
4
DD
5
EE
TABLE-2
ID
SCHOOL
AUT
1
11
A
2
11
A
2
12
B
3
11
A
4
12
A
4
13
B
5
13
A
TABLE-3
ID
TC
1
101
2
102
2
103
2
104
3
105
4
106
4
107
5
108
The result below is the value obtained with an OUTER JOIN.
SELECT
T1.ID, T2.SCHOOL, T3.TC, T2.AUT
FROM
T1
LEFT OUTER JOIN
T2 ON T1.ID = T2.ID
LEFT OUTER JOIN
T3 ON T1.ID = T3.ID
ORDER BY
T1.ID ASC
ID
SCHOOL
TC
AUT
1
11
101
A
2
11
102
A
2
12
102
B
2
11
103
A
2
12
103
B
2
11
104
A
2
12
104
B
3
11
105
A
4
12
106
A
4
13
106
B
4
12
107
A
4
13
107
B
5
13
106
A
How can I get the result like below?
ID
SCHOOL
TC1
TC2
TC3
1
11
101
2
11
102
103
104
3
11
105
4
12
106
107
5
13
108
The important thing here is that in the result value, SCHOOL only shows that AUT is 'A'.
I would appreciate it if you let me know your query.
It looks, from your desired results, you just need to use row_number in combination with a conditional aggregate. Your sample data seems a little inadequate, I can't see any requirement for table1 at all.
Try the following:
with t as (
select t2.id,t2.school,t3.tc, Row_Number() over(partition by t2.id order by t3.tc) col
from t2 join t3 on t2.id=t3.id
where aut='A'
)
select id,school,
max(case when col=1 then tc end) TC1,
max(case when col=2 then tc end) TC2,
max(case when col=3 then tc end) TC3
from t
group by id, school
Example SQL Fiddle
SELECT
T1.ID, T2.SCHOOL,
GROUP_CONCAT(T3.TC),
GROUP_CONCAT(T2.AUT)
FROM
T1
LEFT OUTER JOIN
T2 ON T1.ID = T2.ID
LEFT OUTER JOIN
T3 ON T1.ID = T3.ID
GROUP BY
T1.ID, T2.SCHOOL
WHERE
T2.AUT = ‘A’
ORDER BY
T1.ID ASC
Notice that GROUP_CONCAT concatenates the values in the row.
EDIT: oh my, haven't seen that it's a SQL Server question!
Just replace GROUP_CONCAT with STRING_AGG if you’re using SQL Server 2017 or newer.

SQL JOIN with Team Names from teams table in SQL SERVER

I want to show team name in Match Schedule, I am using below query but it gets me both TeamA & TeamB as same name
SELECT m.ID AS MID, MatchDate,
t.Name AS TeamName, 'v' AS VS,
t.Name AS TeamName, Group
FROM Matches m
JOIN Team T ON m.TeamA_ID = t.Id
JOIN Team T2 ON m.TeamB_ID = t2.Id
How can i get Correct team name based on TeamA_ID, TeamB_ID
Table Team
ID
TeamName
TeamCountry
Table Matches
ID
MatchDate
TeamA_ID
TeamB_ID
Location
Group
ID TeamName TeamCountry
1 TeamOne 100
2 TeamTwo 30
3 TeamThree 80
4 TeamFour 90
5 TeamFive 98
6 TeamSix 99
7 TeamSeven 200
8 TeamEight 14
ID MatchDate TeamA_ID TeamB_ID Location Group
1 01-01-2019 2 4 L1 A
2 02-01-2019 6 1 L1 A
3 04-01-2019 5 8 L1 B
4 06-01-2019 2 6 L1 B
5 10-01-2019 3 4 L1 C
6 16-01-2019 5 6 L1 C
I want result as
ID Date TeamName vs TeamName Location Group
1 01-01-2019 TeamTwo v TeamFour L1 A
2 02-01-2019 TeamSix v TeamOne L1 A
3 04-01-2019 TeamFive v TeamEight L1 B
4 06-01-2019 TeamTwo v TeamSix L1 B
5 10-01-2019 TeamThree v TeamFour L1 C
6 16-01-2019 TeamFive v TeamSix L1 C
Just a simple mistake (you have t.Name twice instead of t2.Name and t.Name)
SELECT m.ID AS MID, MatchDate,
t.Name AS TeamName,
t2.Name AS TeamName,
...
you almost got it.
SELECT m.ID AS MID,
MatchDate,
T.Name AS TeamName,
'v' AS VS,
T2.Name AS TeamName, -- Should be T2.Name
Group
FROM Matches m
JOIN Team T ON m.TeamA_ID = T.Id
JOIN Team T2 ON m.TeamB_ID = T2.Id
In your select you are selecting t.Name twice, change the second one to t2.Name.
SELECT m.ID AS MID, MatchDate,
t.Name AS TeamName, 'v' AS VS,
t2.Name AS TeamName, Group
FROM Matches m
JOIN Team T ON m.TeamA_ID = t.Id
JOIN Team T2 ON m.TeamB_ID = t2.Id

How to get Oracle to return unique results in a one to many relationship tables with a left join

I have a three tables
Table 1
Id Department
1 A
2 B
3 C
4 D
Table 2
Id DepartId Name
1 1 ABC
2 1 DEF
3 1 ASD
4 2 FGH
5 2 HJK
6 3 ZXC
Table 3
Id Depart Area
1 A pp
2 B
3 C nn
4 D oo
I need the result
Id Depart Name Area
1 A ABC pp
2 B FGH Null
3 C ZXC nn
4 D NULL oo
I need one matching entry from table 2 and table 3 to corresponding entry in the table 1
Do a left join to also get t1 rows without any reference in the t2 table. GROUP BY to get only 1 row per Department.
select t1.id, t1.Department, min(t2.Name)
from t1
left join t2 on t1.id = t2.DepartId
group by t1.id, t1.Department
I think I would do this with a correlated subquery:
select t1.*,
(select t2.name
from t2
where t1.id = t2.DepartId and rownum = 1
) as t2name
from t1;
This saves the overhead of an aggregation. An index on t2(DepartId, name) is optimal for this query.
by the way not the answer to your specific question but if instead of just one you want all the names you can use listagg
SELECT t1.id,
department,
LISTAGG (name, ',') WITHIN GROUP (ORDER BY name) names
FROM t1, t2
WHERE t1.id = t2.departId(+)
GROUP BY t1.id, department
ORDER BY 1
ID Department Names
1 A ABC,ASD,DEF
2 B FGH, HJK
3 C ZXC
4 D

Oracle Outer join with 0 value return for non existing rows

I have 2 tables
Person
--------------------
id name dept_id
---------------------
1 x 1
2 y 1
3 z 2
Feedback
------------------------------------
person_id f_date positive negative
------------------------------------
1 2014-05-05 10 4
1 2014-05-15 5 3
2 2014-05-11 3 8
Now my query is
SELECT p.id,
nvl(sum(positive),0) AS pf,
nvl(sum(negative),0) AS nf
FROM person p
LEFT OUTER JOIN feedback f ON p.id = f.person_id
WHERE f_date BETWEEN to_date('2014-05-04', 'YYYY-MM-DD')
AND to_date('2014-05-16', 'YYYY-MM-DD')
GROUP BY p.id
ORDER BY p.id;
I expect to see
id pf nf
---------------
1 15 7
2 3 8
3 0 0
but I don't see the the data for 3. In fact the data i get is only if the rows exist in feedback table as if its an equi join.
Your WHERE clause is turning the outer join into an inner join. The solution is to move the condition to the ON clause:
SELECT p.id, nvl(sum(positive),0) as pf, nvl(sum(negative),0) as nf
FROM person p left outer JOIN
feedback f
on p.id = f.person_id and
f.f_date between to_date('2014-05-04', 'YYYY-MM-DD') AND to_date('2014-05-16', 'YYYY-MM-DD')
GROUP BY p.id
ORDER BY p.id;

Subquery returned more than 1 value in sql server

SELECT * FROM TableC
INNER JOIN TableB ON TableB.mid=TableC.mid
INNER JOIN TableA ON TableA.userid=(
SELECT distinct userid
FROM TableB)
Subquery returned more than 1 value.
Medical_Master
MedicalID MedicalName
1(pk) abc
2 xyx
3 pqr
Child_Medical_Master
ChildMID MedicalID Station Name
1(pk) 1(fk) bnb mfk
2 1 def rwr
3 2 re wrw
Medical_Visit
VTID PMID RFMID age
1(pk) 2(fk) 1 34
2 2 3 45
3 3 1 45
4 1 2 44
5 2 2 76
Medical_Study
UID VTID ChildMID SMID Date time
1(pk) 1(fk) 1 1 kk jdj
2 2 3 2 kdf lfl
6 3 2 3 rgr rtr
Doctor_Master
RFMID Doctorname
1(pk) mr.john
2 mr.jack
3 mr.jim
PAtient_Master
PMID Firstname LastNAme
1(pk) df ere
2 rwe rwer
3 rwr fwr
Study_Master
SMID MedicalID Description Duration
1(pk) 1(fk) fdf efe
2 1 ddf dfdf
3 2 df ef
I want these columns from tables how should be my correct query?
UID,PMID,FIRSTNAME,LASTNAME,AGE,MEDICALNAME,DESCRIPTION,STATION,DATE,DoctorName
Assuming you don't want to do a normal join and there is a purpose to the subquery over a normal join:
You either need to limit what is coming into the subquery like this:
select * from TableC
inner join TableB on TableB.mid=TableC.mid
inner join TableA on TableA.userid=(select distinct userid from TableB where userid=3)
or change your main query like this:
select * from TableC
inner join TableB on TableB.mid=TableC.mid
inner join TableA on TableA.userid in (select distinct userid from TableB)
Okay, got the code and made a sqlfiddle for you to see it working.
select
medical_study.uid,
patient_master.PMID,
patient_master.firstname,
patient_master.surname,
medical_visit.age,
medical_master.medicalName,
study_master.descripto,
child_medical_master.station,
medical_study.dater,
doctor_master.doctorname
from
medical_master
join child_medical_master
on medical_master.medicalID=child_medical_master.medicalID
join medical_study
on child_medical_master.childMID=medical_study.childMID
join medical_visit
on medical_study.VTID=medical_visit.VTID
join doctor_master
on medical_visit.RFMID=doctor_master.RFMID
join patient_master
on medical_visit.PMID=patient_master.PMID
join study_master
on medical_master.medicalID=study_master.medicalID
try this:
select * from TableC
inner join TableB on TableB.mid=TableC.mid
inner join TableA on TableA.userid=TableB.userid