Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Stored View
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
simon  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 12:50
Newsgroups: microsoft.public.sqlserver.programming
From: simon <simon.zu...@studio-moderna.com>
Date: Thu, 5 Nov 2009 13:50:05 +0100
Local: Thurs 5 Nov 2009 12:50
Subject: Stored View
Hi,

how can I use SELECT CASE statement in stored view?

This example throws me an error:

Cannot create the clustered index ... because the select list of the
view contains an expression on result of aggregate function or grouping
column. Consider removing expression on result of aggregate function or
grouping column from select list.

CREATE VIEW dbo.v_Sells WITH SCHEMABINDING
AS
SELECT productID, cntID, SUM(stock) AS stock,
sell=CASE WHEN 4*SUM(sellWeek)<SUM(sellMonth) THEN SUM(sellMonth) ELSE
SUM(sellWeek) END, COUNT_BIG(*) AS counter
FROM dbo.stockTable
GROUP BY productID, cntID


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Uri Dimant  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 13:17
Newsgroups: microsoft.public.sqlserver.programming
From: "Uri Dimant" <u...@iscar.co.il>
Date: Thu, 5 Nov 2009 15:17:05 +0200
Local: Thurs 5 Nov 2009 13:17
Subject: Re: Stored View
simon

See if this helps you

 CREATE VIEW dbo.v_Sells WITH SCHEMABINDING
 AS
 SELECT productID, cntID, SUM(stock) AS stock,
 sell=SUM(CASE WHEN 4*sellWeek<sellMonth THEN sellMonth ELSE
 sellWeek END, COUNT_BIG(*) AS counter
 FROM dbo.stockTable
 GROUP BY productID, cntID

CREATE unique  clustered INDEX here........

"simon" <simon.zu...@studio-moderna.com> wrote in message

news:MPG.255ce86399c944a6989680@news.microsoft.com...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Isaacs  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 13:24
Newsgroups: microsoft.public.sqlserver.programming
From: Eric Isaacs <eisa...@gmail.com>
Date: Thu, 5 Nov 2009 05:24:26 -0800 (PST)
Local: Thurs 5 Nov 2009 13:24
Subject: Re: Stored View
I like Uri's answer better, but this might also work...

SELECT
    Grouped_StockTable.productID,
    Grouped_StockTable.cntID,
    Grouped_StockTable.SumOfStock AS stock,
    CASE WHEN 4 * Grouped_StockTable.SumOfSellWeek <
Grouped_StockTable.SumOfSellMonth THEN
Grouped_StockTable.SUMOfsellMonth
         ELSE Grouped_StockTable.SumOfSellWeek
    END AS sell,
    GroupCounter AS [counter]
FROM
    (
     SELECT
        productID,
        cntID,
        SUM(stock) AS SumOfStock,
        SUM(sellWeek) SumOfSellWeek,
        SUM(sellMonth) SumOfSellMonth,
        COUNT_BIG(*) AS GroupCounter
     FROM
        dbo.stockTable
     GROUP BY
        productID,
        cntID) AS Grouped_StockTable

-Eric Isaacs


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Uri Dimant  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 13:28
Newsgroups: microsoft.public.sqlserver.programming
From: "Uri Dimant" <u...@iscar.co.il>
Date: Thu, 5 Nov 2009 15:28:13 +0200
Local: Thurs 5 Nov 2009 13:28
Subject: Re: Stored View
Sorry, forget to add (...

CREATE VIEW dbo.v_Sells WITH SCHEMABINDING
 AS
 SELECT productID, cntID, SUM(stock) AS stock,
 sell=SUM(CASE WHEN 4*sellWeek<sellMonth THEN sellMonth ELSE
 sellWeek END) AS somevalue, COUNT_BIG(*) AS counter
 FROM dbo.stockTable
 GROUP BY productID, cntID

"Uri Dimant" <u...@iscar.co.il> wrote in message

news:%23FFJXohXKHA.3428@TK2MSFTNGP06.phx.gbl...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
simon  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 13:55
Newsgroups: microsoft.public.sqlserver.programming
From: simon <simon.zu...@studio-moderna.com>
Date: Thu, 5 Nov 2009 14:55:00 +0100
Subject: Re: Stored View
Hi Uri,

thank you for your answer but your view return different results.

I need to compare if 4*sum(sellWeek) is lower than SUM(sellMonth) and
not sum of single row compare SUM(sellWeek<sellMonth).

You see the difference?

Thank you,
Simon


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
simon  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 13:57
Newsgroups: microsoft.public.sqlserver.programming
From: simon <simon.zu...@studio-moderna.com>
Date: Thu, 5 Nov 2009 14:57:16 +0100
Local: Thurs 5 Nov 2009 13:57
Subject: Re: Stored View
In article <e2692693-5674-4c5f-958c-015d691e9757
@t11g2000prh.googlegroups.com>, eisa...@gmail.com says...

Hi Eric

first thank you for your answer.

Your view works as normal view but in indexed view - isn't there
restriction that you can't have derived table in indexed view?

Thanks,
Simon


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Uri Dimant  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 14:03
Newsgroups: microsoft.public.sqlserver.programming
From: "Uri Dimant" <u...@iscar.co.il>
Date: Thu, 5 Nov 2009 16:03:23 +0200
Local: Thurs 5 Nov 2009 14:03
Subject: Re: Stored View
simon
Well, post sample data + an expected result and I would have seen the
difference:-)))))

Perhaps Eric's solution solves the problem

"simon" <simon.zu...@studio-moderna.com> wrote in message

news:MPG.255cf7a146e2359f989681@news.microsoft.com...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Ballantyne  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 14:09
Newsgroups: microsoft.public.sqlserver.programming
From: Dave Ballantyne <symlink@no5p323mmer>
Date: Thu, 05 Nov 2009 14:09:48 +0000
Local: Thurs 5 Nov 2009 14:09
Subject: Re: Stored View
There is a full list of the requirements for a view to be indexable here.

http://msdn.microsoft.com/en-us/library/aa933148(SQL.80).aspx

Dave Ballantyne
http://sqlblogcasts.com/blogs/sqlandthelike/


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Cooper  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 18:22
Newsgroups: microsoft.public.sqlserver.programming
From: "Tom Cooper" <tomcoo...@comcast.net>
Date: Thu, 5 Nov 2009 13:22:04 -0500
Local: Thurs 5 Nov 2009 18:22
Subject: Re: Stored View
One way is to create a indexed view that does not have the case statement,
something like

CREATE VIEW dbo.v_SellsIndexed WITH SCHEMABINDING
AS
SELECT productID, cntID, SUM(stock) AS stock,
SUM(sellWeek) As SellWeekSum, SUM(sellMonth) As SellMonthSum,
COUNT_BIG(*) AS counter
FROM dbo.stockTable
GROUP BY productID, cntID;
go
Create Unique Clustered Index v_SellsIdx On dbo.v_SellsIndexed (productID,
cntID);
go

Then create your view as a nonidexed view on top of your indexed view,
something like

CREATE VIEW dbo.v_Sells WITH SCHEMABINDING
AS
SELECT productID, cntID, stock,
sell=CASE WHEN 4*SellWeekSum<SellMonthSum THEN SellMonthSum ELSE
SellWeekSum END, counter
FROM dbo.v_SellsIndexed (NoExpand);
go

(When creating this view, if you don't have the Enterprise edition of SQL
Server, you will need the NoExpand hint).

Tom

"simon" <simon.zu...@studio-moderna.com> wrote in message

news:MPG.255ce86399c944a6989680@news.microsoft.com...


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google