Cross product of columns [closed] - sql

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
How can we do cross product of columns of a table.
Suppose I have table with this data.
Name Age spend Gender
De 26 10 M
Fu 27 29 F
I want output should be
De 26 10 M
Fu 26 10 M
De 26 29 M
Fu 26 29 M
De 27 10 M
Fu 27 10 M
De 27 29 M
Fu 27 29 M
....
I am doing the same by putting all the columns in different-different table in a procedure.

can you try this?:
updated
you can test here. http://sqlfiddle.com/#!2/9264d7/2/0
SELECT *
FROM
(
SELECT DISTINCT name
FROM test
) x CROSS JOIN
(
SELECT DISTINCT age
FROM test
) y CROSS JOIN
(
SELECT DISTINCT spent
FROM test
) z CROSS JOIN
(
SELECT DISTINCT gender
FROM test
) a;

Related

SQL Query to output single column with below values in each row - 1 2 2 3 3 3 4 4 4 4 [closed]

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

HP vertica SQL query to create new column [closed]

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.

How to arrange sequence range in group by [closed]

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

Finding the Missing Sequence SQL Server [closed]

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
CNo Wno Lno
12 1 1
12 1 2
12 2 3
12 3 15
9 1 1
13 1 1
13 2 2
13 3 5
10 1 1
10 1 2
10 1 3
10 2 4
11 1 1
for Cno i need the missing sequence numbers in Lno
Eg:
for Cn0=12
the line no is missing from 4 to 14
and for Cno=13 the sequence number(3,4) of Lno is missing
i need to find out the missing sequence no's for the clno
You can use common table expression for generating range of numbers and then find missing ones:
with cte as (
select t.CNo, min(t.Lno) as Lno, max(t.Lno) as max_Lno from Table1 as t
group by t.CNo
union all
select c.CNo, c.Lno + 1 as Lno, c.max_Lno
from cte as c
where c.Lno < c.max_Lno
)
select c.Cno, c.Lno
from cte as c
where
not exists (
select *
from Table1 as t
where t.CNo = c.CNo and t.Lno = c.Lno
)
order by 1, 2
option (maxrecursion 0);
if you have tables with sequential numbers, you can do this:
select c.Cno, n.n as Lno
from numbers as n
inner join (
select
tt.CNo, min(tt.Lno) as min_Lno, max(tt.Lno) as max_Lno
from Table1 as tt
group by tt.CNo
) as c on c.min_Lno <= n.n and c.max_Lno >= n.n
where
not exists (
select *
from Table1 as t
where t.CNo = c.CNo and t.Lno = n.n
)
order by 1, 2;
sql fiddle demo
not 100% sure what you are trying to do but you could use a number table to help with this.
if you have a table called numbers like this:
number
1
2
3
4
5.. up to highest number you are interested in
then you could do something like:
select
n.number
from
numbers as n
left outer join table as t
on n.number = t.Lno
and t.cno = 12
where
n.number <= (select max(lno) from table where cno = 12)
and t.nco is null
I don't know what type of output you are looking for or how you want to select the missing numbers you are looking for.

Combine values of two rows into single row using SQL [closed]

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
ID QuestionNo AnswerPercent
1 15 10
1 16 10
1 17 20
1 18 25
2 15 30
2 16 0
2 17 15
2 18 25
Output
ID QuestionNo AnswerPercent
1 15 10
1 16 30
1 17 20
1 18 25
2 15 30
2 16 15
2 17 15
2 18 25
For each id the answer percent for questions 16 and 17 need to be merged into 16. Chances are for some ids there may not be any 16 or 17 question numbers.
Can anyone help me out in this. Thanks!.
I believe this is what you're after, an UPDATE with a JOIN to a subquery:
UPDATE A
SET A.AnswerPercent = B.AnswerPercent
FROM YourTable A
JOIN (SELECT ID, SUM(AnswerPercent)'AnswerPercent'
FROM YourTable
WHERE QuestionNo IN ('16','17')
GROUP BY ID
)B
ON A.ID = B.ID
WHERE A.QuestionNo = '16'
Demo: SQL Fiddle
try adding the table twice...
table aliased as a has all rows except the ones for question 17, and
table aliased as b has the rows for question 17
Select a.Id, a.QuestionNo,
a.AnswerPercent +
case A.QuestionNo When 16
then coalesce(b.AnswerPercent, 0) End
else 0 End AnswerPercent
From table a
left Join table b
on a.id = b.Id
And a.QuestionNo != 17
And b.QuestionNo = 17
if all you want is to update the existing table, then you need an update and a delete.
update a set
AnswerPercent = a.AnswerPercent +
IsNull(b.AnswerPercent, 0)
from table a
left Join table b
on a.id = b.Id
And a.QuestionNo = 16
And b.QuestionNo = 17
--and then ...
delete table where QuestionNo = 17
with aaa as(
select sum(AnswerPercent) as AnswerPercent,ID
from Table
where QuestionNo in (16,17)
group by ID)
select * from Table where QuestionNo <> 16
union
select ID, 16, sum
from aaa
order by ID,AnswerPercent