SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


View sentence error


View sentence error

Author
Message
alvaro-454996
alvaro-454996
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
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


Ninja's_RGR'us
Ninja's_RGR'us
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28695 Visits: 9671

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


alvaro-454996
alvaro-454996
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 8
Thanks ninja, it did work
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84503 Visits: 41063
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
alvaro-454996
alvaro-454996
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
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?


Ninja's_RGR'us
Ninja's_RGR'us
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28695 Visits: 9671

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.


alvaro-454996
alvaro-454996
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 8
Oh I see, thanks for the reply Ninja, I just started working with views, so i'm new to this
Ninja's_RGR'us
Ninja's_RGR'us
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28695 Visits: 9671
That's alright, we're all here to learn .
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84503 Visits: 41063

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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
alvaro-454996
alvaro-454996
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 8
Thanks Jeff for that reply, it's really helpful
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search