eid name
1 john
2 pal
3 mike
4 michel
id eid mobile address
1 1 9640253265
2 2 9632587412 hellpp
3 3 poplop
4 4 9638523690
5 1 9632589632
6 2 addr
how to get the below output
name mobilecount addresscount
john 2 0
pal 1 2
mike 0 1
michel 1 0
Something like this:
SELECT t1.name,
COUNT(mobile) AS mobilecount,
COUNT(address) AS addresscount
FROM table1 AS t1
JOIN table2 AS t2 ON t1.eid = t2.eid
GROUP BY t1.name
The above query depends on COUNT function not counting NULL values.
Maybe this will help.
select name,SUM(mobilecount) as mobilecount,SUM(addresscount) as addresscount
from
(
select t1.name,
case when isnull(t2.mobile,'')<>'' then count(mobile) else 0 end as
mobilecount,
case when isnull(t2.address,'')<>'' then count(mobile) else 0 end as
addresscount
from table1 t1,table2 t2
where t1.eid = t2.eid
group by t1.name,t2.mobile,t2.address
)employee
group by name order by name
Related
I'm trying to only fetch unique result of several duplicates in this little complex query:
$sql = "SELECT t1.*, t2.floor_id
FROM dbo.IMP_ELEMENT AS t1
INNER JOIN dbo.IMP_MODEL_GEOMETRY t2 ON CONCAT(t1.Project, '_', t1.element_mark) = CONCAT(t2.Project, '_', t2.element_mark)
WHERE CONCAT(t2.Project, '-', t2.floor_id) LIKE '%$objNr%'";
I expect to only receive unique result from t2.element_mark.
So where should I put in DISTINCT? or is there another way to achieve this?
In t1, Element_mark is unique, but in t2, Element_mark can have duplicates.
So from t1 I like to receive all data. But from t2 I want DISTINCT element_mark. From t2 i only display floor_id, But I need to use Element_mark and Project to match t1 & t2 rows.
EDIT
It doesn't matter which of the duplicates in t2.element_mark I display. t2.floor_id is the same on all duplicates.
**
t1
----------------------------------------------
id project element_mark type ...
0 1 1 A ...
1 1 2 A ...
2 1 3 A ...
t2
----------------------------------------------
id project element_mark flood_id ...
0 1 1 1 ...
1 1 1 1 ...
2 1 2 1 ...
3 1 3 2 ...
As seen in t1, all element_mark is unique. But in t2, element_mark can be seen twice or more.
What I want is to select * from t1.
The id like to select DISTINCT element_mark, floor_id FROM t2 WHERE CONCAT(t1.project, '', t1.element_mark) = CONCAT(t2.project, '', t2.element_mark)
try like below using subquery
SELECT t1.*, t2.floor_id
FROM dbo.IMP_ELEMENT AS t1
INNER JOIN (select distinct project,element_mark,flood_id from dbo.IMP_MODEL_GEOMETRY) t2 ON CONCAT(t1.Project, '_', t1.element_mark) = CONCAT(t2.Project, '_', t2.element_mark)
WHERE CONCAT(t2.Project, '-', t2.floor_id) LIKE '%$objNr%'
If as you say
t2.floor_id is the same on all duplicates.
then your query is fine and you can add DISTINCT in your select
$sql = "SELECT DISTINCT t1.*, t2.floor_id
FROM dbo.IMP_ELEMENT AS t1
INNER JOIN dbo.IMP_MODEL_GEOMETRY t2 ON CONCAT(t1.Project, '_', t1.element_mark) = CONCAT(t2.Project, '_', t2.element_mark)
WHERE CONCAT(t2.Project, '-', t2.floor_id) LIKE '%$objNr%'";
UPDATE:
Your query returns
----------------------------------------------
id project element_mark type flood_id
0 1 1 A 1
0 1 1 A 1
1 1 2 A 1
2 1 3 A 2
Adding Distinct returns
----------------------------------------------
id project element_mark type flood_id
0 1 1 A 1
1 1 2 A 1
2 1 3 A 2
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
I have an inner join on 2 tables.
Consider
Table1
sid name
1 abc
2 xyz
Table2
sdid sid detailname
1 1 a
2 1 b
3 2 x
So my query looks like below
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.sid=t2.sid
the result i get is
sid name sdid sid detailname
1 abc 1 1 a
1 abc 2 1 b
2 xyz 3 2 x
I want to modify this query to get the highest 'sdid' from table 2
my end result should look like
sid name sdid sid detailname
1 abc 2 1 b
2 xyz 3 2 x
Include one more subquery in the join to get the max sdid for each sid from table2.
SELECT t1.sid,t1.name,t2.sdid,t2.sid,t2.detailname
FROM table1 t1
INNER JOIN table2 t2 ON t1.sid=t2.sid
INNER JOIN (select max(sdid) as maxsdid, sid from table2 group by sid) t21
ON t21.sid=t2.sid and t21.sdid = t2.sdid
ID UserId Name Amount RewardId
----------------------------
1 1 James 10.00 1
2 1 James 10.00 2
3 1 James 10.00 3
4 2 Dave 20.00 1
5 2 Dave 20.00 3
6 3 Lim 15.00 2
I'm trying to insert to another table, and this is the result that i'm struggling with:
Tbl1ID RewardId
------------------
1 1
1 2
1 3
4 1
4 3
6 2
I'm trying to get the MIN(ID) of each person and select all the RewardId that belong to that person.
You could do a simple self join to get the minimum id value per userid/rewardid combination;
SELECT MIN(a.id) Tbl1ID, b.RewardId
FROM mytable a
JOIN mytable b
ON a.name = b.name
GROUP BY b.userid, b.rewardid
ORDER BY tbl1id, rewardid;
An SQLfiddle to test with.
If you are running SQL Server 2008+, you can simplify it by using Window Function.
INSERT INTO AnotherTable (Tbl1ID, RewardID)
SELECT MIN(ID) OVER (PARTITION BY Name),
RewardID
FROM SourceTable
SQLFiddle Demo
Try this
SELECT tbl1id,RewardID From
table1 S JOIN
(
SELECT MIN(ID) as tbl1id,Name FROM table1 GROUP BY Name
) T ON T.Name = S.Name
ORDER BY tbl1id
FIDDLE DEMO
Output:
Tbl1ID RewardId
----------------
1 1
1 2
1 3
4 1
4 3
6 2
If you want insert into new table then try this out
Insert into Newtable (tbl1id,RewardID)
SELECT tbl1id,RewardID from
table1 S JOIN
(
SELECT MIN(ID) as tbl1id,Name
FROM table1
GROUP BY Name
) T ON T.Name = S.Name
ORDER BY tbl1id;
FIDDLE DEMO
I have two tables:
Table1
id name qty
1 Tedd 6
2 Jim 7
3 Sally 8
4 Victoria 1
Table2
id name qty
1 Tedd 2
2 Jim 2
3 Sally 2
4 Victoria 1
5 Alex 9
I need to select all the rows from Table1. However, if a row exists in Table2 that doesn't exist in Table1, I need to include that in the result set. So, in the end, my query should return this:
id name qty
1 Tedd 6
2 Jim 7
3 Sally 8
4 Victoria 1
5 Alex 9
Is there a way I can do this? Thanks.
You can use a FULL OUTER JOIN:
select
coalesce(t1.id, t2.id) id,
coalesce(t1.name, t2.name) name,
coalesce(t1.qty, t2.id) qty
from table1 t1
full outer join table2 t2
on t1.id = t2.id
See SQL Fiddle with Demo