PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > Select with many to one relationship
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Select with many to one relationship

Réponse
 
LinkBack Outils de la discussion
Vieux 12/11/2008, 20h37   #1
Artie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Select with many to one relationship

Hi all,
I have 3 tables:
Order Header (oh) order header info (inv_no customer, address, date, etc)
Order Line (ol) order line detail (items ordered, qty, price, etc)
Credits (c) any available credits against the order

Credits link to order header by inv_no.
How can I avoid returning the credits multiple times (once per line item)
Not all orders have a credit. I need to return all orders and their line
items and show if a credits exists

If an order has 5 items, this will show the credit 5 times
select oh.inv_no, ol.item_no, ol.qty, c.CreditAmt

from OrderHeader oh

inner join OrderLine ol on oh.inv_no = ol.inv_no

left outer join Credits c on oh.inv_no = c.inv_no



Any ideas and pointers are much appreciated

Thanks


  Réponse avec citation
Vieux 12/11/2008, 21h17   #2
Ed Murphy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Select with many to one relationship

Artie wrote:

> Hi all,
> I have 3 tables:
> Order Header (oh) order header info (inv_no customer, address, date, etc)
> Order Line (ol) order line detail (items ordered, qty, price, etc)
> Credits (c) any available credits against the order
>
> Credits link to order header by inv_no.
> How can I avoid returning the credits multiple times (once per line item)
> Not all orders have a credit. I need to return all orders and their line
> items and show if a credits exists
>
> If an order has 5 items, this will show the credit 5 times
> select oh.inv_no, ol.item_no, ol.qty, c.CreditAmt
>
> from OrderHeader oh
>
> inner join OrderLine ol on oh.inv_no = ol.inv_no
>
> left outer join Credits c on oh.inv_no = c.inv_no


create view CreditTotals as
select inv_no, sum(CreditAmt) TotalCreditAmt
from Credits
group by inv_no

select oh.inv_no, ol.item_no, ol.qty, tc.TotalCreditAmt
from OrderHeader oh
inner join OrderLine ol on oh.inv_no = ol.inv_no
left outer join CreditTotals tc on oh.inv_no = tc.inv_no
  Réponse avec citation
Vieux 12/11/2008, 21h35   #3
Artie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Select with many to one relationship

Ed, thanks for the . Sorry, I was not clear enough.
The credits table only has 1 row per inv_no so your view would produce the
same results I already have.

Example:
Credits.Inv_no = 54321
Credits.CreditAmt = 100.00

Order 54321 has 5 line items
my original script would show:
inv_no item_no CreditAmt
54321 abc123 100.00
54321 abc456 100.00
54321 abc789 100.00
54321 cde123 100.00
54321 cde456 100.00

If I total this, I get a CreditAmt of 500.00




"Ed Murphy" <emurphy42@socal.rr.com> wrote in message
news:gffdna$2nd$1@news.eternal-september.org...
> Artie wrote:
>
>> Hi all,
>> I have 3 tables:
>> Order Header (oh) order header info (inv_no customer, address, date,
>> etc)
>> Order Line (ol) order line detail (items ordered, qty, price, etc)
>> Credits (c) any available credits against the order
>>
>> Credits link to order header by inv_no.
>> How can I avoid returning the credits multiple times (once per line item)
>> Not all orders have a credit. I need to return all orders and their line
>> items and show if a credits exists
>>
>> If an order has 5 items, this will show the credit 5 times
>> select oh.inv_no, ol.item_no, ol.qty, c.CreditAmt
>>
>> from OrderHeader oh
>>
>> inner join OrderLine ol on oh.inv_no = ol.inv_no
>>
>> left outer join Credits c on oh.inv_no = c.inv_no

>
> create view CreditTotals as
> select inv_no, sum(CreditAmt) TotalCreditAmt
> from Credits
> group by inv_no
>
> select oh.inv_no, ol.item_no, ol.qty, tc.TotalCreditAmt
> from OrderHeader oh
> inner join OrderLine ol on oh.inv_no = ol.inv_no
> left outer join CreditTotals tc on oh.inv_no = tc.inv_no



  Réponse avec citation
Vieux 12/11/2008, 22h52   #4
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Select with many to one relationship

The main question here is how do you want to see the credit?

Here are two queries, the first one will distribute the credit to all
line items, and the second will show it only on the last item (assuming
item_no is something you can order by, also, you can easily change that
to show on the first item, just remove DESC in the ORDER BY clause of
OVER). Both methods require SQL Server 2005/2008.

SELECT oh.inv_no,
ol.item_no,
ol.qty,
c.CreditAmt /
COUNT(*) OVER(PARTITION BY oh.inv_no) AS credit_amt
FROM OrderHeader AS oh
INNER JOIN OrderLine AS ol
ON oh.inv_no = ol.inv_no
LEFT OUTER JOIN Credits AS c
ON oh.inv_no = c.inv_no;

SELECT oh.inv_no,
ol.item_no,
ol.qty,
CASE
WHEN
ROW_NUMBER() OVER(PARTITION BY oh.inv_no
ORDER BY ol.item_no DESC) = 1
THEN c.CreditAmt
ELSE 0
END credit_amt
FROM OrderHeader AS oh
INNER JOIN OrderLine AS ol
ON oh.inv_no = ol.inv_no
LEFT OUTER JOIN Credits AS c
ON oh.inv_no = c.inv_no;

--
Plamen Ratchev
http://www.SQLStudio.com
  Réponse avec citation
Vieux 13/11/2008, 16h50   #5
--CELKO--
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Select with many to one relationship

"A problem well stated is a problem half solved." -- Charles F.
Kettering

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules. Temporal data should use ISO-8601
formats. Code should be in Standard SQL as much as possible and not
local dialect.

Sample data is also a good idea, along with clear specifications. It
is very hard to debug code when you do not let us see it. If you want
to learn how to ask a question on a Newsgroup, look at:
http://www.catb.org/~esr/faqs/smart-questions.html

  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 02h26.


Édité par : vBulletin®
Copyright ©2000 - 2009, 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,11154 seconds with 13 queries