Question about multiplying columns in SQL - sql

I was wondering if it was possible to multiply two columns and if so how would this take place
Suppose I have a table
a b
1 4
2 5
3 6
Could I do something like
SELECT a *b from table
Would this multiply the contents row by row then store it in a new column
Are these result right
4
10
18

That query would multiply the values, but it wouldn't "store it in a new column" To store it you would have to issue an update statement.
Assuming you add a new column ("c") to your table you could do:
update table
set c = a * b
If all you need is the new column in a result set, without modifying the underlying table you could:
select a, b, (a*b) as c from table

Yes you can perfectly do that.
update
To clarify: The query and output you mentioned in your question are correct.

Rather than storing a calculated column in a base table, consider a viewed table:
CREATE VIEW MyView
AS
SELECT a, b,
a * b AS my_calc
FROM MyTable;

Related

insert and update from table a to table b by combining 2 column from table a in postgres

I have 2 tables name Table A and Table B
"Table A has 10 columns named ta1, ta2, ta3...ta10"
"Table b has 2 columns name id (PK,AI) and tb1"
My requirement is to combine "ta3" and "ta4" from table A initially,
or it will check for the "tb1" column whether any same data is available, if available do nothing else insert the new data.(if a new row is added in the ta3 or ta4, it should be added to the 2nd table column named tb1)
I have checked other functions in the for combining the data from ta3 and ta4 by using the "unnest" function, but if I use that, I'm getting stuck in the insert, as it will insert on each query rather than checking whether any data is there or not.
any solution on this legends.
simply I just want to combine 2 column from 1st table and store it in a column on 2nd table, but in 2nd table's column it should be only distinct values taken from both of those 1st table columns. this should automatically check for the values in the column of 2nd table and compare that with the new one and if there is change the insert else neglect. I hope my question is clear.
Table1
Table 2
new table-1
Expected table-2

How to auto calculate 2 column and display at one column?

I want to alter, the table already created.
Column A + Column B = Column C.
Column A and B already has value, only C don't.
I just need to add function?
If so what is the function?
So next time I just need to add in the column A and B only, cause C already auto calculate
If your table currently has columns A and B but does not yet have C, then you can add it as a virtual column:
alter table your_table
add c number generated always as (a + b) virtual;
db<>fiddle
If A and/or B are nullable then you need to handle that; e.g. if you want to treat null as zero for the calculation you can do:
alter table your_table
add c number generated always as (coalesce(a, 0) + coalesce(b, 0)) virtual;
db<>fiddle
If the column already exists (as suggested by other comments) then you could use a trigger to maintain it for future updates/inserts, which could be as simple as:
create trigger trg_maintain_c
before insert or update on your_table
for each row
begin
:new.c := coalesce(:new.a, 0) + coalesce(:new.b, 0);
end;
/
and update existing values as a one-off exercise if needed.
db<>fiddle
But it would probably be simpler to drop it and re-add it as a virtual column, as it's less work to maintain and prevents the calculated column being directly updated to the wrong value. (Make sure you know what you're doing, and have any required permission/sign-off, before dropping an existing column, though...)
I think 2 ways to do that in order of what you have:
1 - You use a trigger when you add some data to A and B to automatically calculate C
2 - You create a query that calculate C given A and B
select c = a+b
from table
or something like that

Why isn't ALTER TABLE (adding a column) working in this SQL code?

Suppose I have a table (called numbers) like this
A B
1 3
4 4
5 5
I want to create a new column, C, which consists of values A / B. I.e. something that looks like:
A B C
1 3 0.33
4 4 1
5 5 1
I try this code but it doesn't work. Does anyone know why?
ALTER TABLE numbers ADD C VARCHAR(50) NOT NULL INSERT INTO numbers (C) VALUES (A / B)
It sounds like you want a generated column. The syntax varies, depending on the database, but it is often something like this:
ALTER TABLE numbers ADD C VARCHAR(50) GENERATED ALWAYS AS (A / B);
Why you are doing an arithmetic operation and then storing the value as a string is a mystery to me, but it is allowed.
Here is a db<>fiddle, which happens to use MySQL.

multiple unpivot and pivot on same column name

I am not sure how can I achieve this output. In this case, my column is one but the internal value is multiple but I need to merge all and create one unique column header. column "NAME" has multiple values
Please go through with the below problem.
https://dba.stackexchange.com/questions/261454/ignoring-null-values-in-columns-pivot-and-unpivot
Sounds like you would follow steps like:
create new table, split name into col, row, sort by row, col to create:
row col data
1 A Hello
1 B New
2 A World
2 B World
then pivot on col and your are done.
row A B
1 Hello New
2 World World
does that describe your problem?

SQL Command to copy data from 1 column in table and 1 column in another table into a new table?

I had to make a new table to get the Include statement working in Entity Framework since EF was looking for a table called be_PostTagbe_Posts. I was using EF Code First from DB. But now the question is about SQL. I added one row of data and now the include works. But what I am looking for is a SQL command that can copy data from 1 column in 1 table and 1 column in another into the new be_PostTagbe_Posts table. In the be_Posts table I need the data in PostRowID to go into be_Posts_PostRowID and PostTagId to go into be_PostTag_PostTagID. Both be_PostTag_PostTagID and be_Posts_PostRowID are in the new be_PostTagbe_Posts table. I am not very good with SQL so not sure how to do this.
Edit: Thanks for the answers. I tried 2 separate queries but only data was inserted into the the be_PostTag_PostTagID while be_PostTag_PostRowID remained null.
And I tried this query which returned The multi-part identifier "be_PostTag.PostID" could not be bound.
INSERT INTO be_PostTagbe_Posts(be_PostTag_PostTagID, be_Posts_PostRowID)
SELECT be_PostTag.PostTagID, be_Posts.PostRowID
WHERE be_PostTag.PostID = be_Posts.PostID
EDIT:
This only inserted half the data - even 2 inserts leave one column null
INSERT INTO be_PostTagbe_Posts (be_Posts_PostRowID)
SELECT PostRowID FROM be_Posts;
INSERT INTO be_PostTagbe_Posts (be_PostTag_PostTagID)
SELECT PostTagID FROM be_PostTag;
And yet management studio tells me the query executed successfully but one column is still null. Weird.
Here are screenshots of the tables:
SELECT PostTagID AS be_PostTag_PostTagID, PostRowID AS be_Posts_PostRowID
INTO be_PostTagbe_Posts
FROM be_PostTag
Inner JOIN be_Posts
ON be_PostTag.PostID=be_Posts.PostID
That command created the new table with the 2 columns populated.
If i understand you ,you want to Copy Table Z's Column A to Table X And Table Z's Column B to Table Y.
If it is so, According to your question it is not clear about Table Structure of TableX and TableY
Assuming TableX And TableY to single ColumnTable [Apart from IdentityColumn] our query will be
INSERT INTO TableX
SELECT ColumnA FROM TableZ
INSERT INTO TableY
SELECT ColumnB FROM TableZ
Rest put your Entire Structure of Table To Get More Help Because These query are on Assumptions
There's not enough information in your question to give you a working example, but this would be the general syntax for INSERTing into a different table using a query SELECTing from two other tables.
INSERT INTO destination_table(wanted_value_1, wanted_value_2)
SELECT table_1.source_field_1, table_2.source_field_1
WHERE table_1.matching_field = table_2.matching_field
There has to be some sort of relationship between the two tables for the WHERE clause to work in that statement. I'm guessing based the little information you provided that there is a PostRowID field somewhere in the table that contains the tags such that your data would look similar to this in the tag table:
PostRowID PostTagID
--------- ---------
1 1
1 2
1 3
1 4
2 1
2 2
3 3
4 4
It sounds like you should use two sql statements:
Insert into `be_PostTagbe_Posts` (`be_PostTag_PostTagID`)
select `PostTagID` from POSTTAGIDTABLE
and
Insert into `be_PostTagbe_Posts` (`be_Posts_PostRowID`)
select `PostRowID` from POSTTAGIDTABLE
unless the items have some sort of relationship, then if you have a select statement that will select the merged data in two columns you can just do
Insert into `be_PostTagbe_Posts` (`be_PostTag_PostTagID`,`be_Posts_PostRowID`)
(select statement that selects the two items)