Skip site navigation (1) Skip section navigation (2)

Re: need help in building a query

From: Devil™ Dhuvader <gibsosmat(at)gmail(dot)com>
To: Harold A(dot) Giménez Ch(dot) <harold(dot)gimenez(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: need help in building a query
Date: 2008-11-07 17:20:06
Message-ID: aaa67ada0811070920i26781a6dg630d98ee142fd522@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-sql
its like sum up entries of each user in order table backwards (i.e from last
entry to the first) and find the entry that has sum > $500.
If there is some user who didnt even make 500 till now in my shop return the
first date of transaction/order.

ex:
Orders(order_id, user_id, amount_paid, create_timestamp)
values:
(1, 1, 100, 1)
(2, 1, 300, 2)
(3, 2, 100, 2)
(4, 2, 100, 3)
(5, 1, 100, 4)
(6, 1, 200, 5)
(7, 2, 150, 5)

for user 1: the order_id = 2
for user 2: the order_id = 3 (coz he couldnt make 500)

On Thu, Nov 6, 2008 at 10:40 PM, Harold A. Giménez Ch. <
harold(dot)gimenez(at)gmail(dot)com> wrote:

> I personally would help if I understood what you need. I'm sure others feel
> the same way. Provide DDL, sample data, and expected result of the query.
> Maybe you'll have better luck...
>
>
> On Thu, Nov 6, 2008 at 11:15 AM, Devil™ Dhuvader <gibsosmat(at)gmail(dot)com>wrote:
>
>> none can help me?
>>
>> On Tue, Nov 4, 2008 at 9:08 PM, Devil™ Dhuvader <gibsosmat(at)gmail(dot)com>wrote:
>>
>>> hi,
>>> I need some help in creating a sql.
>>> the problem is as below.
>>>
>>> assume that:
>>> I am a store keeper
>>> and I have the list of customer(user_id) transactions in my order table.
>>> schema: Orders(order_id, user_id, amount_paid, create_timestamp)
>>>
>>> I want to give discount of 10% for the customer who made orders of worth
>>> (sum) at least $500 in the least time from now (i.e last few days, but NOT
>>> ALL TIME LEAST TIME TO BUY $500 WORTH GOODS).
>>> and 9% discount for the customer who made $500 in second least time from
>>> now.
>>> and so on
>>>
>>> there is no time constraint.
>>> it customer could take any amount of time to make $500.
>>> the customer might not even made $500 bill till now in that case I should
>>> get the first date.
>>>
>>> the result can take upto the first entry time of the table itself.
>>>
>>> in short its like sum up entries of each user in order table backwards
>>> (i.e from last entry to the first) and find the entry that has sum > $500.
>>> If there is some user who didnt even make 500 till now in my shop return
>>> the first date of transaction/order.
>>>
>>> can anyone help me on this?
>>
>>
>>
>

In response to

Responses

pgsql-sql by date

Next:From: Andreas Joseph KroghDate: 2008-11-07 19:46:10
Subject: Re: Res: Finding all tables that have foreign keys referencing a table
Previous:From: Scott MarloweDate: 2008-11-07 16:49:44
Subject: Re: sum timestamp result in hours

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group