Thread: help yourself by helping others

help yourself by helping others

From
"Ali Adams"
Date:
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>
 

Re: help yourself by helping others

From
Richard Huxton
Date:
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


Re: help yourself by helping others

From
"scott.marlowe"
Date:
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
simpleproblem. 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.



Re: help yourself by helping others

From
Yasir Malik
Date:
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
simpleproblem. 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
>


Re: help yourself by helping others

From
Jonathan Gardner
Date:
-----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-----


Re: help yourself by helping others

From
Michael A Nachbaur
Date:
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."

Re: help yourself by helping others

From
Rod Taylor
Date:
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.


Re: help yourself by helping others

From
Rajesh Kumar Mallah
Date:
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