Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL statement ?!

Status
Not open for further replies.

xxyy

Programmer
Aug 16, 2007
4
Is it possible using sql statement to get the following:
From:
----------------------------------------------------------
Fld1 fld2 fld3 fld4
----------------------------------------------------------
100 invoice
100 invoice
100 water 20.00
100 water 30.00
100 water2 15.00
100 water2 25.00
------------------------------------------
To:
----------------------------------------------------------
Fld1 fld2 fld3 fld4
----------------------------------------------------------
100 invoice
100 invoice
100 water 50.00
100 water2 40.00
------------------------------------------
Thanks in advance.
 
The 'Key' to doing it is to use the "Group By" and Sum Clause.

(Code not tested)

Select Fld1, Fld2, Fld3, Sum( Fld4) as Sum_Fld ;
Group by Fld1, Fld2, Fld3 ;
into cursor qResult

Note in this case the first 2 rows would be combined into one record.

Lion Crest Software Services
Anthony L. Testi
President
 
I've already done this.
The problem is that, first two rows I don't want to be merged.
 
You may have to pre-seed the query source with values that won't group together on those first two rows, but will group together on the others. It's counter-intuitive as all get out, but you can't say "GROUP BY ALL ... EXCEPT".

I can't help thinking, though, that somewhere under the hood this is a crosstab waiting to happen. Is your "From" example here your actual source data?
 
danfreeman,

Yes it is from actual source data, but it is simplified to less columns.

It seems that I've to manage this without using SQL statement.

Thanks,
xxyy
 
You can do trickery. Also untested:

Code:
Select Fld1, Fld2, Fld3, Sum( Fld4) as Sum_Fld, iif(Fld1='invoice',SYS(2015),Space(10)) as groupbyexcept ;
Group by 1,2,3,5 ;
into cursor qResult

The iif expression could also be Recno()<3 for example, to keep the first 2 records out of grouping.

But on the other it seems you're mixing up head records with detail records in one table. You coud query the head records and union that result with the rest of the data. Or append the two query results via APPEND.

Bye, Olaf.
 
OK add UNION to the mix
(Untested code)

Code:
Select Fld1, Fld2, Fld3, Sum_Fld ;
Where empty( Fld3 + fld4) ;
UNION ;
Select Fld1, Fld2, Fld3, Sum( Fld4) as Sum_Fld ;
Where not empty( Fld3 + fld4) ;
Group by Fld1, Fld2, Fld3 ;
into cursor qResult

Lion Crest Software Services
Anthony L. Testi
President
 
Olaf and Antony,

Thank you for your replies.

Buy,
xxyy
 
Anthony,

good, but it should be "Fld4 as Sum_Fld" in the first sql of course.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top