2
Table:
FilhoID INT PK,
PaiID INT FK
Query:
SELECT FilhoID, PaiID, (SELECT COUNT(PaiID) FROM tabela WHERE PaiID = FilhoID) as 'Total Filhos' FROM tabela
My doubt:
My SELECT
within the SELECT
, I wanted him to count how many children the father has. I will then check with that number. (I may have reversed the father-son issue, but I believe it doesn’t stop you from helping me)
In the column Total Filhos
, I wish you’d show up like this
FilhoID PaiID Total Filhos
1 null 3
2 1 1
3 1 0
4 1 0
That is to:
Filhoid 1 -> 3 Paiid;
Filhoid 2 -> 1 Paiid;
Filhoid 3 -> 0 Paiid;
Filhoid 4 -> 0 Paiid.
In C#, I send it to a List
Is that clear? They need some more detail?
It worked, just helps me in one more little thing, in this same query I have a Join, as I do with that your answer?
– LeoHenrique
Please update the question with the details (Join) that I update the answer.
– bruno
I managed to do, I put in the 2 querys inside the FROM ( )
– LeoHenrique
@Leohenrique, Perfeito!
– bruno