Column update and insert 2 rows within the same table - sql

I have a table named table1 and the value in one columnA is X. So when this value is X we need to insert 2 new rows and update the columnA with values Z and Y.
Is it possible with an insert and Update statement?
I am thinking of below query to update the column but how to insert the two rows.
select * from table1 where columnA = 'x'
Update columnA ='Z'

You could use the following statements to insert in the table
INSERT INTO table1
SELECT 'y' columnA,
columnB,
columnC,
...
FROM table1
WHERE columnA = 'x';
INSERT INTO table1
SELECT 'z' columnA,
columnB,
columnC,
...
FROM table1
WHERE columnA = 'x';
But of course, you still need to provide criteria in WHERE clause to get the specific data you wanted replicated in the same table but with different columnA value. You really need to specify each column.

Related

SELECT all distinct rows with two simple characteristic

I have this table in 18c Oracle Database:
CREATE TABLE TABLEA
(
COLUMNA NUMBER(5) NOT NULL,
COLUMNB NUMBER(5) NOT NULL
)
I've this rows:
Insert into TABLEA
(COLUMNA, COLUMNB)
Values
(96, 1011),
(96, 9130),
(848, 1011),
(848, 1172),
(1095, 1011),
(1095, 1172);
It should be very simple but I really stuck here.
I need to get all the different values of COLUMNA, when all COLUMNB from the same COLUMNA are in (1172, 1011, 1037).
Result expected:
COLUMNA
----------
848
1095
The value 96 shouldn't be returned, because there are a row with a value in COLUMNB that aren't in the condition.
Those values (1172, 1011, 1037) can be in a specific table with a single column if it helps.
I've tried this, but I just want the distinct values:
SELECT columnA, COUNT (*) OVER (PARTITION BY columnA)
FROM tableA
WHERE EXISTS
(SELECT 1
FROM tableB -- With (1172, 1011, 1037) in each row
WHERE COLUMNB = values)
GROUP BY COLUMNA
HAVING COUNT (*) > 1;
Is there any other solutions?
Maybe you should use DISTINCT to show non repeated values.
Have you tried something like this?
SELECT DISTINCT columnA FROM tableA WHERE columnB in (1172, 1011, 1037)
Maybe something like so, not 100% it's late here :)
select
columna
from
#tablea
group by columna
having count(*)=sum(case when columnb in (1172, 1011, 1037) then 1 else 0 end)

SQL: Add a SUM(ColumnA) AS ColumnB to a query returning ColumnA

I have a query that returns a number of columns, including ColumnA (which is numerical).
I want to add an additional column to the end of the query that returns the sum of ColumnA
ColumnA
ColumnB
10
37
20
37
5
37
2
37
SELECT
ColumnA,
SUM(ColumnA) AS ColumnB
FROM
Table
The code above doesn't work, but I'm not sure how to create something that will.
I think you need this query:
SELECT ColumnA, (SELECT SUM(ColumnA) FROM table) as ColumnB
FROM table
Something like
SELECT
ColumnA,
ColumnASum
FROM Table
LEFT JOIN (SELECT SUM(columnA) ColumnASum FROM Table)
ON TRUE;
Should work
You could create a variable of the SUM() first.
DECLARE #ColumnB int
SET #ColumnB = (SELECT SUM(ColumnA) FROM Table)
SELECT ColumnA, #ColumnB
FROM Table
This should give you what you need.
I would use CROSS APPLY.
SELECT Table.ColumnA
,CA.ColumnB
FROM Table
CROSS APPLY (SELECT SUM(ColumnA) ColumnB FROM Table) CA
You basically define a subquery that outputs an aggregate value that you can have as another column.

Inserting values based on unique values of another table(Normalization)

I have Table1 with columns A,B & C.
I want to create Table 2 and create Primary Key for values in ColumnA, Table1(unique), and then accordingly populate values B and C from Table1 (based on unique values of Column A).
Any help will be appreciated.
EDIT:I am using SQL server and tried using INSERT INTO...SELECT DISTINCT.
If you are using MS SQL Server, Try this
WITH CTE
AS
(
SELECT
RN = ROW_NUMBER() OVER(PARTITION BY ColumnA ORDER BY ColumnA),
ColumnA,
ColumnB,
ColumnC
FROM YourTable
)
INSERT INTO Table2
(
ColumnA,
ColumnB,
ColumnC
)
SELECT
ColumnA,
ColumnB,
ColumnC
FROM CTE
WHERE RN = 1

How to delete all records returned by a subquery?

I want to delete all records that are returned by a certain query, but I can't figure out a proper way to do this. I tried to DELETE FROM mytable WHERE EXISTS (subquery), however, that deleted all records from the table and not just the ones selected by the subquery.
My subquery looks like this:
SELECT
MAX(columnA) as columnA,
-- 50 other columns
FROM myTable
GROUP BY
-- the 50 other columns above
having count(*) > 1;
This should be easy enough, but my mind is just stuck right now. I'm thankful for any suggestions.
Edit: columnA is not unique (also no other column in that table is globally unique)
Presumably, you want to use in:
DELETE FROM myTable
WHERE columnA IN (SELECT MAX(columnA) as columnA
FROM myTable
GROUP BY -- the 50 other columns above
HAVING count(*) > 1
);
This assumes that columnA is globally unique in the table. Otherwise, you will have to work a bit harder.
DELETE FROM myTable t
WHERE EXISTS (SELECT 1
FROM (SELECT MAX(columnA) as columnA,
col1, col2, . . .
FROM myTable
GROUP BY -- the 50 other columns above
HAVING count(*) > 1
) t2
WHERE t.columnA = t2.columnA AND
t.col1 = t2.col1 AND
t.col2 = t2.col2 AND . . .
);
And, even this isn't guaranteed to work if any of the columns have NULL values (although the conditions can be easily modified to handle this).
Another solution if the uniqueness is only guaranteed by a set of columns:
delete table1 where (col1, col2, ...) in (
select min(col1), col2, ...
from table1
where...
group by col2, ...
)
Null values will be ignored and not deleted.
To achieve this, try something like
with data (id, val1, val2) as
(
select 1, '10', 10 from dual union all
select 2, '20', 21 from dual union all
select 2, null, 21 from dual union all
select 2, '20', null from dual
)
-- map null values in column to a nonexistent value in this column
select * from data d where (d.id, nvl(d.val1, '#<null>')) in
(select dd.id, nvl(dd.val1, '#<null>') from data dd)
If you need to delete all the rows of a table such that the value of a given field is in the result of a query, you can use something like
delete table
my column in ( select column from ...)

SQL select column with same key if all values are null

I have two columns , ColumnA and ColumnB, occassionally columnB doesnt gets populated and it should e. I'm looking for a query that will only select if all ColumnA is unpopulated.
ColumnA|ColumnB
Apples|
Apples|
Apples|Orange
This is what i'm but this is incorrect because it says ColumnA is null with the same value and ColumnB is populated. I want the query only to return rows if all of columnB is unpopulated.
SELECT ColumnA
FROM tblMyTable
WHERE ColumnA IN
(SELECT ColumnA
FROM tblMyTableB
WHERE ColumnB IS NULL)
You current query gives you too many results. The ones you want to eliminate are those where there is a ColumnB value:
SELECT ColumnA
FROM tblMyTable
WHERE ColumnA IN
(SELECT ColumnA
FROM tblMyTableB
WHERE ColumnB IS NULL)
AND NOT ColumnA IN
(SELECT ColumnA
FROM tblMyTableB
WHERE ColumnB IS NOT NULL)
Or, smarter is:
select ColumnA,COUNT(ColumnB) from tblMyTable
group by ColumnA having COUNT(ColumnB) = 0
Because COUNT(Expression) only counts non-null expression values
It looks like your logic is backwards:
Your query finds values in column A where there is a NULL value in column B.
I think you want the values in column A where there isn't a non-NULL value in column B.
Try adding NOT in two places and add DISTINCT to avoid getting duplicate results:
SELECT DISTINCT ColumnA
FROM tblMyTable
WHERE ColumnA NOT IN
(SELECT ColumnA
FROM tblMyTableB
WHERE ColumnB IS NOT NULL)
In addition, if ColumnA can be NULL then you'll have to exclude those NULL values from your inner query otherwise the NOT IN expression will return NULL instead of True and so no results will be returned:
SELECT DISTINCT ColumnA
FROM tblMyTable
WHERE ColumnA NOT IN
(SELECT ColumnA
FROM tblMyTableB
WHERE ColumnA IS NOT NULL
AND ColumnB IS NOT NULL)
Using EXCEPT. This can be expressed as
Get Column A, EXCEPT where some non-null in Column B for that column A
DECLARE #MyTable TABLE (ColumnA varchar(20) NOT NULL, ColumnB varchar(20) NULL);
INSERT #MyTable VALUES
('Apple', NULL),('Apple', NULL),('Apple', 'Orange'),
('Banana', NULL),('Banana', NULL),
('Strawberry', 'Pie'), ('Strawberry', 'Pie')
SELECT ColumnA FROM #MyTable
EXCEPT
SELECT ColumnA FROM #MyTable WHERE ColumnB IS NOT NULL
More on EXCEPT: Why does EXCEPT exist in T-SQL?