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.
Related
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 4 days ago.
Improve this question
Example tables:
Table A Table B
A 1
B 2
C 3
Expected output:
A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
You need a cross join between the two tables:
SELECT a.column_a, b.column_b
FROM a
CROSS JOIN b
ORDER BY a.column_a, b.column_b
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 3 years ago.
Improve this question
Populate sequence by group in sql server:
Input:-
ID data
1 0
1 0
1 0
2 0
2 0
2 0
Output:-
ID data
1 0
1 1
1 2
2 0
2 1
2 2
As it stands, per your sample data, you need to use ROW_NUMBER() along with partitioning.
SELECT ID, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) as DATA
FROM <table>
But because the ID column is not unique, the ORDER BY will not know how to discern between the first row with 1 and the third row with 1.
Which is why, I recommend in the ORDER BY ID part, to also add a unique/primary key column which will give you a deterministic order, so that you can always determine what value a certain row will have, in a fixed set of data.
So, if your table also contains a "PK" (primary key) or unique column:
PK ID data
1 1 0
2 1 1
3 1 2
4 2 0
5 2 1
6 2 2
then your select can turn into:
SELECT ID, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID, PK) as DATA
FROM <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 7 years ago.
Improve this question
ID Name Dept Project Job Platform
A101 1 0 1 1 0
A103 0 1 1 0 1
B201 1 1 0 0 0
C301 1 0 1 1 1
Any help will be greatly appreciated
EDIT: I have tried these queries:
SELECT count(sittingreading),count(WatchingTV) FROM table_name WHERE sittingreading IS NOT NULL
Select COUNT(*) from table_name where column_value=0
Use a case statement in count() to make SQL Server only count the rows where you have the value you are looking for.
select count(case when T.Name = 0 then 1 end) as NameCount,
count(case when T.Dept = 0 then 1 end) as DeptCount,
count(case when T.Project = 0 then 1 end) as ProjectCount
from YourTable as T
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
Let's assume I have a table: Where x is value of chose1 and y value of chose2
ID Chose1 Chose2 x y
1 A B 2 3
2 C D 3 5
From this I would like to get something like this: Where z is x*y (if chose is empty,y=1).
NR ID Chose1 Chose2 z
1 1 A 2
2 1 A B 6
3 1 B 3
4 2 C 3
5 2 C D 15
6 2 D 5
This select statement will get the data above with the examples you gave:
select (ID-1)*3+1 as NR, id, Choose1, null as Choose2, x as z from tbl_values
union
select (ID-1)*3+2, id, Choose1, Choose2, x*y from tbl_values
union
select (ID-1)*3+3, id, null, Choose2, y from tbl_values
order by NR
Demo here (tested in MySQL, SQL Server): http://sqlfiddle.com/#!2/5af4b/3