SELECT film_id, film_actor.actor_id,first_name,last_name,COUNT(*)
FROM film_actor
INNER JOIN actor ON film_actor.actor_id=actor.actor_id
GROUP BY film_actor.actor_id ;
SELECT
film_id,? ---(Which table ? you can neglect)
film_actor.actor_id,
first_name,
last_name,
COUNT(*) AS Total
FROM film_actor
INNER JOIN actor
ON film_actor.actor_id =actor.actor_id
GROUP BY film_actor.actor_id ;
Best practice for this SQL statement will be like
SELECT
c.film_id,
a.actor_id,
b.first_name,
b.last_name
a.Count
FROM
(
SELECT
film_actor.actor_id,
COUNT(*) AS Count
FROM film_actor
INNER JOIN actor
ON film_actor.actor_id =actor.actor_id
GROUP BY film_actor.actor_id ;
) AS a
INNER JOIN actor b ON b.actor_id = a.actor_id
INNER JOIN film_actor c ON c.actor_id = a.actor_id
Related
Say select id from some_expensive_query is the cte I want to share. Currently I write two sql in a transaction:
with t as (select id from some_expensive_query) select * from t1 join t on t.id =t1.id;
with t as (select id from some_expensive_query) select * from t2 join t on t.id =t2.id;
As you can see, the cte is executed twice but I want something like:
t = select id from some_expensive_query;
select * from t1 join t on t.id =t1.id;
select * from t2 join t on t.id =t2.id;
for portability, I don't want to use pgsql or functions, anyway to solve this?
Why don't you use union all ?
with t as (select id from some_expensive_query)
select * from t1 join t on t.id =t1.id
union all
select * from t2 join t on t.id =t2.id;
I have a stored procedure in which I cannot add GROUP BY. I need to somehow take the number of records from another table. From the back-end come filter parameters (samples) for one table from it, I want to get data on the number of records.
SELECT cei_mot_count.cnt FROM someTable AS st
LEFT JOIN CEI_Motivations AS cei_mot ON cei_mot.Id= st.ID
--LEFT OUTER JOIN (SELECT Id, StayingID, COUNT(*) as cnt FROM CEI_Motivations GROUP BY Id,StayingID) as cei_mot_count ON cei_mot_count.StayingID = cei_mot.StayingID
--LEFT OUTER JOIN (SELECT Id, COUNT(cei_mot.Id) as cnt FROM cei_mot GROUP BY Id) as cei_mot_count ON cei_mot_count.Id IS NOT NULL
OUTER APPLY (SELECT COUNT(mot.Id) as cnt
FROM cei_mot as mot
WHERE mot.Id IS NOT NULL
GROUP BY mot.Id) as cei_mot_count
i catch Exception: Invalid object name 'cei_mot'
You can't use a table alias from a join inside an apply. I think what you want is:
SELECT cei_mot_count.cnt FROM someTable AS st
LEFT JOIN CEI_Motivations AS cei_mot ON cei_mot.Id= st.ID
--LEFT OUTER JOIN (SELECT Id, StayingID, COUNT(*) as cnt FROM CEI_Motivations GROUP BY Id,StayingID) as cei_mot_count ON cei_mot_count.StayingID = cei_mot.StayingID
--LEFT OUTER JOIN (SELECT Id, COUNT(cei_mot.Id) as cnt FROM cei_mot GROUP BY Id) as cei_mot_count ON cei_mot_count.Id IS NOT NULL
OUTER APPLY (SELECT COUNT(mot.Id) as cnt
FROM CEI_Motivations as mot
WHERE mot.Id IS NOT NULL AND mot.Id= st.ID
GROUP BY mot.Id) as cei_mot_count
Note how this version uses CEI_Motivations twice independently.
I have 2 tables.
Table 1:
Id Name
1 John
2 Mike
3 Sam
Table 2:
Name Data
John Data1
John Data1
John Data1
Mike Data2
Mike Data2
Sam Data3
If I write
select Table2.Name, Table2.Data
from Table1
inner join Table2 on Table1.Name= Table2.Name
I get all the duplicate data.
I would like to be able to retrieve something like:
John Data1
Mike Data2
Sam Data3
SELECT DISTINCT NAME
, DATA
FROM Table2
WHERE NAME IN (SELECT NAME
FROM Table1)
There are a few different options here...
There are two ways to do that.
You can use distinct clause:
select distinct t2.Name, t2.Data
from Table1 t1
inner join Table2 t2 on t1.Name= t2.Name
Here is link to MSDN.
You can use group by :
select t2.Name, t2.Data
from Table1 t1
inner join Table2 t2 on t1.Name= t2.Name
group by t2.Name, t2.Data
Here is link to MSDN.
I prefer second solution, because I always can add grouping functions.
Note:
In both queries I used aliases (t1,t2). It's more readable.
You can use CTE for this and apply a row_number()
;with cte as
(
select t1.name, t2.data,
row_number() over(partition by t1.id order by t1.id) rn
from table1 t1
inner join table2 t2
on t1.name = t2.name
)
select *
from cte
where rn = 1
Or a non-CTE version:
select *
from
(
select t1.name, t2.data,
row_number() over(partition by t1.id order by t1.id) rn
from table1 t1
inner join table2 t2
on t1.name = t2.name
) x
where rn = 1
see SQL Fiddle with Demo
Are you looking for something like:
select Table2.Name, Table2.Data, count(*) from Table1
inner join Table2 on Table1.Name= Table2.Name
group by Table2.Name, Table2.Data;
OK so I have a query I am trying to build.. I have 2 tables, table1 has a bunch of regular records as normal with a unique ID (auto increment) and table2 has records that include some of those ids from table1. I am trying to order by the highest records with that same ID in table1.. Heres what I've got:
SELECT * FROM table1
WHERE table1.status = 1
AND (SELECT COUNT(*) FROM table2 WHERE table2.tbl1_id = table1.id)
ORDER BY table1.id DESC
Thanks :)
SELECT table1.id
FROM table1
LEFT JOIN table2 ON table2.tbl1_id = table1.id
WHERE table1.status = 1
GROUP BY table1.id
ORDER BY COUNT(table2.tbl1_id) DESC
Try this:
SELECT a.*, b.cnt
FROM table1 a LEFT JOIN
(
SELECT tbl1_id, COUNT(*) cnt
FROM table2
GROUP BY tbl1_id
) b
ON a.id = b.tbl1_id
WHERE table1.status = 1
ORDER BY cnt DESC
I wrote this, and it is wrong syntax, help me fix it, I want 'T' to be an alias of the result of the two inner joins.
select T.id
from table1
inner join table2 on table1.x = table2.y
inner join table3 on table3.z = table1.w as T;
You cannot use aliases to name the "entire" join, you can, however, put aliases on individual tables of the join:
select t1.id
from table1 t1
inner join table2 t2 on t1.x = t2.y
inner join table3 t3 on t3.z = t1.w
In the projection, you will have to use the alias of the table, which defines the id column you are going to select.
You can't directly name the result of a join. One option is to use a subquery:
select T.id
from (
select *
from table1
inner join table2 on table1.x = table2.y
inner join table3 on table3.z = table1.w
) T
Another option is subquery factoring:
with T as (
select *
from table1
inner join table2 on table1.x = table2.y
inner join table3 on table3.z = table1.w
)
select T.id
from T