Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Need SQL help
Message
 
To
20/07/2010 13:26:58
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Title:
Miscellaneous
Thread ID:
01473141
Message ID:
01473171
Views:
46
>After thinking about this some more I realized I don't really need anything from the parent table. Everything I need can be had from the child table. Just 3 fields CUSTID, _YEAR, and AMOUNT, where I select only the most recent year. This should simplify it a bit. It should look something like:
>
>select custid, max(_year), amount ;
>from orders ;
>group by custid 
>
>
>This seems to work with the first 2 fields but fails when I add the third even when I tweak the GROUP BY clause.


Then use just a part of the query :-)))
CREATE CURSOR Customers (CustId I, Name C(200))
FOR lnFor = 1 TO 20
    INSERT INTO Customers VALUES (lnFor, [Name ]+TRANSFORM(lnFor))
NEXT

CREATE CURSOR Orders (CustId I, Date D, Amount N(10,2))
INSERT INTO Orders VALUES (1, DATE(), 200)
INSERT INTO Orders VALUES (1, DATE(), 100)
INSERT INTO Orders VALUES (1, DATE(), 50)
INSERT INTO Orders VALUES (1, DATE()-1, 200)
INSERT INTO Orders VALUES (1, DATE()-1, 2300)

INSERT INTO Orders VALUES (2, DATE(), 800)
INSERT INTO Orders VALUES (2, DATE(), 900)
INSERT INTO Orders VALUES (2, DATE(), 150)
INSERT INTO Orders VALUES (2, DATE()-1, 1200)
INSERT INTO Orders VALUES (2, DATE()-1, 32300)




SELECT Orders.CustId,;
       Orders.Date,;
       SUM(Orders.Amount) AS Amount;
FROM Orders;
     INNER JOIN (SELECT CustId,;  
                        MAX(Date) AS Date;
                        FROM Orders;
                 GROUP BY CustId) TblMaxdate;
           ON Orders.CustId = TblMaxdate.CustId AND;
               Orders.Date   = TblMaxdate.Date;
GROUP BY Orders.CustId, Orders.Date
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Reply
Map
View

Click here to load this message in the networking platform