[SQL] 2 left join taking in too many records, 1 join and 1 left join too few was: join

2009-03-18 Thread Ivan Sergio Borgonovo
first, sorry for the subject.
I started editing the body and I forgot the subject looked nearly
meaningless.

I solved this using a view on 
catalog_promosimpleitem
and
catalog_promosimple
that just take out the record I'm interested into.
If no record, the left join on the view should make it work anyway
as expected.

I still don't like the overall solution, but at least it makes a
more reasonable starting point to clean some mess and having a list
of IsPromo around ready will come handy in other places.

On Tue, 17 Mar 2009 22:40:08 +0100
Ivan Sergio Borgonovo  wrote:

> I'm trying to get rid of the side effect of the 
> 
> left join catalog_promosimple
> 
> That means I'm expecting at least one row no matter if there is an
> entry in
> left join catalog_promosimpleitem
> but I don't want "duplicates" from
> catalog_promosimple
> 
> create table catalog_promosimple (
>  PromoSimpleID int primary key,
>  IsPromo boolean not null default true,
>  Discount numeric(19,4) not null default 0
> );
> create table catalog_promosimpleitem (
>  PromoSimpleID int references catalog_promosimple (PromoSimpleID),
>  ItemID int references catalog_items (ItemID)
> );
> 
> so eg. I've
> 
> 1,true
> 2,false
> 
> 1,1,10
> 1,1,20
> 2,1,0
> 2,1,5
> 
> when I'm looking for ItemID=1 the query should return:
> ItemID,Discount
> 1,20
> and skip the entries with IsPromo=false
> 
> If I've just
> 
> 2,false
> 
> 2,1,0
> 2,1,5
> 
> the query should return:
> ItemID,Discount
> 2,null
> 
> If there are no items at all in catalog_promosimpleitem (but there
> is a corresponding one in catalog_items eg. ItemID=5)
> the query should return:
> ItemID,Discount
> 5,null
> 
> 
> select i.ItemID as _ItemID, i.Code as _Code, i.ISBN as _ISBN,
>  i.CodeAlt as _CodeAlt, i.Name as _Name,
>  i.ListPrice as _ListPrice,
>  DiscountedPrice(
>  i.qty, i.StatusID, max(pi.Discount),
>  p.Percent, p.OnListPrice, p.Vendible, p.OnStock,i.ListPrice,
>  ip.Price )
>  as _DiscountedPrice,
>  i.valIva as _Tax,
>  i.StatusID as _StatusID, i.qty as _qty, b.Name as _Brands,
>  i.BrandID as _BrandID, i.Authors as _Authors,
>  b.Delivery as _Delivery,
>  extract(year from i.dataPub) as _YearPub,
>  s.FamID as _FamID, st.Name as _SName from catalog_items i
>  join catalog_categoryitem s on i.ItemID=s.ItemID
>  join catalog_item_status st on st.StatusID=i.StatusID
>  left join catalog_brands b on b.BrandID=i.BrandID
>  left join catalog_itemprice ip on ip.ItemID=i.ItemID
>  and ip.DiscountClass=10
>  left join catalog_promosimpleitem pi on pi.ItemID=i.ItemID
>  left join catalog_promosimple p on
>   pi.PromoSimpleID=p.PromoSimpleID and p.PromoStart   p.PromoEnd>=now() and p.IsPromo=true where i.ItemID=102020
>  group by i.ItemID, i.Code, i.ISBN, i.CodeAlt,
>   i.Name, i.ListPrice, i.qty, i.StatusID, p.Percent, p.OnListPrice,
>   p.Vendible, p.OnStock, ip.Price, i.valIva, b.Name, i.BrandID,
>   i.Authors, i.dataPub, s.FamID, st.Name, b.Delivery
> 


-- 
Ivan Sergio Borgonovo
http://www.webthatworks.it


-
Sent via pgsql-sql mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


Re: [SQL] How do I optimize this?

2009-03-18 Thread Richard Huxton
Wei Weng wrote:
> I made a mistake in the queries:
> 
> They should be
> 
> update T set t1 = b.aa FROM (select COUNT(1) as aa FROM D WHERE D.da =
> 1) as b where tkey = ;
> update T set t2 = b.aa FROM (select COUNT(1) as aa FROM D WHERE D.da =
> 2) as b where tkey = ;
> ...
> update T set t10 = b.aa FROM (select COUNT(1) as aa FROM D WHERE D.da =
> 10) as b where tkey = ;

You should be able to generate all the counts from one scan:

UPDATE T set t1 = b.a1, t2 = b.a2 ...
FROM (
  SELECT
sum(CASE WHEN D.da=1 THEN 1 ELSE 0 END) AS a1,
sum(CASE WHEN D.da=2 THEN 1 ELSE 0 END) AS a2,
...
  FROM D
) AS b
WHERE tkey = 

You might also want to look at the crosstab functions in the tablefunc
contrib module (see appendix F of the manuals).

-- 
  Richard Huxton
  Archonet Ltd

-
Sent via pgsql-sql mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


Re: [SQL] SQL to Check whether "AN HOUR PERIOD" is between start and end timestamps

2009-03-18 Thread Srikanth
That did the job. Thanks.
Am new to SQL, does not even know that there exists an Operator called OVERLAPS.

Thanks Richard 
../rssrik
--- On Tue, 17/3/09, Richard Huxton  wrote:

From: Richard Huxton 
Subject: Re: [SQL] SQL to Check whether "AN HOUR PERIOD" is between start and 
end timestamps
To: "Srikanth" 
Cc: [email protected]
Date: Tuesday, 17 March, 2009, 8:36 PM

Srikanth wrote:
> Dear all,
> 
> I have a table that records User Login Sessions with two timestamp fields.. 
> Basically Start of Session and End of a Session (start_ts and end_ts). Each 
> row in the table identifies a session which a customer has used.  
> 
> Data from the table (session):
> -
>  customer_id | log_session_id  |          start_ts          |           end_ts
> -+-++
>  1006100716  | 209571229340224 | 15/12/2008 16:53:52.665327 | 15/12/2008 
>16:58:52.665327
>  1006100789  | 112061228488202 | 05/12/2008 20:13:32.773065 | 09/12/2008 
>22:59:02.770218
>  1006100888  | 214221233045949 | 27/01/2009 14:15:16.289626 | 27/01/2009 
>14:58:59.989182
>  100608  | 205221236839534 | 12/03/2009 12:02:15.947509 | 12/03/2009 
>12:07:15.947509
>  1006100825  | 112331229068049 | 12/12/2008 13:17:37.229249 | 12/12/2008 
>13:56:58.394577
> 
> 
> The requirement is as follows,
> 
> I have to find out how many User Sessions that were present in any given "1 
> HOUR TIME PERIOD".  A single User Session can span across many days.

SELECT * FROM session WHERE (start_ts,end_ts) OVERLAPS (,
);

> I tried using wildcards in timestamp '07/01/2009 11:%:%" but in vain.
I tries googling / searching archives without any success either.

I'd have thought OVERLAPS would be mentioned in the date/time handling
sections of the manual.

-- 
  Richard Huxton
  Archonet Ltd



  Add more friends to your messenger and enjoy! Go to 
http://messenger.yahoo.com/invite/