Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Conditionaly sum
Message
 
To
18/08/2006 00:50:24
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01146640
Message ID:
01146650
Views:
12
>Dear Experts
>
>Table1 has following two fields
>
>code -----n(4)
>amount----n(2)
>
>The data in both fields is as under
>
>code-------amount
>1-------------
>1401--------50
>1602--------80
>2-------------
>2202--------40
>2808--------10
>
>Now I want to get the sum of 4 digit codes in 1 digit as
>
>1------------130
>1401--------50
>1602--------80
>2-------------50
>2202--------40
>2808--------10
>
>Explanation
>The sum of all digits that are starts with 1 with wll be written in 1 as
>The sum of 1401 and 1602 will be written against 1 as 130
>
>1-----------130
>1401--------50
>1602--------80
>
>
>Please help
CREATE CURSOR Table1 (Code N(4), Ammount N(2))
INSERT INTO Table1 VALUES (1401,50)
INSERT INTO Table1 VALUES (1602,80)
INSERT INTO Table1 VALUES (2202,40)
INSERT INTO Table1 VALUES (2808,10)

SELECT INT(VAL([000]+LEFT(TRANSFORM(code),1))) AS Code,;
       SUM(Ammount)                            AS Ammount,;
       PADR(LEFT(TRANSFORM(code),1),4)         AS OrdBy;
FROM Table1;
GROUP BY 1,3;
UNION ALL;
SELECT code,;
       SUM(Ammount)                            AS Ammount,;
       PADR(TRANSFORM(code),4)                 AS OrdBy;
FROM Table1;
GROUP BY code,3;
ORDER BY 3;
INTO CURSOR Test
BROWSE NORMAL
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