Re: Select question

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 10/21/04


Date: Thu, 21 Oct 2004 22:11:15 +0200

On Thu, 21 Oct 2004 16:01:47 +0200, simon wrote:

(snip)
>I hope you understand now what I want.

Hi Simon,

I'm not sure. Posting CREATE TABLEs, INSERTs for sample data and expected
output is really a much better way to explain what you want than any
narrative and made up examples. Judging by your e-mail address and posting
host, English is not your native language. It's not mine either, so you
should expect misunderstandings if you don't use the one language that we
do have in common: SQL.

Anyway, if I understand your need correctly, you're doing something that
you might want to do in the presentation layer isntead. If that option is
not available, read on.

>If that won't be derived table, it's simple:
>
>SELECT T1.orderId,T1.productId,T1.value,
>quantity1=(SELECT sum(value) FROM table WHERE orderID=T1.orderID),
>quantity2=(SELECT sum(value) FROM table WHERE orderID=T1.orderID AND
>T1.crossell=0)
>FROM table as T1
>
>but if table is derived you can't create the same query

Yes, you can. But you'd have to repeat the subquery that defines the
derived table several times. Ugly, I know. Plus, I have seen evidence that
seems to indicate that SQL Server's optimizer doesn't take advantage of
the fact that the three subqueries are exactly identical.

An alternative would be to define a view that holds the definition of the
derived table. You can then reference the view as often as you wish in the
query. That would make the query as clean as it can be, but you might
still find SQL Server's optimizer producing odd-looking plans.

Other alternatives would involve temp tables, or derived tables using
GROUP BY, GROUPING and nasty CASE expressions. They'll defefinitely be
lots uglier than just repeating the derived table, so I wouldn't even
think about going there if I were you.

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages

  • Re: parameter in sql query
    ... Could you try expanding the autogenerated code and posting ... > Does the parameter substitution occur after or before the query is sent to ... > being transferred to the sql server without the parameter being replaced ... '@Start' is the name of the param and the error says there ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Memo fields and update query
    ... Do you have a table with that many memo fields, ... Consider posting the SQL of your query if you'd like folks in the newsgroup ...
    (microsoft.public.access.macros)
  • Re: Finding last occurence of a record
    ... > copied the sql code and tried to utilise it in my application however the ... > original posting to confirm the original sql or contact the person who ... You should, of course, make sure the relevant fields are indexed. ... you could first run a make-table query to create a table ...
    (microsoft.public.access.queries)
  • Re: Slow to commit a dimension
    ... I mean someone issuing MDX against Analysis Services. ... SQL BI Product Unit ... This posting is provided "AS IS" with no warranties, ... > Do you mean an AS query or a query from AS to the database? ...
    (microsoft.public.sqlserver.olap)
  • Re: DBMS and lisp, etc.
    ... Naively implemented with SQL, again for 10 ... (1 query for the initial orders, 1 query for each order for its ... soon as you upgrade to the SQL database. ... (eq (order-customer orderA) ...
    (comp.lang.lisp)