Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how to Increment Data of the Same table with AutoIncrement .i have a table with 4 columns
id type no amount
1 type1 a1 1000
2 type1 a2 2000
3 type2 b1 3000
4 type3 c1 4000
using Loop how can i increment them to hundered rows same data .
id type no amount
1 type1 a1 1000
2 type1 a2 2000
3 type2 b1 3000
4 type3 c1 4000
5 type1 a1 1000
6 type1 a2 2000
7 type2 b1 3000
8 type3 c1 4000
9 type1 a1 1000
10 type1 a2 2000
11 type2 b1 3000
12 type3 c1 4000
Please suggest me the best using loop .How can i move forward on this one
Use a table with sufficient number of rows in a cross join.
This will add the rows you already have in your table 100 times.
insert into YourTable(type, no, amount)
select type, no, amount
from YourTable
cross join (
select top(100) 1
from sys.all_objects
) as T(X)
This is the way of inserting part of your table into same table.
INSERT INTO TABLE1
SELECT TOP 4 type,no,amount
FROM TABLE1
ORDER BY ID
There are two ways in which you can achieve the requirement:
If the insert statements are easy to script out and there are less records:
USE <database_name>
Go
INSERT INTO looprecords([type],[no],[amount]) VALUES('type1','a1',1000);
INSERT INTO looprecords([type],[no],[amount]) VALUES('type1','a2',2000);
INSERT INTO looprecords([type],[no],[amount]) VALUES('type2','b1',3000);
INSERT INTO looprecords([type],[no],[amount]) VALUES('type3','c1',4000);
Go 5 -- You can control this number on how many times you want to loop.
If the insert statements cannot be scripted easily then:
insert into looprecords([type],[no],[amount])
select [type],[no],[amount] from looprecords;
Every time you run it it inserts records in this sequence 1,2,4,8,16,32........so on.
(For example if there are 16 records existing, in the next run it will give you 32 records)
Put the T-SQL in any valid loop to get the desired results.
I hope it has answered your question.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I need to make a query to another table and add the result to the current one as a new record if there is no record with an id in it, but if the current table has a value with such an id then add the value.
table 1:
id
name
date
1
alex
2022-01-01
2
tom
2022-01-01
table 2:
id
name
pass
1
alex
111
3
mitch
222
expected result:
id
name
date
value
1
alex
2022-01-01
111
2
tom
2022-01-01
3
mitch
222
Its a merge results for both the tables so you query will be
select * from table1 full outer join table2 on table1.(some column name) = table2.(some column name)
here some column name represents you id in table1 and table2 respectively.
so your query will be
select * from table1 full outer join table2 on table1.id = table2.id
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This is the table I have and I have 5 distinct programs. when a user like a program it reads as follows:
User Program
----------------
A 1
A 4
B 2
B 4
B 5
However I want to write a query that will allow me to also see the 5 distinct programs for each of my user and create a new column that will take two value (binary) 1 if the user liked a specific program and if not 0. Any help will be appreciated. Thanks
User Program NewColumn
A 1 1
A 2 0
A 3 0
A 4 1
A 5 0
B 1 0
B 2 1
B 3 0
B 4 1
B 5 1
You can do this with a cross join and left join:
select u.user, p.program,
(case when t.user is not null then 1 else 0 end) as NewCol
from (select distinct user from table) u cross join
(select distinct program from table) p left join
table t
on u.user = t.user and p.program = t.program;
Note: You may already have tables with the users and the programs. If so, use those instead of the subqueries.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Having two tables, I want to display the result from alternate rows from both tables, just like UNION ALL.
Can you help me to find out the solution in a MS SQL Server query?
Records of Table1:
id - value
-------------
1 - abc
4 - dce
9 - fgh
16 - ijk
25 - lmn
Records of Table2:
id - value
-------------
5 - opq
10 - rst
15 - uvw
20 - xyz
25 - zab
The result I want:
Id - value
-----------
1 - abc
5 - opq
15 - uvw
9 - fgh
15 - uvw
20 - xyz
16 - ijk
25 - lmn
25 - zab
----------------
I think this will do it for you, but you have to change the query and add your table names and your column names in the ORDER BY statement of the OVER clause.
Also, take note that both of your tables must have the same number of columns, and the same datatypes in order for them to work in an UNION.
SELECT
ROW_NUMBER() OVER (ORDER BY column),
1 AS 'rowOrder',
*
FROM TABLE1
UNION ALL
SELECT
ROW_NUMBER() OVER (ORDER BY column),
2 AS 'rowOrder',
*
FROM TABLE2
ORDER BY 1, 2
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Select RowID from MainTable
------------------------
1
2
3
4
5
6
7
I have another table where it stores some numbers, so when I pass in those numbers as parameter I need the following out. All the inputs and outputs are in table. Input parameter is stored in input table. Select RowID from InputTable. Output should come from MainTable.
Case 1:
Input:
1
2
3
Output:
4
5
6
Case 2:
Input:
4
5
6
Output:
7
1
2
Case 3:
Input:
7
1
2
Output:
3
4
5
Case 4:
Input:
3
4
5
Output:
6
7
1
....
....
The only problem is reading it from the top of the table once it reaches the end of the table.
Input is stored in "InputTable"
Output must come from "MainTable"
Thanks
Please try:
declare #MainTable as table(RowID int)
insert into #MainTable values (1),(2),(3),(4),(5),(6),(7)
declare #tbl as table(RowID int)
insert into #tbl values (3),(4),(5)
select RowID from #MainTable
select top 3 RowID From(
select RowID from #MainTable where RowID>(select MAX(RowID) from #tbl)
union all
select RowID from #MainTable where RowID<(select MAX(RowID) from #tbl)
)x
where RowID not in (select RowID from #tbl) //**
**Note: For mandatory 3 row output, please remove this where condition.
I don't know what in the world is the best way to go about this. I have a very large array of columns, each one with 1-25 rows associated with it. I need to be able to combine all into one large column, skipping blanks if at all possible. Is this something that Access can do?
a b c d e f g h
3 0 1 1 1 1 1 5
3 5 6 8 8 3 5
1 1 2 2 1 5
4 4 2 1 1 5
1 5
there are no blanks within each column, but each column has a different number of numbers in it. they need to be added from left to right so a,b, c, d, e, f. And the 0 from be needs to be in the first blank cell after the second 3 in A. And the first 5 in H needs to be directly after the 1 in g, with no blanks.
So you want a result like:
3
3
0
5
1
4
1
6
1
4
etc?
Here is how I would approach the problem. Insert your array into a work table with an autonumber column (important to retain the order the data is in, databases do not guarnatee an order unless you can give them something to sort on) called id
as well as the array columns.
Create a final table with an autonumber column (see above note on why you need an automnumber) and the column you want as you final table.
Run a separate insert statment for each column in your work table and run them in the order you want the data.
so the inserts would look something like:
insert table2 (colA)
select columnA from table1 order by id
insert table2 (colA)
select columnB from table1 order by id
insert table2 (colA)
select columnC from table1 order by id
Now when you do select columnA from table2 order by id you should have the results you need.