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 > Retrieve ONLY first/max
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Retrieve ONLY first/max

Réponse
 
LinkBack Outils de la discussion
Vieux 25/09/2007, 08h17   #9
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Retrieve ONLY first/max

On Sep 23, 4:30 pm, --CELKO-- <jcelko...@earthlink.net> wrote:
> >> You'd think they'd have a "standard" (and simple) method for doing this eh? <<

>
> It is a little hard to guess what your tables and columns look like
> from your narrative. There is even a magical, universal "id" attached
> to nothing in particular! I guess you meant "product_id" and do not
> have an industry standard identifier to use, like UPC. I guess type
> is the product type (category?) code, and that it is in the Products
> table, not the Orders. But it could be the order type, customer blood
> type or anything.
>
> One Standard SQL answer would be:
>
> WITH ProductCategorySales(product_type, sold_tot)
> AS (SELECT P.product_type, SUM(O.sold_units)
> FROM Orders AS O, Products AS P
> WHERE O.product_id = P.product_id
> GROUP BY product_type)
>
> SELECT product_type
> FROM ProductCategorySales
> HAVING sold_tot = (SELECT MAX(sold_tot) FROM ProductCategorySales);
>
> or get fancy and use some stuff not in SQL Server yet:
>
> (SELECT P.product_type,
> SUM(O.sold_units)
> OVER (PARTITION BY P.product_type)
> FROM Orders AS O, Products AS P
> WHERE O.product_id = P.product_id)
>
> The reason that Standard SQL does not have LIMIT or something like it,
> is that SQL is a set-oriented database language, not a sequential file
> language. Such things would have to be part of a cursor's ORDER BY
> clause to fit into the language model.


I didn't think the tables and columns were important. It's a fake
data set.
I hadn't heard of this "WITH" clause. I'll look into it some more.
Thanks again.

BTW, I ended up using something like WHERE unitsum=MAX(...) which
seems horrible and hacky, but will suffice.

  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 12h39.


É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,08215 seconds with 9 queries