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
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 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 10 months ago.
Improve this question
Need to write SQL query for getting below output with the given input table:
Input Table:
Col1
1
2
3
Output:
Col1
1
2
2
3
3
3
WITH CTE(NN)AS
(
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5
)
SELECT C.NN
FROM CTE AS C
CROSS JOIN CTE C2
WHERE C2.NN<=C.NN
CTE is your input table
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 2 years ago.
Improve this question
I have
id value
1 12
1 15
1 17
1 22
1 22
1 23
And I need like this
id value
1 --
1 3
1 2
1 5
1 0
1 1
Could you tell me, how to achive this?
You can try the below -
select id,
value-max(value) over(order by id rows between 1 preceding and 1 preceding) as value
from tablename
You seem to want lag(), which I'm guessing is per id and based on the ordering of value:
select t.*,
(value - lag(value) over (partition by id order by value)) as diff
from t
order by value;
That said, your sample data has exact duplicates. That is unusual in a SQL table.
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 9 years ago.
Improve this question
Hi all
i am facing a problem and need a query for that ,
I have a table and data like
----------
seq_id run_id mark_Flag
1 10 A
2 11 A
3 12 A
4 13 Z
5 14 A
6 15 A
7 16 Z
8 17 Z
9 18 A
10 19 A
11 20 Z
----------
Now i required the output like
seq runidFrom runidTo mark_Flag
1 10 12 A
2 13 13 Z
3 14 15 A
4 16 17 Z
5 18 19 A
6 20 20 Z
Thanks in advance ....
Try this query the main idea is to group by count of previous Mark_flag not equal to current
SELECT min(run_id),max(run_id),max(mark_Flag)
FROM T as T1
GROUP BY mark_flag,
(
SELECT COUNT(*) FROM T
WHERE seq_id<T1.seq_id
and
mark_flag<>T1.Mark_flag
)
ORDER BY MIN(seq_id)
SQLFiddle demo
This query have to work on any database system but when you post a question please add a tag with your RDBMS (not just SQL) so a query can be optimized for your data base system.
UPD: Here is the MS SQL version:
SELECT min(run_id),max(run_id),max(mark_Flag)
FROM
(
Select run_id, mark_flag,seq_id,
(
SELECT COUNT(*) FROM T
WHERE seq_id<T1.seq_id
and mark_flag<>T1.Mark_flag
) as group_id
From t as T1
) as T2
GROUP BY mark_flag,group_id
ORDER BY MIN(seq_id)
SQLFiddle demo
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.