Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 27th, 2007, 08:59 PM
Registered User
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loop Stored Procedure

Hi Everyone!

I'm trying to create a stored procedure to create a table with a LOT of data. I have been playing around with this for quite a while and I still haven't gotten it to work!

The select query is copied and pasted below. I need to replace [CM1] with 53 other questions.

Thank you so much in advance. I hope someone knows how to do this! :)


SELECT 'CM1'
      ,[Segment]
      ,[SubSegment]
      ,[PrimaryMarketAudience]
      ,[SalesDistrict]
      ,[GRS_DATE]
      ,SUM(CASE WHEN [CM1] IN (1,2,3,4)
                THEN 1
            ELSE 0
        END) AS NUM_DSAT
      ,SUM(CASE WHEN [CM1] IN (8,9)
                THEN 1
                ELSE 0
                END) AS NUM_VSAT
      ,COUNT(*) AS Total
      ,SUM(CASE WHEN [CM1] = 1 and [Segment] = 'Major'
        THEN 0.02
        WHEN [CM1] = 1 and[Segment] ='Education'
        THEN 0.02
        WHEN [CM1] = 1 and [Segment]= 'CAS'
        THEN 0.02
        WHEN [CM1] = 1 and [Segment] = 'Small Business'
        THEN 2
        WHEN [CM1] = 1 and [Segment]= 'Mid-Market'
        THEN 2
        WHEN [CM1] = 1 and [Segment]= 'IT Pro'
        THEN 2.4
            WHEN [CM1] = 2 and [Segment] = 'Major'
        THEN 0.02
        WHEN [CM1] = 2 and [Segment]= 'Education'
        THEN 0.02
        WHEN [CM1] = 2 and [Segment]= 'CAS'
        THEN 0.02
        WHEN [CM1] = 2 and [Segment] = 'Small Business'
        THEN 2
        WHEN [CM1] = 2 and [Segment]= 'Mid-Market'
        THEN 2
        WHEN [CM1] = 2 and [Segment]= 'IT Pro'
        THEN 2.4
        WHEN [CM1] = 3 and [Segment] = 'Major'
        THEN 0.02
        WHEN [CM1] = 3 and [Segment]= 'Education'
            THEN 0.02
        WHEN [CM1] = 3 and [Segment]= 'CAS'
        THEN 0.02
        WHEN [CM1] = 3 and [Segment] = 'Small Business'
        THEN 2
        WHEN [CM1] = 3 and [Segment]= 'Mid-Market'
        THEN 2
        WHEN [CM1] = 3 and [Segment]= 'IT Pro'
        THEN 2.4
        WHEN [CM1] = 4 and [Segment] = 'Major'
        THEN 0.02
        WHEN [CM1] = 4 and [Segment]= 'Education'
        THEN 0.02
        WHEN [CM1] = 4 and [Segment]= 'CAS'
        THEN 0.02
        WHEN [CM1] = 4 and [Segment] = 'Small Business'
        THEN 2
        WHEN [CM1] = 4 and [Segment]= 'Mid-Market'
        THEN 2
        WHEN [CM1] = 4 and [Segment]= 'IT Pro'
        THEN 2.4
        ELSE 0
        END) as RND_DSAT
      ,SUM(CASE WHEN [CM1] = 8 and [Segment] = 'Major'
        THEN 0.02
        WHEN [CM1] = 8 and [Segment]= 'Education'
        THEN 0.02
        WHEN [CM1] = 8 and [Segment]= 'CAS'
        THEN 0.02
        WHEN [CM1] = 8 and [Segment] = 'Small Business'
        THEN 2
        WHEN [CM1] = 8 and [Segment]= 'Mid-Market'
        THEN 2
        WHEN [CM1] = 8 and [Segment]= 'IT Pro'
        THEN 2.4
            WHEN [CM1] = 9 and [Segment] = 'Major'
        THEN 0.02
        WHEN [CM1] = 9 and [Segment]= 'Education'
        THEN 0.02
        WHEN [CM1] = 9 and [Segment]= 'CAS'
        THEN 0.02
        WHEN [CM1] = 9 and [Segment] = 'Small Business'
        THEN 2
        WHEN [CM1] = 9 and [Segment]= 'Mid-Market'
        THEN 2
        WHEN [CM1] = 9 and [Segment]= 'IT Pro'
        THEN 2.4
        ELSE 0
        END) as RND_VSAT
      ,SUM(CASE WHEN [SEGMENT] = ‘Major'
        THEN 0.02
        WHEN [SEGMENT] = 'Education'
        THEN 0.02
        WHEN [SEGMENT] = 'CAS'
        THEN 0.02
        WHEN [SEGMENT] = 'Small Business'
        THEN 2
        WHEN [SEGMENT] = 'Mid-Market'
        THEN 2
        WHEN [SEGMENT] = 'IT Pro'
        THEN 2.4
        ELSE 0
        END) as RND_Total
FROM [2007H2GRS].[dbo].[H1_H2Data]
WHERE GRS_Date = 200702
GROUP BY [Segment]
      ,[SubSegment]
      ,[PrimaryMarketAudience]
      ,[SalesDistrict]
    ,[GRS_DATE]
      ,[CM1]



 
Old November 29th, 2007, 10:48 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Could you not just create it as a stored procedure?

CREATE PROCEDURE CORRINEO_QUERY @CM1 VARCHAR(255) AS
... then your query, but replace CM1 with @CM1

How about storing you 54 questions in a table and then joining that table into your query?

Regards,

Sean Anderson





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stored Procedure loop ? hman SQL Language 5 December 21st, 2006 05:51 AM
stored procedure keyvanjan Classic ASP Basics 6 August 1st, 2006 07:42 AM
stored procedure lokey VB How-To 7 June 30th, 2005 12:37 AM
stored procedure kvanchi ADO.NET 1 December 9th, 2004 07:27 AM
Stored Procedure bmains SQL Server ASP 2 October 8th, 2004 03:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.