I have a 5 columns table. Column1 which has values 'A', 'B', 'C' and 'D' and another column2 having int values from 0 to 20, and column3, column4 and column5 have other values, not important for now.
I want to select all records in the table, but not records having in column1 value 'B'. However, records which have in colum1 value 'B', should meet the condition having in column2 a value > 10. If they have in column2 for ex. 15, we can include them in the select.
How to select this?
SELECT
column1
, column2
, column3
, column4
, column5
FROM tableA
WHERE
only_include_values_B_from_column1_IF_it_has_in_column2_value_superior_than_10
If I understand correctly, you want OR:
SELECT column1, column2, column3, column4, column5
FROM tableA a
WHERE column1 <> 'B' OR column2 > 10
Related
I have scenarios where there are multiple tables Table1, Table2, and Table3 and there are some common columns in them. Now I have to take join with tables on bases of the condition if record exists from the table than its good but if it not exist then it doesn't return any row but I have to return some default/0
select 'Section','Table1',column1, column2, column3 from table1 where column>1
union
select 'Section','Table2',column1, column2, column3 from table2 where column>3
union
select 'Section','Table3',column1, column2, column3 from table3 where column>2
suppose data doesn't exist in table 2 instead of skipping that table record should show in the result
in simple I want if the record not exist against any table it would be replaced by the below code
select 'Section','Table2',0 as column1, 0 as column2, 0 as column3
Output should be like this
Results
Section Table1 2 2022-06-12 abc
Section Table2 0 '' ''
Section Table3 3 2022-07-22 Xyz
You can use EXISTS. ie:
select 'Section','Table1',column1, column2, column3
from (values (0,0,0))
t(column1,column2,column3)
where not exists (select * from table1 where column1 > 1)
union
select 'Section','Table1',column1, column2, column3 from table1 where column1>1
union
select 'Section','Table2',column1, column2, column3
from (values (0,0,0))
t(column1,column2,column3)
where not exists (select * from table2 where column1 > 3)
union
select 'Section','Table2',column1, column2, column3 from table2 where column1>3
union
select 'Section','Table3',column1, column2, column3
from (values (0,0,0))
t(column1,column2,column3)
where not exists (select * from table3 where column1 > 2)
union
select 'Section','Table3',column1, column2, column3 from table3 where column1>2;
DBFiddle demo
It is SQL server but is valid for many databases if not all.
Wanted to know if I could "artificially" insert new records when a record contains a value for a specific column. For example say I have this table in my database with the following two records:
Column1 Column2 Column3
-------------------------
DataA1 DataA2 null
DataB1 DataB2 DataB3
Now Column3 is the column I want to trigger an extra row if there is a value. Column3 is essentially Column2 but with another value (this is non-normalized and I can't change it so I need to resort to a query instead). So I want to create a query that returns 3 rows using the example above and it should come out like this:
DataA1 DataA2
DataB1 DataB2
DataB1 DataB3
How do I write my sql to return the results above?
Use union all:
SELECT Column1, Column2
FROM TableName
WHERE Column3 IS NULL
UNION ALL
SELECT Column1, Column3
FROM TableName
WHERE Column3 IS NOT NULL
Not totally sure what you want here but I think you are looking for something like this.
select Column1
, Column2
from SomeTable
where Column2 is not null
UNION ALL
select Column1
, Column3
from SomeTable
where Column3 is not null
You could use a UNION statement to merge a result set that uses the third column as the second column when the third column is not null:
SELECT column1, column2
FROM Sample
UNION
SELECT column1, column3
FROM Sample
WHERE column3 IS NOT NULL
http://sqlfiddle.com/#!9/42ca15/6
i have a hive table Android_1 with 6 columns among those 6 columns:
android_id, column1, column2, column3, column4, column5
another table ios_2 with 6 columns among those tables:
ios_id, column1, column2, column3, column4, column5
now i have table combine which has 5 columns among those 5 columns one column is id and rest are column1,column2, column3,column4.now i want to insert the data into the table combine so that both data in the Android_id and ios_id should come under id column in table combine and the data in Android_1 in column1,column2,column3,column4 and the data in ios_2 in column1,column2, column3,column4 shoud come under table combine column1,column2, column3,column4
Sounds to me like you want a UNION -- like this:
SELECT android_id as id, column1, column2, column3, column4, column5
FROM android_table
UNION ALL
SELECT ios_id as id, column1, column2, column3, column4, column5
FROM ios_table
I have a table with 4 bit columns
I need to create a report that will show the total of all "true" values for each column but I need the column names to return as a row.
For examples, the table will contain:
Column1 Column2 Column3
1 1 0
0 1 0
1 1 0
The result should be:
Category Value
Column1 2
Column2 3
Column3 0
The table has other columns, I just need specific ones
Thanks
I don't know if there are other approaches, but the following should work:
select 'Column1' as "Category", sum(column1) as "Value" from my_table union
select 'Column2', sum(column2) from my_table union
select 'Column3', sum(column3) from my_table
Here's a SQLFiddle for it.
You can try UNPIVOT on the table (this is for SQL Server)
create table Test (Column1 bit, Column2 bit, Column3 bit)
insert into Test values (1,1,0)
insert into Test values (0,1,0)
insert into Test values (1,1,0)
SELECT Value, sum(Vals)
FROM
(CONVERT(INT, Column1) Column1, CONVERT(INT, Column2) Column2, CONVERT(INT, Column3) Column3
FROM Test) p
UNPIVOT
(Vals FOR Value IN
(Column1, Column2, Column3)
)AS unpvt
GROUP BY Value
PIVOT/UNPIVOT documentation
http://sqlfiddle.com/#!6/957c6/1/0
Try this:
select
category = "column1", value = sum (convert(int,col1)) from MyTable1
union
select
category = "column2", value = sum (convert(int,col2)) from MyTable1
union
select
category = "column3", value = sum (convert(int,col3)) from MyTable1
I want to copy the contents of table1 into table2, but it's not a straight copy, as table2 contains more columns than table 1. The structure is similar to this:
Table1
{
column2
column4
column6
}
Table2
{
column1
column2
column3
column4
column5
column6
}
What I want to do is add every row from table1 to table2, and set default values for the missing columns. Any help would be appreciated.
You can just do an
INSERT INTO xxx
SELECT yyy
And in the select clause, put default values.
INSERT INTO Table2(column1, column2, column3, column4, column5, column6)
SELECT 'horse', column2, 2, column4, 'what ever I want', column6
FROM Table1
So int Table2, all column1 will have 'horse' value.
All column3 will have 2.
Etc.
use INSERT..INTO SELECT statement
INSERT INTO table2 (column2, column4, column6)
SELECT column2, column4, column6
FROM table1
so in this case, columns: column1, column2, column3 will have null values. or whatever default you have set.
Not great at SQL but something like this:
INSERT INTO [Table1] (column2, column4, column6) SELECT (column1, column2, column3) FROM [Table2]
Hope this helps. Link that may be useful, http://www.blackwasp.co.uk/SQLSelectInsert.aspx
vote me I need points :P
Please try
INSERT INTO
Table2 (
column1,
column2,
column3,
column4,
column5,
column6,
)
SELECT
'StaticValue1',
column2,
'StaticValue2'
column4,
'StaticValue3'
column6,
FROM
Table1
To copy a full table into a new table:
SELECT * INTO table2 FROM table1;
http://www.w3schools.com/sql/sql_select_into.asp
To copy a table into an existing table:
INSERT INTO table2
SELECT * FROM table1;
http://www.w3schools.com/sql/sql_insert_into_select.asp
insert into TABLE2
select null colum1, column2,null colum3,column4,null colum5,column6
from((
Select TABLE1.column2, TABLE1.column4, TABLE1.column6
from TABLE1, TABLE2
where TABLE1.primarykey=TABLE2.primarykey))