pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
help yourself by helping others
8+ messages / 8 participants
[nested] [flat]

* help yourself by helping others
@ 2003-04-04 11:10 Ali Adams <aliadams@doit4u.com>
  2003-07-10 08:24 ` Re: help yourself by helping others Richard Huxton <dev@archonet.com>
  2003-07-10 12:25 ` Re: help yourself by helping others Yasir Malik <ymalik@cs.stevens-tech.edu>
  2003-07-10 13:47 ` Re: help yourself by helping others Rod Taylor <rbt@rbt.ca>
  2003-07-10 15:20 ` Re: help yourself by helping others scott.marlowe <scott.marlowe@ihs.com>
  2003-07-10 19:14 ` Re: help yourself by helping others Jonathan Gardner <jgardner@jonathangardner.net>
  2003-07-10 20:00 ` Re: help yourself by helping others Michael A Nachbaur <mike@nachbaur.com>
  2003-07-13 07:46 ` Re: help yourself by helping others Rajesh Kumar Mallah <mallah@trade-india.com>
  0 siblings, 7 replies; 8+ messages in thread

From: Ali Adams @ 2003-04-04 11:10 UTC (permalink / raw)
  To: pgsql-sql

Dear All,

I am new to Relational Databases and SQL and my background in ODBs is clouding my way to solving what seems to be a simple problem. I am sure many of you have met it many times.

OK, I have a table as follows:


ID Machine   Date             Withdrawals
1  1              01/01/2003    1101
2  2              01/01/2003    2101
3  3              01/01/2003    3101

4  1              02/01/2003    1102
5  2              02/01/2003    2102

6  1              03/01/2003    1103
7  3              03/01/2003    3103

8  2              04/01/2003    2104
9  4              04/01/2003    4104

And i would like to create a monthly withdrawals report as follows:

Machine   Day1  Day2  Day3  Day4 .............................................. Day31
1             11      12      13       0                                                      0
2             21      22      0        24                                                     0
3             31      0        33       0                                                      0
4              0        0       0         0                                                     0

Can you please help?

Many thanks in advance.

Ali
<help yourself by helping others>
www.geocities.com/aliadams

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: help yourself by helping others
  2003-04-04 11:10 help yourself by helping others Ali Adams <aliadams@doit4u.com>
@ 2003-07-10 08:24 ` Richard Huxton <dev@archonet.com>
  6 siblings, 0 replies; 8+ messages in thread

From: Richard Huxton @ 2003-07-10 08:24 UTC (permalink / raw)
  To: Ali Adams <aliadams@doit4u.com>; pgsql-sql

On Friday 04 Apr 2003 12:10 pm, Ali Adams wrote:
> Dear All,
>
> I am new to Relational Databases and SQL and my background in ODBs is
> clouding my way to solving what seems to be a simple problem. I am sure
> many of you have met it many times.
>
> OK, I have a table as follows:
>
>
> ID Machine   Date             Withdrawals
> 1  1              01/01/2003    1101
> 2  2              01/01/2003    2101
> 3  3              01/01/2003    3101
[snip]
> And i would like to create a monthly withdrawals report as follows:
>
> Machine   Day1  Day2  Day3  Day4............ Day31

Two options - either do it in your app or write a crosstab function.

You can extract the day part of the month using:
  SELECT EXTRACT('day' FROM now());

You can find some crosstab functions in the contrib/tablefunc directory of the 
source distribution (or possibly as a package if installed as binary)



-- 
  Richard Huxton



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: help yourself by helping others
  2003-04-04 11:10 help yourself by helping others Ali Adams <aliadams@doit4u.com>
@ 2003-07-10 12:25 ` Yasir Malik <ymalik@cs.stevens-tech.edu>
  6 siblings, 0 replies; 8+ messages in thread

From: Yasir Malik @ 2003-07-10 12:25 UTC (permalink / raw)
  To: Ali Adams <aliadams@doit4u.com>; +Cc: pgsql-sql

I'll try.
In your report, I do not understand how you are getting the withdrawls.
For example, in  your table, you have for machine 1, day one of January,
1101 withdrawls but that does not show in your report.  And what is the
relation of id to machine?
Yasir

On Fri, 4 Apr 2003, Ali Adams wrote:

> Date: Fri, 4 Apr 2003 12:10:06 +0100
> From: Ali Adams <aliadams@doit4u.com>
> To: pgsql-sql@postgresql.org
> Subject: [SQL] help yourself by helping others
>
> Dear All,
>
> I am new to Relational Databases and SQL and my background in ODBs is clouding my way to solving what seems to be a simple problem. I am sure many of you have met it many times.
>
> OK, I have a table as follows:
>
>
> ID Machine   Date             Withdrawals
> 1  1              01/01/2003    1101
> 2  2              01/01/2003    2101
> 3  3              01/01/2003    3101
>
> 4  1              02/01/2003    1102
> 5  2              02/01/2003    2102
>
> 6  1              03/01/2003    1103
> 7  3              03/01/2003    3103
>
> 8  2              04/01/2003    2104
> 9  4              04/01/2003    4104
>
> And i would like to create a monthly withdrawals report as follows:
>
> Machine   Day1  Day2  Day3  Day4 .............................................. Day31
> 1             11      12      13       0                                                      0
> 2             21      22      0        24                                                     0
> 3             31      0        33       0                                                      0
> 4              0        0       0         0                                                     0
>
> Can you please help?
>
> Many thanks in advance.
>
> Ali
> <help yourself by helping others>
> www.geocities.com/aliadams
>



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: help yourself by helping others
  2003-04-04 11:10 help yourself by helping others Ali Adams <aliadams@doit4u.com>
@ 2003-07-10 13:47 ` Rod Taylor <rbt@rbt.ca>
  6 siblings, 0 replies; 8+ messages in thread

From: Rod Taylor @ 2003-07-10 13:47 UTC (permalink / raw)
  To: Ali Adams <aliadams@doit4u.com>; +Cc: pgsql-sql

On Fri, 2003-04-04 at 11:10, Ali Adams wrote:
> Dear All,
>  
> I am new to Relational Databases and SQL and my background in ODBs is
> clouding my way to solving what seems to be a simple problem. I am
> sure many of you have met it many times.

You need to use a pivot table.

Specifically, see crosstab() within contrib/tablefunc in the source
base.

Attachments:

  signature.asc (application/pgp-signature, 186B) download

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: help yourself by helping others
  2003-04-04 11:10 help yourself by helping others Ali Adams <aliadams@doit4u.com>
@ 2003-07-10 15:20 ` scott.marlowe <scott.marlowe@ihs.com>
  6 siblings, 0 replies; 8+ messages in thread

From: scott.marlowe @ 2003-07-10 15:20 UTC (permalink / raw)
  To: Ali Adams <aliadams@doit4u.com>; +Cc: pgsql-sql

On Fri, 4 Apr 2003, Ali Adams wrote:

> Dear All,
> 
> I am new to Relational Databases and SQL and my background in ODBs is clouding my way to solving what seems to be a simple problem. I am sure many of you have met it many times.
> 
> OK, I have a table as follows:
> 
> 
> ID Machine   Date             Withdrawals
> 1  1              01/01/2003    1101
> 2  2              01/01/2003    2101
> 3  3              01/01/2003    3101
> 
> 4  1              02/01/2003    1102
> 5  2              02/01/2003    2102
> 
> 6  1              03/01/2003    1103
> 7  3              03/01/2003    3103
> 
> 8  2              04/01/2003    2104
> 9  4              04/01/2003    4104
> 
> And i would like to create a monthly withdrawals report as follows:
> 
> Machine   Day1  Day2  Day3  Day4 .............................................. Day31
> 1             11      12      13       0                                                      0
> 2             21      22      0        24                                                     0
> 3             31      0        33       0                                                      0
> 4              0        0       0         0                                                     0
> 
> Can you please help?

What you need is a table that has all the dates you want a report on 
already in it to join against.  Just something like:

day
---
1
2
3
4
5
6
7
...
29
30
31  <- or fewer for other months.  

Just make a temp table to hold the days.  Then join against that using 
extract().

If you get stuck let us know.




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: help yourself by helping others
  2003-04-04 11:10 help yourself by helping others Ali Adams <aliadams@doit4u.com>
@ 2003-07-10 19:14 ` Jonathan Gardner <jgardner@jonathangardner.net>
  6 siblings, 0 replies; 8+ messages in thread

From: Jonathan Gardner @ 2003-07-10 19:14 UTC (permalink / raw)
  To: Ali Adams <aliadams@doit4u.com>; pgsql-sql

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Friday 04 April 2003 03:10, Ali Adams wrote:
> Dear All,
>
> I am new to Relational Databases and SQL and my background in ODBs is
> clouding my way to solving what seems to be a simple problem. I am sure
> many of you have met it many times.
>
> OK, I have a table as follows:
>
>
> ID Machine   Date             Withdrawals
> 1  1              01/01/2003    1101
> 2  2              01/01/2003    2101
> 3  3              01/01/2003    3101
>
> 4  1              02/01/2003    1102
> 5  2              02/01/2003    2102
>
> 6  1              03/01/2003    1103
> 7  3              03/01/2003    3103
>
> 8  2              04/01/2003    2104
> 9  4              04/01/2003    4104
>
> And i would like to create a monthly withdrawals report as follows:
>
> Machine   Day1  Day2  Day3  Day4
> .............................................. Day31 1             11     
> 12      13       0                                                      0 2
>             21      22      0        24                                    
>                 0 3             31      0        33       0                
>                                      0 4              0        0       0   
>      0                                                     0
>
> Can you please help?
>

Doing this purely in SQL is difficult. The reason is because there is no way 
to say things like "all the dates in a month, eg, 7/1, 7/2, 7/3, ... 7/31" in 
SQL.

What I would do is select the data using aggregates and "group by" the 
machine. You can specify how many days or months to aggregate across by 
"group by date_trunc("day", Date)" or "date_trunc("month", Date)" or whatever 
you like. You can get some interesting behavior by combining that with 
date_part() (IE, grouping by day of the week, to compare Mondays with 
Tuesdays.)

The data you get back will look something like this:

Machine | Date | Withdrawals
- ---
1 | June 1 | 50
1 | June 7 | 65
1 | June 8 | 12
2 | June 2 | 14
2 | June 9 | 19

When you go to display the code, you populate a list of dates and withdrawals 
for each machine. You fill in '0' for the missing dates. Then displaying it 
is easy. You can also probably iterate over the results if everything is 
sorted, and fill in the missing dates as you encounter them.

- -- 
Jonathan Gardner <jgardner@jonathangardner.net>
(was jgardn@alumni.washington.edu)
Live Free, Use Linux!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE/DbsOWgwF3QvpWNwRAqUiAKCDxyDbzRrIqzLDcZ6jOMESLvooAwCgvlOU
HYWoH/6iRlqhgDB5/KaoYJA=
=rhPR
-----END PGP SIGNATURE-----



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: help yourself by helping others
  2003-04-04 11:10 help yourself by helping others Ali Adams <aliadams@doit4u.com>
@ 2003-07-10 20:00 ` Michael A Nachbaur <mike@nachbaur.com>
  6 siblings, 0 replies; 8+ messages in thread

From: Michael A Nachbaur @ 2003-07-10 20:00 UTC (permalink / raw)
  To: Ali Adams <aliadams@doit4u.com>; pgsql-sql

On Friday 04 April 2003 03:10 am, Ali Adams wrote:
> Dear All,
>
> I am new to Relational Databases and SQL and my background in ODBs is
> clouding my way to solving what seems to be a simple problem. I am sure
> many of you have met it many times.
<snip>

Assuming you can have multiple records per day per machine (e.g. you're 
polling for information throughout the day), the following holds.  However, 
the output doesn't provide for a separate column per day, but rather you get 
a separate row for each day/machine combination.

Attached is a file that demonstrates the behavior I think you would like.  But 
basically, here is the SQL statement you're looking for:

SELECT Datestamp, Machine, SUM(Withdrawls)
FROM WithdrawlsTable
GROUP BY Machine, Datestamp
ORDER BY Datestamp, Machine;

-- 
/* Michael A. Nachbaur <mike@nachbaur.com>
 * http://nachbaur.com/pgpkey.asc
 */

"A thought seemed to strike the woman. It struck her very slowly. You could 
watch it coming in like a long wave on a sandy beach."

Attachments:

pgsql-example.sql (text/x-sql, 2.0K) download CREATE TABLE WithdrawlsTable ( id integer , machine integer , datestamp date , withdrawls integer ); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(1, 1, '01-01-2003', 1101); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(2, 2, '01-01-2003', 2101); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(3, 3, '01-01-2003', 3101); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(4, 1, '02-01-2003', 1102); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(5, 2, '02-01-2003', 2102); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(6, 1, '03-01-2003', 1103); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(7, 3, '03-01-2003', 3103); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(8, 2, '04-01-2003', 2104); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(9, 4, '04-01-2003', 4104); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(11, 1, '01-01-2003', 1101); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(12, 2, '01-01-2003', 2101); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(13, 3, '01-01-2003', 3101); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(14, 1, '02-01-2003', 1102); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(15, 2, '02-01-2003', 2102); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(16, 1, '03-01-2003', 1103); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(17, 3, '03-01-2003', 3103); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(18, 2, '04-01-2003', 2104); INSERT INTO WithdrawlsTable (id, machine, datestamp, withdrawls) VALUES(19, 4, '04-01-2003', 4104); SELECT Datestamp , Machine , SUM(Withdrawls) FROM WithdrawlsTable GROUP BY Machine , Datestamp ORDER BY Datestamp , Machine;
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: help yourself by helping others
  2003-04-04 11:10 help yourself by helping others Ali Adams <aliadams@doit4u.com>
@ 2003-07-13 07:46 ` Rajesh Kumar Mallah <mallah@trade-india.com>
  6 siblings, 0 replies; 8+ messages in thread

From: Rajesh Kumar Mallah @ 2003-07-13 07:46 UTC (permalink / raw)
  To: Ali Adams <aliadams@doit4u.com>; pgsql-sql

dear ali,

something like

select machine,date_part('day' , date) , sum(withdrawals) from 
Table where date_part('month' , date)='month in question' group by
machine,date_part('day' , date) ;

will give you agrregated withdrawals by machine and day,
use the frontend language  for formatting it.

note that it will not display the days for which there
has been no withdrawls. If u need to report them also 0
then create a table that holds 1 year of dates and 
left or right join the output of first query with it.
exact query is not being provided , its just an idea.



regds
mallah.


On Friday 04 Apr 2003 4:40 pm, Ali Adams wrote:
> Dear All,
>
> I am new to Relational Databases and SQL and my background in ODBs is
> clouding my way to solving what seems to be a simple problem. I am sure
> many of you have met it many times.
>
> OK, I have a table as follows:
>
>
> ID Machine   Date             Withdrawals
> 1  1              01/01/2003    1101
> 2  2              01/01/2003    2101
> 3  3              01/01/2003    3101
>
> 4  1              02/01/2003    1102
> 5  2              02/01/2003    2102
>
> 6  1              03/01/2003    1103
> 7  3              03/01/2003    3103
>
> 8  2              04/01/2003    2104
> 9  4              04/01/2003    4104
>
> And i would like to create a monthly withdrawals report as follows:
>
> Machine   Day1  Day2  Day3  Day4
> .............................................. Day31 1             11     
> 12      13       0                                                      0 2
>             21      22      0        24                                    
>                 0 3             31      0        33       0                
>                                      0 4              0        0       0   
>      0                                                     0
>
> Can you please help?
>
> Many thanks in advance.
>
> Ali
> <help yourself by helping others>
> www.geocities.com/aliadams




^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2003-07-13 07:46 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-04-04 11:10 help yourself by helping others Ali Adams <aliadams@doit4u.com>
2003-07-10 08:24 ` Richard Huxton <dev@archonet.com>
2003-07-10 12:25 ` Yasir Malik <ymalik@cs.stevens-tech.edu>
2003-07-10 13:47 ` Rod Taylor <rbt@rbt.ca>
2003-07-10 15:20 ` scott.marlowe <scott.marlowe@ihs.com>
2003-07-10 19:14 ` Jonathan Gardner <jgardner@jonathangardner.net>
2003-07-10 20:00 ` Michael A Nachbaur <mike@nachbaur.com>
2003-07-13 07:46 ` Rajesh Kumar Mallah <mallah@trade-india.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox