Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
More Complicated Query
Message
De
15/05/2008 05:01:32
 
 
À
14/05/2008 22:42:00
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
01317209
Message ID:
01317234
Vues:
9
At that point I think you'll need to introduce a "surrogate primary key" for your table - SQL does not work well without them.

(Note: the Sys(2015) PK value in the following example is only for demo purposes, in a real table you can use something like a GUID or a default value as shown in the VFP newID() example.)

hth
-Stefan
* your DDL with an additional PK field
Create Cursor table1 ;
(pk Char(10), date d(8),code Char(7),name Char(30), qty n(4), credit N(12), Debit N(12), code2 Char(7))

Insert Into table1 Values (SYS(2015), {^2008-01-15},'1401001','Eric',0,0,100,'3201001')
Insert Into table1 Values (SYS(2015), {^2008-01-15},'3201001','Apple',10,100,0,'1401001')
Insert Into table1 Values (SYS(2015), {^2008-01-15},'1401001','Eric',0,0,200,'3202001')
Insert Into table1 Values (SYS(2015), {^2008-01-15},'3202001','Potato',20,200,0,'1401001')
Insert Into table1 Values (SYS(2015), {^2008-02-18},'1401001','Eric',0,0,600,'3202001')
Insert Into table1 Values (SYS(2015), {^2008-02-18},'3202001','Potato',40,600,0,'1401001')
Insert Into table1 Values (SYS(2015), {^2008-06-17},'1401002','Boris',0,0,300,'3202001')
Insert Into table1 Values (SYS(2015), {^2008-06-17},'3202001','Juice',20,300,0,'1401002')

*The result must look like this
*month---qty---amt1--amt2---total
*Jan08---30----100----200-----300
*Feb08---40--------0----600-----600
*Jun08---20--------0----300-----300

* SQL
SELECT ;
	T1.date, ;
	( LEFT(CMONTH(T1.date),3) + ;
		RIGHT(TRANSFORM(YEAR(T1.date)),2) ;
		) as month, ;
	SUM(T1.qty) as qty, ;
	SUM(T2.credit) as amt1, ;
	SUM(T3.credit) as amt2, ;
	SUM( NVL(T2.credit,0)+NVL(T3.credit,0) ) as total ;
	FROM table1 T1 ;
	LEFT JOIN table1 T2 ;
		ON T2.pk = T1.pk AND T2.code Like '3201%' ;
	LEFT JOIN table1 T3 ;
		ON T3.pk = T1.pk AND T3.code Like '3202%' ;
	WHERE T1.code Like '320%' ;
	GROUP BY 1, 2 ;
	ORDER BY 1
>Dear Experts
>
>Create Cursor table1 ;
>(date d(8),code Char(7),name Char(30), qty n(4), credit N(12), Debit N(12), code2 Char(7))
>
>Insert Into table1 Values ({^2008-01-15},'1401001','Eric',0,0,100,'3201001')
>Insert Into table1 Values ({^2008-01-15},'3201001','Apple',10,100,0,'1401001')
>Insert Into table1 Values ({^2008-01-15},'1401001','Eric',0,0,200,'3202001')
>Insert Into table1 Values ({^2008-01-15},'3202001','Potato',20,200,0,'1401001')
>Insert Into table1 Values ({^2008-02-18},'1401001','Eric',0,0,600,'3202001')
>Insert Into table1 Values ({^2008-02-18},'3202001','Potato',40,600,0,'1401001')
>Insert Into table1 Values ({^2008-06-17},'1401002','Boris',0,0,300,'3202001')
>Insert Into table1 Values ({^2008-06-17},'3202001','Juice',20,300,0,'1401002')
>
>A shopkeeper wants to see Product wise monthly sale summary
>
>
>The result must look like this
>
>month---qty---amt1--amt2---total
>Jan08---30----100----200-----300
>Feb08---40--------0----600-----600
>Jun08---20--------0----300-----300
>

>Hints:
>amt1 is amount of codes begins with 3201
>amt2 is amount of codes begins with 3202
>total is sum of amt1+amt2
>
>This thread is second part of SOLVED Thread ID # 1316576
>
>Please help
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform