Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

View sentence error Expand / Collapse
Author
Message
Posted Wednesday, June 20, 2007 2:49 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 28, 2013 6:20 AM
Points: 9, Visits: 8

Please help with the following, I am creating a view in SQL Server 2000, the sentence is I have is this one:

SELECT     TOP 100 PERCENT ITEMNMBR, SUM(TRXQTY) AS Expr1, CASE WHEN datediff(month, docdate, getdate()) = 0 THEN SUM(TRXQTY)
                      END AS period1
FROM         dbo.IV30300
GROUP BY ITEMNMBR, DOCDATE
ORDER BY itemnmbr

This runs fine, but I don't get the results I expect since I just want to group by ITEMNMBR and not DOCDATE, how can I do this, if I remove DOCDATE from the groupby list I get and error that says something like "DOCDATE not valid from the selection list since its not on an add function or the GROUPBY list"

Sorry had to translate from spanish, hope it's clear enough.

So, how can I just have on the groupby the column ITEMNMBR? Something like:

SELECT     TOP 100 PERCENT ITEMNMBR, SUM(TRXQTY) AS Expr1, CASE WHEN datediff(month, docdate, getdate()) = 0 THEN SUM(TRXQTY)
                      END AS period1
FROM         dbo.IV30300
GROUP BY ITEMNMBR
ORDER BY itemnmbr

Thanks in advance

Post #375426
Posted Wednesday, June 20, 2007 2:56 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, November 27, 2014 4:23 AM
Points: 20,584, Visits: 9,624

You should be able to get this to work...

SELECT     TOP 100 PERCENT ITEMNMBR, SUM(TRXQTY) AS Expr1, SUM(CASE WHEN datediff(month, docdate, getdate()) = 0 THEN TRXQTY ELSE 0 END) AS period1
FROM         dbo.IV30300
GROUP BY ITEMNMBR
ORDER BY itemnmbr

Post #375429
Posted Wednesday, June 20, 2007 2:59 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 28, 2013 6:20 AM
Points: 9, Visits: 8
Thanks ninja, it did work
Post #375431
Posted Wednesday, June 20, 2007 11:56 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 35,769, Visits: 32,437
Alvaro... really, really bad idea to sort in a view... really bad...

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #375471
Posted Thursday, June 21, 2007 7:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 28, 2013 6:20 AM
Points: 9, Visits: 8

Jeff, Actually the sort sentence was just to "debug" the view.

Thanks for your tip, but could you explain why is it such a bad idea?

Post #375570
Posted Thursday, June 21, 2007 8:03 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, November 27, 2014 4:23 AM
Points: 20,584, Visits: 9,624

I'm sure Jeff will come up with a more complete answer, but one example is that you'll create that view, then a few weeks later, someone else will use this view to create is own view.  Then the process repeats itself a few more times.  Now you have a single view with 5-6 sorts, all of them useless.  This is also another way to tell that it's not a good idea to build views on a mountain of views.  I have no figures to give you, but the less the better.

Post #375592
Posted Thursday, June 21, 2007 8:12 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 28, 2013 6:20 AM
Points: 9, Visits: 8
Oh I see, thanks for the reply Ninja, I just started working with views, so i'm new to this
Post #375602
Posted Thursday, June 21, 2007 8:18 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, November 27, 2014 4:23 AM
Points: 20,584, Visits: 9,624
That's alright, we're all here to learn .
Post #375610
Posted Thursday, June 21, 2007 8:52 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 35,769, Visits: 32,437

Even if you don't build views on the sorted view, consider this... what if you want the data from the view to be in a different order?  The view will still sort it one way and the query will then have to resort the data... one sort is bad enough for performance but two is worse.

My other recommendation is (almost) never write a view of a view... sometimes the "inner" view must fully materialize before the outer view can resolve especially when aggragate functions are involved... huge performance hit when that happens.  I'll likely get some guff about this but if a view doesn't return from a SELECT TOP 10 * almost instantaneously, you may end up having some very significant performance problems and memory usage problems when the view scales up in rows.

Basically, the only thing I really use views for is a synonyms to tables in another database so I don't have to grant access to the other database and I can leave out "sensitive" columns.  I may also create "INDEXED VIEWS" for heavy aggragation because the run pretty darned fast... they're a pain to make but they can be worth it.

 



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #375626
Posted Thursday, June 21, 2007 10:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 28, 2013 6:20 AM
Points: 9, Visits: 8
Thanks Jeff for that reply, it's really helpful
Post #375665
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse