0

I have a very simple select query which is being used to create an input file for a piece of software. I have the query pulling all the required fields, however I need to replicate the results six times with a hard coded ID number (1,2,3,4,5,6).

I have seen CROSS APPLY and PIVOT but the problem is the column I need to use for these doesn't exist as I'm hard coded then number.

Any help would be much appreciated.

Thanks in Advance

2
  • Apologies the query was so simple I didn't think I needed to add it but as requested: SELECT 1 AS ID, [Reference], [Name], [Business Type] FROM Quarterly_Download Commented Nov 20, 2017 at 11:35
  • I asked because there are different ways depending on whether it is an aggregate query or not. Commented Nov 20, 2017 at 11:50

2 Answers 2

1

Maybe like this:

select CJ.ID,T.* from dbo.Table T CROSS JOIN (select 1 ID UNION ALL select 2 ID UNION ALL select 3 ID UNION ALL select 4 ID UNION ALL select 5 ID UNION ALL select 6 ID) CJ 
Sign up to request clarification or add additional context in comments.

Comments

1

Bit of a pure guess here, but are you saying that every row in your table needs to be repeated 6 times with the ID 1-6? If so, you can use a CTE of the values 1-6 and CROSS APPLY to that.

WITH Nums AS( SELECT * FROM (VALUES (1),(2),(3),(4),(5),(6)) V(N)) SELECT * FROM YourTable YT CROSS APPLY Nums; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.