PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > needed on query syntax - easy question!
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
needed on query syntax - easy question!

Réponse
 
LinkBack Outils de la discussion
Vieux 03/10/2007, 14h33   #1
Mike Harrison
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut needed on query syntax - easy question!

Hi, this is quite a basic question I think, but I'm still learning

If I have a table like this:

Date Amount
2007-10-01 10.39
2007-10-03 2.15
2007-10-03 4.92
2007-10-04 6.00
2007-10-06 1.23
2007-10-06 13.67

What query would I have to run to get this output?:

Date Total amount
2007-10-01 10.39
2007-10-02 0
2007-10-03 7.07
2007-10-04 6.00
2007-10-05 0
2007-10-06 14.9
2007-10-07 0

I'm having problems getting a line in the output for a date that
doesn't exist in the table. Is it possible to do this?

Thanks.

  Réponse avec citation
Vieux 03/10/2007, 14h46   #2
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed on query syntax - easy question!

On 3 Oct, 13:33, Mike Harrison <mjh...@yahoo.co.uk> wrote:
> Hi, this is quite a basic question I think, but I'm still learning
>
> If I have a table like this:
>
> Date Amount
> 2007-10-01 10.39
> 2007-10-03 2.15
> 2007-10-03 4.92
> 2007-10-04 6.00
> 2007-10-06 1.23
> 2007-10-06 13.67
>
> What query would I have to run to get this output?:
>
> Date Total amount
> 2007-10-01 10.39
> 2007-10-02 0
> 2007-10-03 7.07
> 2007-10-04 6.00
> 2007-10-05 0
> 2007-10-06 14.9
> 2007-10-07 0
>
> I'm having problems getting a line in the output for a date that
> doesn't exist in the table. Is it possible to do this?
>
> Thanks.


The usual way is to join with a calendar table containing all dates.

  Réponse avec citation
Vieux 03/10/2007, 16h18   #3
frizzle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed on query syntax - easy question!

On Oct 3, 2:46 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 3 Oct, 13:33, Mike Harrison <mjh...@yahoo.co.uk> wrote:
>
>
>
>
>
>
>
> > Hi, this is quite a basic question I think, but I'm still learning

>
> > If I have a table like this:

>
> > Date Amount
> > 2007-10-01 10.39
> > 2007-10-03 2.15
> > 2007-10-03 4.92
> > 2007-10-04 6.00
> > 2007-10-06 1.23
> > 2007-10-06 13.67

>
> > What query would I have to run to get this output?:

>
> > Date Total amount
> > 2007-10-01 10.39
> > 2007-10-02 0
> > 2007-10-03 7.07
> > 2007-10-04 6.00
> > 2007-10-05 0
> > 2007-10-06 14.9
> > 2007-10-07 0

>
> > I'm having problems getting a line in the output for a date that
> > doesn't exist in the table. Is it possible to do this?

>
> > Thanks.

>
> The usual way is to join with a calendar table containing all dates.


Or -if possible- leave it up to e.g. PHP. E.g. have an array compared
to the output.

  Réponse avec citation
Vieux 03/10/2007, 18h05   #4
Peter H. Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed on query syntax - easy question!

On Wed, 03 Oct 2007 05:33:45 -0700, Mike Harrison wrote:
> Hi, this is quite a basic question I think, but I'm still learning
>
> If I have a table like this:
>
> Date Amount
> 2007-10-01 10.39
> 2007-10-03 2.15
> 2007-10-03 4.92
> 2007-10-04 6.00
> 2007-10-06 1.23
> 2007-10-06 13.67
>
> What query would I have to run to get this output?:
>
> Date Total amount
> 2007-10-01 10.39
> 2007-10-02 0
> 2007-10-03 7.07
> 2007-10-04 6.00
> 2007-10-05 0
> 2007-10-06 14.9
> 2007-10-07 0
>
> I'm having problems getting a line in the output for a date that
> doesn't exist in the table. Is it possible to do this?


Sure. Make a table with all the dates you want to list in it. Join
your real table to it with an outer join.

--
18. I will not have a son. Although his laughably under-planned attempt to
usurp power would easily fail, it would provide a fatal distraction at a
crucial point in time.
--Peter Anspach's list of things to do as an Evil Overlord
  Réponse avec citation
Vieux 03/10/2007, 19h37   #5
Mike Harrison
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed on query syntax - easy question!

On Oct 3, 5:05 pm, "Peter H. Coffin" <hell...@ninehells.com> wrote:

> Sure. Make a table with all the dates you want to list in it. Join
> your real table to it with an outer join.
>


Thanks for the responses. So what's the easiest way to create a table
of dates? Is there a way to automate it or have I to enter it all
manually?

For example, I would like a table of dates from 2007-09-01 to
2008-12-31.

I guess one way would be to create the column of dates in Excel or
another spreadsheet, export to a file and then LOAD DATA INFILE. But
is there an easier way to do it?

  Réponse avec citation
Vieux 09/10/2007, 15h12   #6
Peter H. Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed on query syntax - easy question!

On Wed, 03 Oct 2007 10:37:38 -0700, Mike Harrison wrote:
> On Oct 3, 5:05 pm, "Peter H. Coffin" <hell...@ninehells.com> wrote:
>
>> Sure. Make a table with all the dates you want to list in it. Join
>> your real table to it with an outer join.
>>

>
> Thanks for the responses. So what's the easiest way to create a table
> of dates? Is there a way to automate it or have I to enter it all
> manually?
>
> For example, I would like a table of dates from 2007-09-01 to
> 2008-12-31.
>
> I guess one way would be to create the column of dates in Excel or
> another spreadsheet, export to a file and then LOAD DATA INFILE. But
> is there an easier way to do it?


Any number of application programs would do it simply, with loop over an
INSERT query. It's just not something that SQL is well-suited for. It's
something you only have to do ONCE, after all, if you make a big enough
table. For anything else, you just do something like

INSERT INTO my_dates (my_date)
SELECT date_col
FROM master_dates
WHERE date_col >= DATE('2007-09-01') AND
date_col <= DATE('2008-12-31');

Your master date table can run for 100 years with less than 36,000 rows,
after all.

--
For every subject you can think of there are at least 3 web sites.
The owners of these web sites know each other and at least one of
them hates at least one of the others.
-- mnlooney's view of Skif's Internet Theorem
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h43.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11170 seconds with 14 queries