how do i join two different tables in mysql - mysql

This is table1:
id name m1 m2 m3 total itemno
1 raj 10 10 10 30 1
2 ram 60 60 60 180 1
3 kumar 70 70 70 210 1
4 kanna 50 50 50 150 1
5 vivek 64 64 91 200 1
5 vivek 90 90 90 270 2
This is table2:
id name mark1 mark2 mark3 itemno
101 vivek 78 78 78 1
102 vivekkanna 89 88 78 1
103 rajagopalan 97 90 98 1
104 kumar 69 54 56 1
101 vivek 90 90 90 2
I want to join these two tables like this into a result set that looks like this:
id name m1 m2 m3 total mark1 mark2 mark3 item no
1 raj 10 10 10 30 0 0 0 1
2 ram 60 60 60 180 0 0 0 1
3 kumar 70 70 70 210 69 54 56 1
4 kanna 50 50 50 150 0 0 0 1
5 vivek 64 64 91 200 78 78 78 1
5 vivek 90 90 90 270 90 90 90 2

Seems you want a regular LEFT JOIN, returning a default value if the row in table2 does not exist;
SELECT t1.id, t1.name, t1.m1, t1.m2, t1.m3,
COALESCE(t2.mark1, 0) mark1, COALESCE(t2.mark2, 0) mark2, t1.itemno
FROM table1 t1
LEFT JOIN table2 t2 ON t1.name = t2.name AND t1.itemno = t2.itemno
ORDER BY t1.id, t1.itemno
An SQLfiddle to test with
We use a LEFT JOIN to get the default NULL value for all table2 fields that don't match with a row in table1, then COALESCE to turn them into 0 instead.

Related

MySQL: Get top 1 IDs

I am trying to figure out how to select the 1st property ID per client ID that gets associated to the Customer ID. Please help. How would I query this?
PropertyID ClientID CustomerID Date
10 1 35 2004
20 1 35 2004
30 2 35 2004
40 2 35 2004
50 3 35 2004
60 3 35 2004
70 4 35 2004
80 4 35 2004
90 5 35 2004
100 5 35 2004
110 6 35 2005
120 6 35 2005
130 7 35 2005
140 7 35 2005
150 8 35 2005
160 8 35 2005
170 9 35 2005
180 9 35 2005
220 15 37 2007
240 15 37 2007
260 16 37 2007
270 16 37 2007
Expected Result:
PropertyID ClientID CustomerID
10 1 35
30 2 35
50 3 35
70 4 35
90 5 35
110 6 35
130 7 35
150 8 35
170 9 35
220 15 37
260 16 37
Assuming by 1st you mean with lowest propertyId, you can use aggregation in subquery to find the lowest propertyId per clientId and then join the results with the original table to get the other corresponding columns.
select propertyId, clientId, customerId
from your_table t
join (
select clientId,
min(propertyId) as propertyId
from your_table
group by clientId
) t2 using (clientId, propertyId);
This assumes the propertyId is unique (per client at least).
Demo
SELECT MIN(PropertyID) AS PropertyID, ClientID, CustomerID
FROM table_name
GROUP BY ClientID,CustomerID;
http://sqlfiddle.com/#!9/e3dce/1
for example

Percentage by Row Group

I have a matrix with rows grouped by Dept (Department). I am trying to get the actual hours / required hours percentage in a column for each row group, but I can only get the total %, not the % by group. Ex:
I should get this:
Total Employee Req Hrs Rep Hrs % Billable hrs % NonBill Hrs % Time Off %
Dept A Total 672 680 101 575 85 140 21 8 1
Emp1 168 170 101 150 89 50 29 0 0
Emp2 168 165 98 120 71 20 12 8 4
Emp3 168 175 104 155 92 20 12 0 0
Emp4 168 170 101 150 89 50 29 0 0
Dept B Total 420 428 102 365 87 80 19 4 .1
Emp5 168 170 101 150 89 50 29 0 0
Emp6 84 84 98 60 71 10 12 4 4
Emp7 168 175 104 155 92 20 12 0 0
G Total 1092 1108 101 940 86 190 17 12 1
But I get this:
Total Employee Req Hrs Rep Hrs % Billable hrs % NonBill Hrs % Time Off %
Dept A Total 1684 1675 101 1250 86 225 17 12 1
Emp1 168 170 101 150 89 50 29 0 0
Emp2 168 165 98 120 71 20 12 8 4
Emp3 168 175 104 155 92 20 12 0 0
Emp4 168 170 101 150 89 50 29 0 0
Dept B Total 1092 1108 101 1250 86 225 17 12 1
Emp5 168 170 101 150 89 50 29 0 0
Emp6 84 84 98 60 71 10 12 4 4
Emp7 168 175 104 155 92 20 12 0 0
G Total 1092 1108 101 940 86 190 17 12 1
The totals are correct but the % is wrong.
I have several Datasets because the report only runs the department you are in, except for the VPs who can see all departments.
I Insert the percentage columns into the matrix and have tried several expressions with no results including:
=Fields!ActHrs.Value/Fields!ReqHrs.Value
=Sum(Fields!ActHrs.Value, "Ut_Query")/Sum(Fields!ReqHrs.Value, "Ut_Query")
=Sum(Fields!ActHrs.Value, "Ut_Query","Dept")/Sum(Fields!ReqHrs.Value,
"Ut_Query","Dept")
=Sum(Fields!ActHrs.Value,"Dept", "Ut_Query")/Sum(Fields!ReqHrs.Value,
"Dept","Ut_Query")
Plus more I can't even remember.
I tried creating new groups, and even a new matrix.
There must be a simple way to get the percentage by group but I have not found an answer on any of the interned boards.
OK, I figured this out, but it doesn't make much sense. If I try:
=Textbox29/TextBox28 I get error messages about undefined variables.
If I go the the textbox properties and rename the textboxes to Act and Req and use:
=Act/Req I get the right answer.

Update specific records based on some group

I am using SQL Server 2008 R2.
I am having a database table that contains some user data as given below :
Id UserId Sys Dia ReadingType DataId IsDeleted
1 10 98 65 last 1390556024216 0
2 10 99 69 average 1390556024216 0
3 10 102 96 last 1390562788540 0
4 10 102 96 average 1390562788540 0
5 11 130 98 last 1390631241547 0
6 11 130 98 average 1390631241547 0
7 2 285 199 first 1390770562374 0
8 2 250 180 last 1390770562374 0
9 2 267 189 average 1390770562374 0
10 1 258 180 first 1391191009457 0
11 1 258 180 last 1391191009457 0
12 1 258 180 average 1391191009457 0
13 1 285 199 additional 1391191009457 0
14 22 110 78 last 1391549208338 0
15 22 123 83 last 1391549208349 0
In this table, there are records that are having the same DataId but different ReadingType.
I want to set IsDeleted=1 for the records having ReadingType='last' and having a record with ReadingType='average' with the same DataId, Sys, Dia and UserId.
So the Desired result shoul be :
Id UserId Sys Dia Reading DataId IsDeleted
1 10 98 65 last 1390556024216 0
2 10 99 69 average 1390556024216 0
3 10 102 96 last 1390562788540 1
4 10 102 96 average 1390562788540 0
5 11 130 98 last 1390631241547 1
6 11 130 98 average 1390631241547 0
7 2 285 199 first 1390770562374 0
8 2 250 180 last 1390770562374 0
9 2 267 189 average 1390770562374 0
10 1 258 180 first 1391191009457 0
11 1 258 180 last 1391191009457 1
12 1 258 180 average 1391191009457 0
13 1 285 199 additional 1391191009457 0
14 22 110 78 last 1391549208338 0
15 22 123 83 last 1391549208349 0
Here the records with Id 3, 5 and 11 should be marked as deleted as they are having same UserId, Sys, Dia, DataId and ReadingType="last" with another record having ReadingType="average" with same other fields.
Can anyone help me how to find out such a records and update them?
Just use UPDATE with EXISTS subquery:
UPDATE T
SET IsDeleted=1
WHERE
ReadingType='last'
AND
EXISTS(SELECT * FROM T as T1
WHERE T1.ReadingType='average'
AND T1.DataId=T.DataId
AND T1.Sys=T.Sys
AND T1.Dia=T.Dia
AND T1.UserId=T.UserId
)
SQLFiddle demo
You Can solve many way but here i am using the sub-query to solve your problem
UPDATE TABLE SET IsDeleted=1
WHERE DataId=(SELECT DataId FROM TABEL WHERE Reading='last')

select query to get those result where for every unique value of column 3, column 2 has bothval1 and val2

MEMO TYPE ID
2442 11 52
33658 4 52
823 6 56
825 4 56
826 7 56
85 4 57
3298 7 57
87 4 141
377 7 141
88 4 142
378 7 142
98 1 143
99 2 143
194 7 143
7586 5 143
1451 4 143
7781 6 143
3252 4 167
3249 6 167
3915 7 167
13666 5 167
115 4 168
9253 9 168
9254 10 168
138 1 194
139 2 194
1951 4 194
8650 7 194
8191 6 197
8192 7 197
9687 8 197
9930 9 197
I need to select those records from above table where for every unique value in column 'ID', column 'TYPE' have value 6 and 7 both.
Result of this select query would be as below:
MEMO TYPE ID
823 6 56
826 7 56
7781 6 143
194 7 143
3249 6 167
3915 7 167
8191 6 197
8192 7 197
I hope this data is not too much.
select t.*
from your_table t
inner join
(
select id, min(type) as mint, max(type) as maxt
from your_table
where type in (6,7)
group by id
having count(distinct type) = 2
) x on x.id = t.id and t.type in (x.maxt,x.mint)
SQLFiddle demo

mysql several column ranking per group

I have table data in this format
studno name level year term subject1 subject2 subject3
212 victor l1 2000 1 45 56 80
213 HOM l1 2000 1 42 56 70
214 ken l1 2000 1 60 70 50
215 ted l1 2000 1 46 36 47
212 victor l1 2000 2 45 36 68
213 Hom l1 2000 2 38 78 49
214 ken l1 2000 2 38 34 62
my desired output is the following
studno name level year term subject1 sub1rank subject2 sub2rank
213 victor l1 2000 1 42 3 56 2
214 HOM l1 2000 1 60 1 70 1
215 TED l1 2000 1 46 2 36 3
212 victor l1 2000 2 45 2 36 1
213 hOM l1 2000 2 38 3 36 1
214 KEN l1 2000 2 38 3 32 3
215 TED l1 2000 2 90 1 30 4
I have managed to get the rank but the problem is how to get the rank per year, level, term and subject. another problem is that if i use nested statement and try to create view in mysql database it throws an error, "View's SELECT contains a subquery in the FROM clause"
You can do this with correlated subqueries in the select clause. If I understand correctly, something like this:
select t.*,
(select COUNT(distinct t1.subject1)
from t t2
where t2.level = t.level and t2.year = t.year and t2.term = t.term and
t2.subject1 >= t.subject1
) as subj1rank,
(select COUNT(distinct t2.subject2)
from t t2
where t2.level = t.level and t2.year = t.year and t2.term = t.term and
t2.subject2 >= t.subject2
) as subj2rank
from t
The count(*) might be count(distinct subject1) (etc.), depending on how you treat ties.