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 |
|
sign_seventh
Yak Posting Veteran
66 Posts |
Posted - 2008-04-11 : 02:24:46
|
hi i have herecolAName | colBValue-------- ---------A 1C 2F 3H 4 i need a query where let say i want to get value of A to G and will going to get result like thisA 1B 1C 2D 2E 2F 3G 3 Bcoz letter B,D,E,G do not exist in the table, the value that they will going to have will be the value of the previous letter which exist in the table. Like B will going to get the value of A,D and E = value C,. etc. I hope you can help me. |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-04-11 : 03:08:32
|
| Use the order clause in your query and order columen colAName asc |
 |
|
|
pravin14u
Posting Yak Master
246 Posts |
Posted - 2008-04-11 : 03:11:47
|
quote: Originally posted by afrika Use the order clause in your query and order columen colAName asc
The Requirement is that, even if you dont have any alphabets, those should be dissplayed with the value of last existing alphabetif Cols haveA-1C-2B should also be displayed in the result with A's Value(i,e)A-1B-1C-2etc.,Prakash.PThe secret to creativity is knowing how to hide your sources! |
 |
|
|
sign_seventh
Yak Posting Veteran
66 Posts |
Posted - 2008-04-11 : 03:21:39
|
yes..:)quote: Originally posted by pravin14u
quote: Originally posted by afrika Use the order clause in your query and order columen colAName asc
The Requirement is that, even if you dont have any alphabets, those should be dissplayed with the value of last existing alphabetif Cols haveA-1C-2B should also be displayed in the result with A's Value(i,e)A-1B-1C-2etc.,Prakash.PThe secret to creativity is knowing how to hide your sources!
|
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-04-11 : 05:07:25
|
Here's one way...declare @t table (colAName char(1), colBValue int)insert @t select 'A', 1union all select 'C', 2union all select 'F', 3union all select 'H', 4declare @letters table (x char(1))insert @letters select distinct char(number + 64) from master.dbo.spt_values where number between 1 and 26select x, max(colBValue) as y from @letters inner join @t on x >= colANamewhere x < (select max(colAName) from @t)group by x order by x Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
sign_seventh
Yak Posting Veteran
66 Posts |
Posted - 2008-04-11 : 05:44:35
|
| ok i will try this. |
 |
|
|
sign_seventh
Yak Posting Veteran
66 Posts |
Posted - 2008-04-13 : 20:50:31
|
| records A,B,C is just my sample data, what if it is a Month?.can you give me some sample with no help from master dbase. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-14 : 00:06:23
|
| Try like this:-DECLARE @MonthNumbers TABLE(ID identity(1,1),MonthNumber int)DECLARE @Temp TABLE(ID identity(1,1),ColA varchar(20),ColB int)INSERT INTO @MonthNumbers(MonthNumber)SELECT 1UNION ALLSELECT 2...SELECT 12INSERT INTO @Temp (ColA,ColB)SELECT colAName,colBValueFROM YourTableSELECT t.Current,t.CurrentValueFROM (SELECT t1.colAName AS Current,t1.colBValue AS CurrentValue,t2.colAName AS NextFROM @Temp t1LEFT JOIN @Temp t2ON t2.ID=t1.ID+1)tCROSS JOIN @MonthNumbers mnWHERE mn.MonthNumber<= DATEDIFF(mm,CAST('01 ' + t.Current + ' 2008' AS datetime),CAST('01 ' + ISNULL(t.Next,t.Current) + ' 2008' AS datetime))+1 You can dispense with the code in red if you have an id column in your table. Also i've jsut put a random year and date for date comparison. You can put year and day value if you have them as seperate columns in your table. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-04-14 : 05:19:48
|
Here's my version adapted to your new requirements...--Preparation (you may not need some of this if you have it already)DECLARE @Months TABLE (MonthNumber int, MonthName varchar(10))INSERT INTO @Months (MonthNumber) SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12update @Months set MonthName = datename(month, dateadd(month, MonthNumber-1, 0))declare @t table (MonthName varchar(10), colBValue int)insert @t select 'January', 1union all select 'March', 2union all select 'June', 3union all select 'August', 4--Query (the bit you need)select a.MonthNumber, a.MonthName, max(b.colBValue) as colBValuefrom @Months a inner join @t b on a.MonthNumber >= month(cast('1 ' + b.MonthName + ' 1900' as datetime))group by a.MonthNumber, a.MonthName order by a.MonthNumber/* resultsMonthNumber MonthName colBValue----------- ---------- -----------1 January 12 February 13 March 24 April 25 May 26 June 37 July 38 August 49 September 410 October 411 November 412 December 4*/Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 05:22:31
|
What about OP posted real problem to start with?Now we have to think twice and spend twice as much time with this problem... E 12°55'05.25"N 56°04'39.16" |
 |
|
|
pravin14u
Posting Yak Master
246 Posts |
Posted - 2008-04-14 : 23:53:47
|
| The following will work for AlphabetsCREATE TABLE [dbo].[Alpha]( [Alpha] [nchar](10) NULL, [No] [smallint] NULL) ON [PRIMARY]INSERT INTO ALPHA VALUES('A',1)INSERT INTO ALPHA VALUES('E',5)INSERT INTO ALPHA VALUES('T',4)INSERT INTO ALPHA VALUES('Y',7)DECLARE @LIST VARCHAR(26)DECLARE @N INTDECLARE @ALPHA CHAR(1)DECLARE @NO INTSET @LIST ='ABCDEFGHIJKLMNOPQRSTUVWXYZ'SET @N=1CREATE TABLE #MyTempTable (ALPHA CHAR(1),[NO] INT)WHILE @N<27BEGINPRINT SUBSTRING(@LIST,@N,1)INSERT INTO #MyTempTable SELECT SUBSTRING(@LIST,@N,1) AS ALPHA,CASE WHEN EXISTS(SELECT * FROM ALPHA WHERE ALPHA= SUBSTRING(@LIST,@N,1)) THEN (SELECT NO FROM ALPHA WHERE ALPHA= SUBSTRING(@LIST,@N,1)) ELSE (SELECT NO FROM #MyTempTable WHERE ALPHA= SUBSTRING(@LIST,@N-1,1) ) END AS ALPHASET @N=@N+1ENDSELECT * FROM #MyTempTable-- Dropping the Temp TableDROP TABLE #MyTempTablePrakash.PThe secret to creativity is knowing how to hide your sources! |
 |
|
|
|
|
|
|
|