Board index » delphi » sum up with different conditions in SQL

sum up with different conditions in SQL

My SQL problem cut down to an easy example :

A table with parts (numbers and names) and a table with amounts and an
action ID for receipts, sales and losses etc.

table parts:    ID , PARTNO, PARTNAME
table bookings  : BOOKID, ACTION, AMOUNT,  REPORTDATE

ACTION=1 for sales, ACTION=2 for receipts
ACTION=3 for losses, ACTION=4 for returns

How can I generate a list for a certain date that looks like this?

BOOKNO  , BOOKNAME , SALES, RECEIPTS, LOSSES, RETURNS
345678  , red Roses, 16   , 12      , 0     , 2
345679  
...

How can I sum up the same column with all different conditions in a
single SQL statement?

Thank you.

 

Re:sum up with different conditions in SQL


On Tue, 04 Jan 2000 08:35:52 GMT, b...@mythos.in-berlin.de (Matthias

Quote
Nolting) wrote:
>My SQL problem cut down to an easy example :

>A table with parts (numbers and names) and a table with amounts and an
>action ID for receipts, sales and losses etc.

>table parts:    ID , PARTNO, PARTNAME
>table bookings  : BOOKID, ACTION, AMOUNT,  REPORTDATE

>ACTION=1 for sales, ACTION=2 for receipts
>ACTION=3 for losses, ACTION=4 for returns

>How can I generate a list for a certain date that looks like this?

>BOOKNO  , BOOKNAME , SALES, RECEIPTS, LOSSES, RETURNS
>345678      , red Roses, 16   , 12      , 0     , 2
>345679  
>...

>How can I sum up the same column with all different conditions in a
>single SQL statement?

What database back-end are you using? I can explain the process for local
table types (dBASE, Paradox, FoxPro) in the context of local SQL.
(Follow-ups to the newsgroup only, please.)

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Steve Koterski              "Health nuts are going to feel stupid someday,
Felton, CA                  lying in hospitals dying of nothing."
                                                              -- Redd Foxx

Re:sum up with different conditions in SQL


On Tue, 04 Jan 2000 21:19:00 GMT, koter...@NOSPAMgte.net (Steve
Quote
Koterski) wrote:

>What database back-end are you using? I can explain the process for local
>table types (dBASE, Paradox, FoxPro) in the context of local SQL.
>(Follow-ups to the newsgroup only, please.)

Thank you steve,  
I am using dbase tables with local BDE.
Matthias

Re:sum up with different conditions in SQL


On Wed, 05 Jan 2000 01:07:56 GMT, b...@mythos.in-berlin.de (Matthias

Quote
Nolting) wrote:

[...]

Quote
>Thank you steve,  
>I am using dbase tables with local BDE.

What you described in your first post is what is known as a cross-tab
query. There is no inherent support in standard SQL (or local SQL) for this
sort of query and so its requires multiple steps to accomplish. In local
SQL, use two queries. The first retrieves data in an imperfect,
intermediate form. This statement acts like an SQL view would, its result
used as the source for a second statement. In the second statement, the
data massaged in the first statement is refined to produce the final result
set.

In the first statement, use multiple SELECT statements concatenated into a
single result set with a UNION join. Each SELECT retrieves a subset of the
total rows in the table, based on the value in your ACTION column. Each has
the same number of columns, but provides a value of 1 (one) for one of the
columns SALES, RECEIPTS, LOSSES, and RETURNS, and a value of zero for the
rest of these columns. For example:

  /* Query for ACTION value of 1 (Sales) */
  SELECT BOOKID, PARTNAME, 1 AS SALES, 0 AS RECEIPTS,
    0 AS LOSSES, 0 AS RETURNS
  FROM "Bookings.dbf"
    INNER JOIN "Parts.dbf"
      ON (BOOKID = PARTNO)
  WHERE (ACTION = 1)
  UNION ALL
  /* Query for ACTION value of 2 (Receipts) */
  SELECT BOOKID, PARTNAME, 0, 1, 0, 0
  FROM "Bookings.dbf"
    INNER JOIN "Parts.dbf"
      ON (BOOKID = PARTNO)
  WHERE (ACTION = 2)
  UNION ALL
  /* Query for ACTION value of 3 (Losses) */
  SELECT BOOKID, PARTNAME, 0, 0, 1, 0
  FROM "Bookings.dbf"
    INNER JOIN "Parts.dbf"
      ON (BOOKID = PARTNO)
  WHERE (ACTION = 3)
  UNION ALL
  /* Query for ACTION value of 4 (Returns) */
  SELECT BOOKID, PARTNAME, 0, 0, 0, 1
  FROM "Bookings.dbf"
    INNER JOIN "Parts.dbf"
      ON (BOOKID = PARTNO)
  WHERE (ACTION = 4)
  ORDER BY 1, 2

Then save this query to a text file (with a .SQL filename extension),
FIRST.SQL for example. This allows it to be used as if it were a table in
the second statement.

In the second statement (executed from a TQuery or a saved query file)
aggregates the data from the first statement, grouping on the book ID and
name. Apply the SUM function to each of the columns SALES, RECEIPTS,
LOSSES, and RETURNS returned by the first query and it will produce a count
of each for every book.

  SELECT BOOKID, PARTNAME, SUM(SALES), SUM(RECEIPTS),
    SUM(LOSSES), SUM(RETURNS)
  FROM "FIRST.SQL"
  GROUP BY BOOKID, PARTNAME

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Steve Koterski              "Health nuts are going to feel stupid someday,
Felton, CA                  lying in hospitals dying of nothing."
                                                              -- Redd Foxx

Re:sum up with different conditions in SQL


On Wed, 05 Jan 2000 16:27:34 GMT, koter...@NOSPAMgte.net (Steve

Quote
Koterski) wrote:

>What you described in your first post is what is known as a cross-tab
>query. There is no inherent support in standard SQL (or local SQL) for this
>sort of query and so its requires multiple steps to accomplish. In local
>SQL, use two queries. The first retrieves data in an imperfect,
>intermediate form. This statement acts like an SQL view would, its result
>used as the source for a second statement. In the second statement, the
>data massaged in the first statement is refined to produce the final result
>set.
> ...

Though it looked easy and it doesn't seem possible in a single
standard sql query.
Thank you, I have tried half a day and it is a good way to learn SQL.

Matthias

Other Threads