Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ajustice
Starting Member
2 Posts |
Posted - 2005-09-17 : 13:51:38
|
I need some help with writing a stored procedure. Any suggestions would be greatly appreciated!I am going to give fictional data and circumstance to help simplify my question. Let’s say we have 4 columns:ColA smalldatetime Time-stamp of record entryColB int Number identifying data (1 through 4)ColC int DataColD int Data First off, let me give you a sample data list to help me explain what I need (I used only dates for ColA for ease of explanation):ColA ColB ColC ColD8/1 1 10 508/1 3 10 308/1 2 10 308/1 4 60 208/2 3 10 308/2 2 10 308/2 1 10 508/2 4 60 208/3 4 60 208/3 3 10 508/3 2 10 308/3 1 10 508/4 1 30 608/4 2 10 308/4 3 10 508/4 4 10 508/5 1 30 608/5 3 10 508/5 2 10 308/5 4 10 508/6 1 30 608/6 2 10 308/6 3 10 308/6 4 10 308/7 1 20 408/7 4 10 308/7 3 10 308/7 2 10 308/8 4 10 308/8 2 10 308/8 1 20 408/8 3 10 30 I want to find records that are DISTINCT in ColC and ColD for each ColB, and I can do this by the following://***********************************************************//SET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOCREATE PROCEDURE usp_TEST @start smalldatetime, @end smalldatetimeASSET NOCOUNT ONSELECT DISTINCT[ColB], [ColC], [ColD]FROM [SQL].[dbo].[Test]WHERE ColA >= @startAND ColA <= @endGROUP BY [ColB], [ColC], [ColD]ORDER BY ColBGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO//***********************************************************// Which would return:ColB ColC ColD1 10 501 30 601 20 402 10 303 10 303 10 504 60 204 10 504 10 30 But I want to know what was the first and last TimeStamp (ColA) for that particular record. And how many (COUNT) records were there. Giving a return like this:ColB ColC ColD StartDate EndDate Count1 10 50 8/1 8/3 31 30 60 8/4 8/6 31 20 40 8/7 8/8 22 10 30 8/1 8/8 83 10 30 8/1 8/2 23 10 50 8/3 8/5 33 10 30 8/6 8/8 34 60 20 8/1 8/3 34 10 50 8/4 8/5 24 10 30 8/6 8/8 3 Notice that for ColB = 3 that there are two sets of ColC = 10, and ColD = 30 BUT the "run" was interrupted by the ColC = 10 and ColD = 50, so I do in fact want BOTH returns.BUT I don’t know how to put this into a stored procedure – I can’t include the ColA in the Select DISTINCT, or else I would get every record.I am new with SQL, and I am trying my best! The actual data I am looking at are Batch Records, which include what Recipe was used. So the return will tell me: what recipe was used and for how long, and for how many batches. Thank you in advance for your help!Thanks,ajustice |
|
|
ajustice
Starting Member
2 Posts |
Posted - 2005-09-17 : 15:27:56
|
UPDATEI think I may have figured part of it out://***********************************************************//SET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOCREATE PROCEDURE usp_TEST @start smalldatetime, @end smalldatetimeASSET NOCOUNT ONSELECTmin([ColA]) as FirstDate,max([ColA]) as LastDate,count([ColB]) as NumRecords,[ColB], [ColC], [ColD]FROM [SQL].[dbo].[Test]WHERE ColA >= @startAND ColA <= @endGROUP BY [ColB], [ColC], [ColD]ORDER BY ColBGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO//***********************************************************// But I don't believe this will work for when a "run" is split into two differant times. I will run this on my data and try to verify. Please post anything you may see! Thanks! It does work correctly EXCEPT it does not work when a "run" is split - so - I still need some advice on this - I will continue to search and try diff. things, and I will keep posting anything more that I figure out - Thanks! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-19 : 01:48:36
|
| >>It does work correctly EXCEPT it does not work when a "run" is split Can you explain it more?MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|