Solved

Question about SQL

Posted on 2009-04-11
7
272 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Very correct answer to question, thank you!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now