views:

53

answers:

3
SELECT * FROM TABLE1
ID      Name 
1       SOF
2       USER

I want query which can one or two or four .... times ID = 1 in row like

What will be the Query of this in Ms.Access?

ID      Name 
1       SOF
1       SOF
1       SOF
2       USER
+1  A: 

Following example uses SQL Server's CTE syntax.

As it stands, the @ID gets selected @Count times, all other records are selected one time. It would be fairly easy to extend it to whatever count for whatever ID's you like.

Setup

DECLARE @Table TABLE (ID INTEGER, Name VARCHAR(32))
DECLARE @Count INTEGER
DECLARE @ID INTEGER

SET @Count = 3
SET @ID = 1

INSERT    INTO @Table 
          SELECT 1, 'SOF'
UNION ALL SELECT 2, 'USER'

SQL Statement

;WITH Multiple AS (
  SELECT  ID, Name, cnt = 1
  FROM    @Table
  WHERE   ID = @ID
  UNION ALL
  SELECT  ID, Name, cnt = cnt + 1
  FROM    multiple
  WHERE   cnt < @Count
)
SELECT  ID, Name
FROM    Multiple
UNION ALL
SELECT  ID, Name
FROM    @Table
WHERE   ID <> @ID
Lieven
Remember i using MS Access
SOF User
How can I. You've only just added that tag?!
Lieven
I m sorry i forgot lieven i have added ms-access tag now
SOF User
Don't worry about it but I have no idea how to do it using Access besides making it a Pass Through query (to SQL Server :).
Lieven
A: 

This solution depends on having a LabelCount field (column) with the required number of labels, and a Coiunter table, with a field, Num, that contains integers from 1 to maximum number of labels required:

SELECT Id, [Name]
FROM Table1, [Counter]
WHERE Counter.Num<=[LabelCount]
Remou
A: 

In an Access report it can be done without changing the SQL. This really ancient Knowledge Base article explains it, and I've had it in production use for nearly 15 years!

How to Skip Used Mailing Labels and Print Duplicates

David-W-Fenton