How to pivot this - sql

BA21 YEAR 109 10 119 EBC
BA21 YEAR 9 0 9 FP
BA21 YEAR 3 0 3 BC
My data is like this and I need to convert this to
BA21 YEAR 109 10 119 EBC 9 0 9 FP 3 0 3 BC
How to do this?

THis does not seem like PIVOTing, it more seems like a self join.
Something like
SELECT t1.*,t2.column1,t2.column2,...,t3.column1,t3.column2,...
FROM Table1 t1 INNER JOIN
Table2 t2 ON t1.PrimaryKey = t2.PrimaryKey INNER JOIN
Table3 t3 ON t1.PrimaryKey = t3.PrimaryKey
where you are selecting only the columns from Table2 and Table3 that you need.
Also, this can be changed to LEFT joins if required. Have a look at Introduction to JOINs – Basic of JOINs for JOIN explenations.

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.

The DATEDIFF function return 2 row

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, ...

Join two tables, using value from the first unless it is null, otherwise use value from the second

I have three tables which look like those:
TABLE 1
id j_id
1 1
2 2
3 3
TABLE 2
id j_id table1_id
1 57 1
2 84 1
3 1 1
4 9 2
5 2 2
and every j has a value in a third table
id value
1 1abc
2 2bcd
3 3abc
57 57abc
84 84abc
9 9abc
I am trying to write a query which will join table 1 and table 2 and use the J value from the third table instead of the j_id, but the problem is that I want to use the j value from the second table if it exists and otherwise use the value from the first table.
in order the make it clearer this is my query result without using the third table:
tbl1.j_id tbl2.j_id
1 1
1 84
1 57
2 2
2 9
3 null
I want the end query result to use the second table's j value unless it is null:
tbl1.j_id tbl2.j_id j_id
1 1 1abc
1 84 84abc
1 57 57abc
2 2 2abc
2 9 9abc
3 null 3abc
(Question and title edits are more than welcome, weren't that sure how to phrase them..)
You can simply JOIN to table3 on the COALESCE of table2.j_id and table1.j_id:
SELECT t1.j_id AS t1_j_id, t2.j_id AS t2_j_id, t3.value
FROM table1 t1
LEFT JOIN table2 t2 ON t2.table1_id = t1.id
JOIN table3 t3 ON t3.id = COALESCE(t2.j_id, t1.j_id)
Output:
t1_j_id t2_j_id value
1 1 1abc
1 57 57abc
1 84 84abc
2 2 2bcd
2 9 9abc
3 null 3abc
Demo on dbfiddle
One solution is to left join table3 twice:
select
t1.j_id,
t2.j_id,
coalesce(t31.value, t32.value) j_value
from
table1 t1
left join table2 t2 on t2.table1_id = t1.id
left join table3 t31 on t31.id = t2.j_id
left join table3 t32 on t32.id = t1.j_id

SQL Server - Using Join to show only entries from second table

I am using SQL Server
The data that I have is:
table1:
R_Time ID Q1
2012-02-26 14 8
2012-02-27 14 7
2012-02-27 15 8
2012-02-27 16 9
2012-02-27 11 10
2012-02-28 11 6
2012-02-28 14 10
2012-02-28 15 9
and
table2:
ID Supervisor
11 2
14 2
15 3
16 3
What i am trying to due is only show R_Time and Q1 Entries from table1 where table2 Supervisor is 3
I know I will somehow have to do a join but im not quite sure how to.
Thanks.
Description
You can use a inner join to get this done.
T-SQL INNER JOIN operator can be used in any FROM clause to combine records from two tables.
Sample
Select tbl1.R_Time, tbl1.Q1 from table1 tbl1
inner join table2 tbl2 on tbl2.Id = tbl1.Id
where tbl2.Supervisor = 3
More Information
T-SQL - Inner Join
hope it helps!
select t2.time
from table1 t1
inner join table2 t2
on t1.Id = t2.Id
where t2.Supervisor = 3
Yup, you need to do an inner join:
select
a.r_time, a.q1
from
table1 a (nolock)
inner join table2 b (nolock) on b.id = a.id
where
b.supervisor = 3

How to combine these tables?

In sql server 2005, how do I get the result set in table3 below? table3 is created by combining table1 and table2 like this:
table1.empid
table1.ticket
table2.identid
And update against table1 joined to table2 doesn't work because empid isn't unique. If need to increment to the next table2.indentid if the ID is already being used by that employee.
Also, table3 below isn't created yet. It's a generated set from table1 and table2. I'm using table3 as an example of what the generated set should look like.
table1
empid ticketid indentid
1 7 20
1 9 4
2 9 21
table2
indentid empid
90 1
91 1
92 2
table3
empid ticketid table1_indentid table2_identid
1 7 20 90
1 9 4 91
2 9 21 92
The only connection between table1 and table2 is empid.
The lines 1 and 2 of your table3 example have the same empid, so they should have the same table2_identid as well. Your example is not proper, or not possible to get with a query on the existing data.
But maybe this is what you want:
If you join the tables like so
SELECT empid, ticketid, t1.indentid as table1_indentid, t2.identid as table2_identid
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.empid = t2.empid
you will get
table3
empid ticketid table1_indentid table2_identid
1 7 20 90
1 7 20 91
1 9 4 90
1 9 4 91
2 9 21 92
This is what you need:
select t1.empid, t1.ticketid,
t1.indentid as table1_indentid,
t2.indentid as table2_identid
from table1 t1
inner join table2 t2 on t1.empid = t2.empid
You should however consider reviewing your data structure.