Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

select to add alternate lines with text as 'go' Expand / Collapse
Author
Message
Posted Saturday, February 23, 2013 3:06 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, April 27, 2014 7:55 PM
Points: 218, 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?




Post #1423333
Posted Saturday, February 23, 2013 3:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 4:15 PM
Points: 6,842, Visits: 13,370
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
Post #1423336
Posted Saturday, February 23, 2013 3:58 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, April 27, 2014 7:55 PM
Points: 218, Visits: 556
thanks it worked
Post #1423339
Posted Wednesday, February 27, 2013 4:11 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: 2 days ago @ 6:56 AM
Points: 406, Visits: 779
I'm assuming you want to copy the output and execute it.

Try this
select col1 + char(10) + 'go' from table
Post #1424457
Posted Wednesday, February 27, 2013 4:22 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 1:28 PM
Points: 2,386, Visits: 7,611
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



Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1424459
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse