Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
More Complicated Query
Message
From
15/05/2008 05:01:32
 
 
To
14/05/2008 22:42:00
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01317209
Message ID:
01317234
Views:
8
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
Previous
Reply
Map
View

Click here to load this message in the networking platform