问题
This is extended from previous question regarding Query Top Three Reference ID. Since "level" column become dynamic and I need to keep it while display on gridview.
My plan to create 2 dataset (Parent and Child).
1)
Regarding Child Dataset already have a answer (thanks to Rudolf) like below:-
Table Test
ID Name RefID
A AAAA null
B BBBB A
C CCCC B
D DDDD C
E EEEE D
F FFFF E
Sql Statement
select ID, 'Level(' + cast(level as varchar(255)) + ')' GroupLevel, RefID from (
select t0.ID,
case level.level
when 1 then t1.ID
when 2 then t2.ID
when 3 then t3.ID
end RefID,
level.level
from Test t0
left outer join Test t1 on t1.RefID = t0.ID
left outer join Test t2 on t2.RefID = t1.ID
left outer join Test t3 on t3.RefID = t2.ID
cross join (
select 1 level union all
select 2 union all
select 3
) level
) t
where t.RefID is not null
Output:
ID | GroupLevel | RefID |
---+------------+-------+
A | Level(1) | B |
A | Level(2) | C |
A | Level(3) | D |
B | Level(1) | C |
B | Level(2) | D |
B | Level(3) | E |
C | Level(1) | D |
C | Level(2) | E |
D | Level(1) | E |
Now I need to create Parent Dataset like below:-
Output in GridView with "+" and "-"
Introducer Level Total Id
A Level (1) 2
- Id Name
-- -----
B BBBB
C CCCC
A Level(2) 1
- Id Name
-- -----
D DDDD
Before that, I need to create sql statement with Count(*) to get total id based table above. I try many time to place it on Rudolf statement but can get total correctly:-
select ID,
'Level(' + cast(level as varchar(255)) + ')' GroupLevel,
RefID,
*second try - result error
count(*) as reccount*
from (
select t0.ID,
case level.level
when 1 then t1.ID
when 2 then t2.ID
when 3 then t3.ID
end RefID,
*first try - result count double
count(*) as reccount*
level.level
from Test t0
left outer join Test t1 on t1.RefID = t0.ID
left outer join Test t2 on t2.RefID = t1.ID
left outer join Test t3 on t3.RefID = t2.ID
cross join (
select 1 level union all
select 2 union all
select 3
) level
) t
where t.RefID is not null
I can figure out where to put count statement to get total record based level group.
2)
I also need to add column and sum value from any related table. Let say I have table child for each id like below:-
Table Child
ID Date Join Child
B XX/XX/XXXX 2
C XX/XX/XXXX 5
My desire output should be:-
Introducer Level Total Id Total Child
A Level (1) 2 7
- Id Name Date Join Child
-- ---- --------- -----
B BBBB XX/XX/XX 2
C CCCC XX/XX/XX 5
A Level(2) 1 0
- Id Name Date Join Child
-- ---- --------- ------
D DDDD XX/XX/XX Null
I am very appreciate and thanks on advance for yours time and read this thread and hope anybody cross this problem have a solutions regarding this questions please share with me.
Thanks you.
回答1:
I found answer for question no 2
" I also need to add column and sum value from any related table. Let say I have table child for each id like below:-
Table Child
ID Date Join Child
B XX/XX/XXXX 2
C XX/XX/XXXX 5
On sql statement I put:-
.....
(Select
1 As level
union All
Select
2
union All
Select
3) level) t Left Join
child On t.RefID = child.memberid
......
where I get my desired output like below:-
Id Name Date Join Child
-- ---- --------- -----
B BBBB XX/XX/XX 2
C CCCC XX/XX/XX 5
By using Left Join I can scan and show all Table Child related with RefID either exist on not as Child dataset.
I still need to show child = 0 if record not found in Table Child instead of null or blank value.
I am very appreciate if yours all can improve this solutions. Maybe have others sql statement more effective and faster than this.
And, please help me to figure out regarding COUNT and SUM on Parent dataset. I still no idea how to overcome this problem yet.
Thanks you.
回答2:
After try many time with sql builder finally I found a solutions even not so elegance but for time being it worth and work for my current situation. I can operate COUNT, SUM and Join Table for question No 1 and No 2 without problem so far.
Actually are very sweet where the statement not so complicated as what I imaging at all. All this thanks should goes to Rudolf, joop and and jakub kania where yours all give me some aura to encourage me think more to find a solutions.
Here I put sql statement where I can count total record based Group Level and join a Table Child where in same time I can get sum total no child based id and refid.
Select distinct
t.ancestor,
t.level,
sum(t.trec) as xrec,
sum(tchild) as xchild
From
(Select Distinct
Count(t0.id) As trec,
t0.refid,
t0.id,
Case level.level When 1 Then t1.id When 2 Then t2.id When 3 Then t3.id
End As ancestor,
level.level
From
......
and this is a result
Parent Dataset
If anybody found this is useful and help for their project than I am glad for sharing it.
Thanks stackoverflow anyway..
来源:https://stackoverflow.com/questions/35621931/how-to-count-and-sum-in-same-table-to-become-parent-dataset