Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to solve this SQL Select?
Message
 
 
To
27/02/2006 16:37:57
Cindy Winegarden
Duke University Medical Center
Durham, North Carolina, United States
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01099608
Message ID:
01099695
Views:
14
>>I have the following SQL that sometimes gives me erroneous results:
>
>Try this:
>Create Cursor T1 (Pk C(1))
>Insert Into T1 Values ("A")
>Insert Into T1 Values ("B")
>
>Create Cursor T2 (Pk C(1), Fk C(1), Value I)
>Insert Into T2 Values ("X", "A", 100)
>Insert Into T2 Values ("Y", "A", 200)
>Insert Into T2 Values ("Z", "B", 500)
>
>Create Cursor T3 (Fk C(1), Value I)
>Insert Into T3 Values ("X", 1000)
>Insert Into T3 Values ("X", 2000)
>Insert Into T3 Values ("Y", 3000)
>Insert Into T3 Values ("Y", 4000)
>	
>Select ;
>	T1.Pk, ;
>	T2Result.SumT2value, ;
>	T3Result.SumT3Value ;
>From ;
>	T1 ;
>	Left Join ;
>		(Select T2a.Fk, Sum(T2a.Value) As SumT2Value From T2 T2a Group By 1) As T2Result ;
>			On T1.Pk = T2Result.Fk ;
>	Left Join ;
>		(Select T1a.Pk As Fk, Sum(T3.Value) As SumT3Value ;
>			From T1 T1a ;
>			Left Join T2 T2b On T1a.Pk = T2b.Fk ;
>			Left Join T3 On T2b.Pk = T3.Fk Group By 1) As T3Result ;
>		On T1.Pk = T3Result.Fk ;
>Group By 1
Cindy,

You suggestion above works, in general. But I simplified it a bit.
That is, I don't understand (and I guess therefore don't see the need) for the more complicated second subquery. My second subquery is almost the same as the first one, except it selects records from T3. Why did you choose to select from T1 and JOIN T2 and T3 in the second subquery? I just want to make sure I am not missing something that will come back to bite me.

Thank you.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform