Question about SQL

Hi expert!

Im using SQL Server 2005 and have question. I need to make effective SQL

I have follow table
/****** Object:  Table [dbo].[LPlans]    Script Date: 04/11/2009 09:55:06 ******/
CREATE TABLE [dbo].[LPlans](
      [LPlanID] [bigint] NOT NULL,
      [LTimeSlotID] [bigint] NOT NULL,
      [LCampaignFileID] [bigint] NOT NULL,
      [LCampaignID] [bigint] NOT NULL,
      [SortOrder] [int] NOT NULL,
      [Point] [int] NOT NULL CONSTRAINT [DF_LPlans_Point]  DEFAULT ((0)),
      [Type] [tinyint] NOT NULL CONSTRAINT [DF_LPlans_Type]  DEFAULT ((0)),
      [CB] [int] NULL,
      [CD] [smalldatetime] NULL,
      [LUB] [int] NULL,
      [LUD] [smalldatetime] NULL,
      [LPlanID] ASC

I need one view, which return Count of LPlanID and list with ID separated with comman for which are this count. I guess about this:

SELECT COUNT(LPlanID) AS Expr1, dbo.fn_GetID(LCampaignFileID) AS Expr2
FROM  dbo.LPlans
WHERE (LCampaignFileID = 2)
GROUP BY dbo.fn_GetID()

And dbo.fn_GetID(LCampaignFileID) is User Defined function in which I loop via record with   CURSOR and manually make return string for example 5,6,9,10 (List with ID of LPlanID)

This will work, but will be slow. Can somebady offer better way ?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Chris LuttrellConnect With a Mentor Senior Database ArchitectCommented:
I believe you are looking for something like the code below.  If you don't really need a view you can just add the where clause to the query used in the view.
Create view YourView as
SELECT LCampaignFileID, 
       COUNT(LPlanID) as LPlanIDCount, 
       LPlanIDList = REPLACE((SELECT LPlanID AS [data()]
                   FROM dbo.LPlans AS b
                   WHERE b.LCampaignFileID = a.LCampaignFileID
                   FOR XML PATH ('')), ' ', ',')
FROM dbo.LPlans AS a
GROUP BY a.LCampaignFileID
SELECT LPlanIDCount, LPlanIDList
FROM  YourView
WHERE (LCampaignFileID = 2)

Open in new window

I am having difficulty understanding you.  Maybe someone else will better understand you and be able to answer, but you might consider getting someone with better English skills to help you word your question, or just try to be as detailed as possible.
dvplayltdAuthor Commented:
OK, I will try again. I need a view, which return to me count of record and list with this record ID separated with comma . Very simple demand. Here you data which I expect to get like result

  CountLplanID   ListID
2                            3,4

3 and 4 are values of LPlanID , which are counted in first field.
I know that result in kind 3,4 is not common, however I need it.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

I don't think you can create the comma seperated list without looping through the records and manipulating a string. If you could post your user defined function that would be useful.

Can you explain why you need to have a comma delimited list in SQL. Is it possible to just select the raw data in SQL and return it to code where you can manipulate it?
What you're asking for is certainly not something that an SQL statement is designed to do and it may reflect a poor implementation of whatever process it is that needs these results.

If you can't do what QuintainT suggested, and you need to have SQL give you the results in this format then you should either do the entire process in your function or else in a stored procedure.  To do it in an SQL function would require subqueries which make for very slow SQL statements (and that's basically what you have now with the function you are calling from your SQL).  Instead, use a stored procedure to loop through the records one at a time and create the result as it goes.  This way you only traverse the records one single time and it should be much faster.  I think that stored procedures can have output parameters (sorry, I'm not an expert on them so I'm not positive), but if not then you can just use the stored procedure to create a tempory table that contains 1 record with the two fields you need.

Hope that helps
Edit:  Where I said "To do it in an SQL function..." I meant to say "To do it in an SQL statement..."
dvplayltdAuthor Commented:
Very correct answer to question, thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.