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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 getting data

Author  Topic 

sign_seventh
Yak Posting Veteran

66 Posts

Posted - 2008-04-11 : 02:24:46
hi i have here

colAName | colBValue
-------- ---------
A 1
C 2
F 3
H 4


i need a query where let say i want to get value of A to G and will going to get result like this


A 1
B 1
C 2
D 2
E 2
F 3
G 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
Go to Top of Page

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 alphabet

if Cols have

A-1
C-2

B should also be displayed in the result with A's Value(i,e)

A-1
B-1
C-2
etc.,

Prakash.P
The secret to creativity is knowing how to hide your sources!
Go to Top of Page

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 alphabet

if Cols have

A-1
C-2

B should also be displayed in the result with A's Value(i,e)

A-1
B-1
C-2
etc.,

Prakash.P
The secret to creativity is knowing how to hide your sources!

Go to Top of Page

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', 1
union all select 'C', 2
union all select 'F', 3
union all select 'H', 4

declare @letters table (x char(1))
insert @letters
select distinct char(number + 64) from master.dbo.spt_values where number between 1 and 26

select x, max(colBValue) as y from @letters inner join @t on x >= colAName
where 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.
Go to Top of Page

sign_seventh
Yak Posting Veteran

66 Posts

Posted - 2008-04-11 : 05:44:35
ok i will try this.
Go to Top of Page

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.

Go to Top of Page

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 1
UNION ALL
SELECT 2
...
SELECT 12


INSERT INTO @Temp (ColA,ColB)
SELECT colAName,colBValue
FROM YourTable


SELECT t.Current,t.CurrentValue
FROM (SELECT t1.colAName AS Current,
t1.colBValue AS CurrentValue,
t2.colAName AS Next
FROM @Temp t1
LEFT JOIN @Temp t2
ON t2.ID=t1.ID+1)t
CROSS JOIN @MonthNumbers mn
WHERE 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.
Go to Top of Page

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 4
UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12

update @Months set MonthName = datename(month, dateadd(month, MonthNumber-1, 0))

declare @t table (MonthName varchar(10), colBValue int)
insert @t
select 'January', 1
union all select 'March', 2
union all select 'June', 3
union all select 'August', 4


--Query (the bit you need)
select a.MonthNumber, a.MonthName, max(b.colBValue) as colBValue
from @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


/* results
MonthNumber MonthName colBValue
----------- ---------- -----------
1 January 1
2 February 1
3 March 2
4 April 2
5 May 2
6 June 3
7 July 3
8 August 4
9 September 4
10 October 4
11 November 4
12 December 4
*/


Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

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"
Go to Top of Page

pravin14u
Posting Yak Master

246 Posts

Posted - 2008-04-14 : 23:53:47
The following will work for Alphabets

CREATE 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 INT
DECLARE @ALPHA CHAR(1)
DECLARE @NO INT
SET @LIST ='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
SET @N=1
CREATE TABLE #MyTempTable (ALPHA CHAR(1),[NO] INT)
WHILE @N<27
BEGIN
PRINT 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 ALPHA
SET @N=@N+1
END
SELECT * FROM #MyTempTable
-- Dropping the Temp Table
DROP TABLE #MyTempTable

Prakash.P
The secret to creativity is knowing how to hide your sources!
Go to Top of Page
   

- Advertisement -