creating 2 columns from 1 main column in the same table - sql

SELECT TOP 1000 [Value]
FROM [OnlineQnres].[dbo].[tmp_DataSets]
WHERE [VariableID] in ('1')
UNION ALL
SELECT TOP 1000 [Value]
FROM [OnlineQnres].[dbo].[tmp_oDataSets]
WHERE [VariableID] in ('4')
Providing a more updated Detail
Hi All, I have this select query above using UNION ALL. There is a column called Value and VariableID in tmp_datasets table. I need to create 2 separate columns and name them val1 for variableID is 1 and val2 if variableID is 4. If i use UNION ALL it works it creates 2000 records with the first 1000 as val1 records and next 1000 as val2 records but does not separate out into 2 sep columns. How do i separate this value column in 2 separate columns as stated above.
I have 2 columns
VALUE which has values TEST56,TEST57,230,245
VARIABLEID = 1 and 4
TEST56 AND TEST57 comes under variableid == 1
230 and 245 comes under variableid == 4
So based on this above example, I want to create a view where I have 2 columns called val1 if variableid == 1 and val2 if variable id == 4.
So it should look something like this
val1 = should show test56 and test57
val2 = should show 230 and 245
Thanks in advance

A couple case statements should do what I think you want:
SELECT TOP 1000
[Variable]
CASE [VariableID]
WHEN 1 THEN 1
ELSE 0
END AS Val1,
CASE [VariableID]
WHEN 4 THEN 1
ELSE 0
END AS Val2
FROM [OnlineQnres].[dbo].[tmp_DataSets]
WHERE [VariableID] = 1 or [VariableID] = 4
SQLFiddle link: http://sqlfiddle.com/#!6/825f2/8/0

Related

Excluding Data Values which common column

Currently, I've got a single set of data in which I want to exclude based on if a condition is meet. The group has a common column reference.
Name Sequence Value
-----------------------------------
Text 1 1
Don 1 30
Text 2 0
Sid 2 240
Florence 2 300
Text 3 200
Casper 3 20
Cat 3 10
Text 4 0
Dem 4 50
Basically any row in which Text is not equal to 0 needs to be excluded be excluded. In addition the rows in which share the same sequence. Expected outcome is to only have data from sequence 2 and 4.
You can try with NOT EXISTS as below-
SELECT Name,
Position,
Value
FROM your_table
WHERE NOT EXISTS (
SELECT Name,Position,Value
FROM your_table
WHERE (Name = 'Text' AND Value = 1)
OR (Position = Value)
)
As you are looking for options other than NOT EXISTS, you can try this below-
SELECT *
FROM your_table
WHERE [Sequence] NOT IN (
SELECT DISTINCT [Sequence]
FROM your_table
WHERE [Name] = 'Text'
AND [Value] <> 0
)

eliminating all the null values and showing all the values in one record

I need to make a select statement to table1 so that it looks like table2 oracle. Please help. I tried some case when and pivot but I failed again and again.
table1:
Product abc def ghi
1 100 0 0
1 0 10 0
1 0 0 20
2 0 0 80
2 0 60 0
2 3 0 0
table2:
product abc def ghi
1 100 10 20
2 3 60 80
Do aggregation :
select product, max(abc), max(def), max(ghi)
from table1 t1
group by product;
However, the value you have provided with sample data the sum() would also work. If, table has NULL instead of 0 then both function would work but i would prefer to use max() if the null values in table.

Find values which are present in all columns in a Cable

I would like a SQL Server query which finds the Values in a cell which fills multiple columns. For example, if I have table
ID Value1 Value2 Value3
1 2 NULL NULL
1 NULL 3 NULL
1 NULL NULL 4
1 3.4 NULL NULL
2 NULL 3 NULL
2 NULL NULL NULL
3 NULL NULL 91
As in the table above, only 2 of the columns can be filled at a time(First is ID and 2nd is either of Value1, 2 or 3) and ID can be repeated multiple times.
I want to return the ID as only 1 because 1 is the only ID that populates all the three other columns. 2 fills only Value2 and all the other values of 2nd iteration of 2 are NULL where as 3 is present only in Column Value3. Is there someway that I can find the Id's which fill all the other columns.
I would love to do this preferably without a cursor but I can go for cursor if it's compulsory. Thanks
EDIT
Desired Table:
ID
1
The Statement should return only the filtered IDs which populate all the other columns.
Try this
SELECT id,
FROM TableName
GROUP BY id
HAVING MAX(value1) IS NOT NULL AND
MAX(value2) IS NOT NULL AND
MAX(value3) IS NOT NULL
Something for you try if you want some less lines of code:
select ID from dbo.Table_1 group by ID having count(Value1) > 0 AND count(Value2) > 0 AND count(Value3) > 0

how to get the even and odd column separately with separate column by query

I have an input:
id
1
2
3
4
5
6
7
8
9
10
I want get even and odd columns separately by columns in specified output like this
id col
1 2
3 4
5 6
7 8
9 10
here id and col are separate columns id contains the odd number and col contains the even number for specified input
SELECT MIN(id) as id, MAX(id) as col
FROM YourTable
GROUP BY FLOOR((id+1)/2)
For IDs 1 and 2, (id+1)/2 are 2/2 = 1 and 3/2 = 1.5, respectively, and FLOOR then returns 1 for both of them. Similarly, for 3 and 4, this is 2, and so on. So it groups all the input rows into pairs based on this formula. Then it uses MIN and MAX within each group to get the lower and higher IDs of the pairs.
Joined the table on itself
select *
from yourTable tA
left join yourTable tb on tA.id = (tB.id - 1)
where tA.id % 2 <> 0
If you use SQL you can try:
SELECT CASE WHEN column % 2 = 1
THEN column
ELSE null
END AS odds,
CASE WHEN column % 2 = 2
THEN column
ELSE null
END AS even
FROM yourtable
but not exactl as you ask
To show odd:
Select * from MEN where (RowID % 2) = 1
To show even:
Select * from MEN where (RowID % 2) = 0
Now, just join those two result sets and that's it.
Source

Inserting a new indicator column to tell if a given row maximizes another column in SQL

I currently have a table in SQL that looks like this
PRODUCT_ID_1 PRODUCT_ID_2 SCORE
1 2 10
1 3 100
1 10 3000
2 10 10
3 35 100
3 2 1001
That is, PRODUCT_ID_1,PRODUCT_ID_2 is a primary key for this table.
What I would like to do is use this table to add in a row to tell whether or not the current row is the one that maximizes SCORE for a value of PRODUCT_ID_1.
In other words, what I would like to get is the following table:
PRODUCT_ID_1 PRODUCT_ID_2 SCORE IS_MAX_SCORE_FOR_ID_1
1 2 10 0
1 3 100 0
1 10 3000 1
2 10 10 1
3 35 100 0
3 2 1001 1
I am wondering how I can compute the IS_MAX_SCORE_FOR_ID_1 column and insert it into the table without having to create a new table.
You can try like this...
Select PRODUCT_ID_1, PRODUCT_ID_2 ,SCORE,
(Case when b.Score=
(Select Max(a.Score) from TableName a where a.PRODUCT_ID_1=b. PRODUCT_ID_1)
then 1 else 0 End) as IS_MAX_SCORE_FOR_ID_1
from TableName b
You can use a window function for this:
select product_id_1,
product_id_2,
score,
case
when score = max(score) over (partition by product_id_1) then 1
else 0
end as is_max_score_for_id_1
from the_table
order by product_id_1;
(The above is ANSI SQL and should run on any modern DBMS)