Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Question about SQL

Posted on 2009-04-11
7
282 Views
Last Modified: 2012-05-06
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,
 CONSTRAINT [PK_LPlans] PRIMARY KEY CLUSTERED
(
      [LPlanID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


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 ?
0
Comment
Question by:dvplayltd
7 Comments
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24121142
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.
0
 

Author Comment

by:dvplayltd
ID: 24121198
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.
0
 
LVL 5

Expert Comment

by:QuintainT
ID: 24121237
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?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 24121250
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.
HTH
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
GO
 
SELECT LPlanIDCount, LPlanIDList
FROM  YourView
WHERE (LCampaignFileID = 2)

Open in new window

0
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24121277
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
0
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24121278
Edit:  Where I said "To do it in an SQL function..." I meant to say "To do it in an SQL statement..."
0
 

Author Closing Comment

by:dvplayltd
ID: 31569145
Very correct answer to question, thank you!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question