SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


select to add alternate lines with text as 'go'


select to add alternate lines with text as 'go'

Author
Message
Krishna1
Krishna1
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 556
Dear All

I have a query select Id from abc which returs
1
2
3

I want it to return
1
go
2
go
3
go


Is there a way to achive this suing single selct or i need to use cursor only?
LutzM
LutzM
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10465 Visits: 13559
I'd use the cte approach. I'm confident it can be compressed a little further but this would be my starting point:

WITH cte_originalquery AS
(
SELECT 1 AS Number UNION ALL
SELECT 2 AS Number UNION ALL
SELECT 3 AS Number
),
cte_originalqueryExpanded AS -- add two columns to be used for output order
(
SELECT
1 AS Sort,
Number AS Sort2,
Number
FROM cte_originalquery
),
cte_helperquery AS -- add the required number of "go" lines using separate cte
(
SELECT
2 AS Sort,
Number AS Sort2,
'GO' AS AddLine
FROM cte_originalquery
),
cte_merged AS -- merge the two queries
(
SELECT
Sort,
Sort2,
AddLine
FROM cte_helperquery
UNION ALL
SELECT
Sort,
Sort2,
CAST(Number AS VARCHAR(10))
FROM cte_originalqueryExpanded
)
-- final output, ordered properly
SELECT AddLine as final
FROM cte_merged
ORDER BY Sort2,Sort





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Krishna1
Krishna1
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 556
thanks it worked
Arjun Sivadasan
Arjun Sivadasan
Say Hey Kid
Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)

Group: General Forum Members
Points: 674 Visits: 976
I'm assuming you want to copy the output and execute it.

Try this
select col1 + char(10) + 'go' from table
Cadavre
Cadavre
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3928 Visits: 8472
LutzM (2/23/2013)
I'd use the cte approach. I'm confident it can be compressed a little further but this would be my starting point:

WITH cte_originalquery AS
(
SELECT 1 AS Number UNION ALL
SELECT 2 AS Number UNION ALL
SELECT 3 AS Number
),
cte_originalqueryExpanded AS -- add two columns to be used for output order
(
SELECT
1 AS Sort,
Number AS Sort2,
Number
FROM cte_originalquery
),
cte_helperquery AS -- add the required number of "go" lines using separate cte
(
SELECT
2 AS Sort,
Number AS Sort2,
'GO' AS AddLine
FROM cte_originalquery
),
cte_merged AS -- merge the two queries
(
SELECT
Sort,
Sort2,
AddLine
FROM cte_helperquery
UNION ALL
SELECT
Sort,
Sort2,
CAST(Number AS VARCHAR(10))
FROM cte_originalqueryExpanded
)
-- final output, ordered properly
SELECT AddLine as final
FROM cte_merged
ORDER BY Sort2,Sort



Is all that necessary Lutz? Could you not do something like this: -
--Sample data
IF object_id('tempdb..#abc') IS NOT NULL
BEGIN
DROP TABLE #abc;
END;

SELECT Id
INTO #abc
FROM (VALUES(1),(2),(3))a(Id);

--==========================================================================--
--ACTUAL QUERY
--==========================================================================--
SELECT Id
FROM (SELECT CAST(Id AS VARCHAR(2)), (ROW_NUMBER() OVER(ORDER BY Id)*2)-1
FROM #abc
UNION ALL
SELECT 'go', ROW_NUMBER() OVER(ORDER BY Id)*2
FROM #abc
)a(Id,Sort)
ORDER BY Sort;




--EDIT--

Well, I'm going to concede to being wrong here.

SET NOCOUNT ON;

--Sample data
IF object_id('tempdb..#abc') IS NOT NULL
BEGIN
DROP TABLE #abc;
END;

--1,000,000 Random rows of data
SELECT TOP 1000000 IDENTITY(INT,1,1) AS Id
INTO #abc
FROM master.dbo.syscolumns sc1, master.dbo.syscolumns sc2, master.dbo.syscolumns sc3;

DECLARE @HOLDER VARCHAR(10), @Duration CHAR(12), @StartTime DATETIME;

DBCC FREEPROCCACHE WITH NO_INFOMSGS;
DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS;

SELECT @StartTime = GETDATE();

SELECT @HOLDER = Id
FROM (SELECT CAST(Id AS VARCHAR(10)), (ROW_NUMBER() OVER(ORDER BY Id)*2)-1
FROM #abc
UNION ALL
SELECT 'go', ROW_NUMBER() OVER(ORDER BY Id)*2
FROM #abc
)a(Id,Sort)
ORDER BY Sort;

SELECT @Duration = CONVERT(CHAR(12),GETDATE()-@StartTime,114);
RAISERROR('Duration: %s',0,1,@Duration) WITH NOWAIT;

DBCC FREEPROCCACHE WITH NO_INFOMSGS;
DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS;

SELECT @StartTime = GETDATE();

with cte_originalqueryExpanded AS -- add two columns to be used for output order
(
SELECT
1 AS Sort,
Id AS Sort2,
Id
FROM #abc
),
cte_helperquery AS -- add the required number of "go" lines using separate cte
(
SELECT
2 AS Sort,
Id AS Sort2,
'GO' AS AddLine
FROM #abc
),
cte_merged AS -- merge the two queries
(
SELECT
Sort,
Sort2,
AddLine
FROM cte_helperquery
UNION ALL
SELECT
Sort,
Sort2,
CAST(Id AS VARCHAR(10))
FROM cte_originalqueryExpanded
)
-- final output, ordered properly
SELECT @HOLDER = AddLine
FROM cte_merged
ORDER BY Sort2,Sort

SELECT @Duration = CONVERT(CHAR(12),GETDATE()-@StartTime,114);
RAISERROR('Duration: %s',0,1,@Duration) WITH NOWAIT;



Over a million rows (see above), the results were: -
Duration: 00:00:04:043
Duration: 00:00:03:893

So Lutz' was about 0.150 seconds faster.


--Second EDIT--

I changed my query slightly and improved the performance by getting rid of the ranking function. This will only work if the Id is numeric.

SELECT Id
FROM (SELECT CAST(Id AS VARCHAR(10)), (Id*2)-1
FROM #abc
UNION ALL
SELECT 'go', Id*2
FROM #abc
)a(Id,Sort)
ORDER BY Sort;



Now stick that into the 1 million row test harness: -
SET NOCOUNT ON;

--Sample data
IF object_id('tempdb..#abc') IS NOT NULL
BEGIN
DROP TABLE #abc;
END;

--1,000,000 Random rows of data
SELECT TOP 1000000 IDENTITY(INT,1,1) AS Id
INTO #abc
FROM master.dbo.syscolumns sc1, master.dbo.syscolumns sc2, master.dbo.syscolumns sc3;

DECLARE @HOLDER VARCHAR(10), @Duration CHAR(12), @StartTime DATETIME;

DBCC FREEPROCCACHE WITH NO_INFOMSGS;
DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS;

SELECT @StartTime = GETDATE();

SELECT @HOLDER = Id
FROM (SELECT CAST(Id AS VARCHAR(10)), (Id*2)-1
FROM #abc
UNION ALL
SELECT 'go', Id*2
FROM #abc
)a(Id,Sort)
ORDER BY Sort;

SELECT @Duration = CONVERT(CHAR(12),GETDATE()-@StartTime,114);
RAISERROR('Duration: %s',0,1,@Duration) WITH NOWAIT;

DBCC FREEPROCCACHE WITH NO_INFOMSGS;
DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS;

SELECT @StartTime = GETDATE();

with cte_originalqueryExpanded AS -- add two columns to be used for output order
(
SELECT
1 AS Sort,
Id AS Sort2,
Id
FROM #abc
),
cte_helperquery AS -- add the required number of "go" lines using separate cte
(
SELECT
2 AS Sort,
Id AS Sort2,
'GO' AS AddLine
FROM #abc
),
cte_merged AS -- merge the two queries
(
SELECT
Sort,
Sort2,
AddLine
FROM cte_helperquery
UNION ALL
SELECT
Sort,
Sort2,
CAST(Id AS VARCHAR(10))
FROM cte_originalqueryExpanded
)
-- final output, ordered properly
SELECT @HOLDER = AddLine
FROM cte_merged
ORDER BY Sort2,Sort

SELECT @Duration = CONVERT(CHAR(12),GETDATE()-@StartTime,114);
RAISERROR('Duration: %s',0,1,@Duration) WITH NOWAIT;



Duration: 00:00:03:533
Duration: 00:00:03:843

Now mine is fastest by ~310ms :-)


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search