dividing values of 2 columns - sql

How can I divide the values of 2 columns?
select column1, column2, (column1 / column2) as %ofSales ------
won't work for me.

Your query have not from clase
select column1, column2, (column1 / column2) as %ofSale
from my_table
and could be that the as column name with % should be wrapped in quotes
select column1, column2, (column1 / column2) as '%ofSale'
from my_table

You are probably getting a syntax error. Try with apostrophes like below
select column1, column2, (column1 / column2) as '%ofSales'
from yourTable

Related

Is is possible to use a CTE combined with a UDT to delete data?

I have a stored procedure that takes in a UDT. I would like to sanitize this data before inserting into the database to prevent any duplicate data from getting inserted. (Primary Key is still allowing duplicate data just the PK is different).
So I am using shorthand with a cte to get remove all of the possible duplicate rows and then use the clean data to insert into my table
The problem is I am getting an error that "#UDT_MyTable is READONLY and cannot be modified". Is there someway around this without going the temp table route?
Create Procedure InsertIntoMyTable
(
#UDT_MyTable dbo.MyUDT readonly
)
as
Begin
declare #TodaysDate datetime = CURRENT_TIMESTAMP
begin transaction
;with cte as
(
Select Row_Number() Over
(
PARTITION BY
Column1,
Column2,
Column3,
Column4,
Column5,
Column6
)
as sequence from
#UDT_MyTable
Errors Here
**Delete from cte Where sequence > 1**
insert into my table(
Column1,
Column2,
Column3,
Column4,
Column5,
Column6,
DateCreated
)
select
Column1,
Column2,
Column3,
Column4,
Column5,
Column6,
#TodaysDate
from cte

postgresql conditionnal insert values

I need a SQL which do following work.
INSERT INTO table_1 (column1, column2, column3) VALUES (:1,:2, :3)
This insert will only happen when another condition is TRUE, the
condition is:
SELECT 1 FROM table_2 WHERE column_time = 'time_stamp'
Table_1 and table_2 dont have any relation. the INSERT values to table_1 should only happen if the query on Table_2 return TRUE(1)
So i try to write my SQL as following
INSERT INTO table_1 (column1, column2, column3) VALUES (:1,:2, :3)
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING
However, this SQL not work as Postgresql looks dont like the INSERT combine with WHERE condition.
BTW: the values (:1, :2:, :3) are bind array values. the final SQL looks like this:
INSERT INTO table_1 (column1, column2, column3) VALUES ('ca_1','cb_1', 'cc_1') ('ca_2','cb_2', 'cc_2') ... ('ca_n','cb_n', 'cc_n') WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'my_time_stamp') ON CONFLICT DO NOTHING
Really need help thanks.
You need to select those values.
This would work in Postgres:
INSERT INTO table_1 (column1, column2, column3)
SELECT :1, :2, :3
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING
Alternatively:
INSERT INTO table_1 (column1, column2, column3)
SELECT x.*
FROM (VALUES (:1, :2, :3)) AS x(column1, column2, column3)
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING

INSERT INTO subtract 2 values

Is it possible for an INSERT query to subtract 2 values you have entered to create a 3rd value that can then be inserted into a table - if that makes sense...
e.g.
INSERT INTO table1 (column1, column2, column3)
VALUES ('50', '25', column1 - column2)
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', column1 - column2) from table1 where conditions)
This is a sample query! hope it helps!
Convoluted:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
(select 50 as column1,
25 as column2
) t
Since you can't reference other columns from the same SELECT clause, you have to do it as a subquery. I've also switched to using int literals rather than strings, because I can't make subtraction make sense in my head otherwise.
You could also do it using a Table Value Constructor:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
( VALUES (50, 25)
) AS t (column1, column2);
As indicated in my comment though, if the relationship should always hold, I'd build table1 as:
CREATE TABLE table1 (
column1 int not null,
column2 int not null,
column3 as column1 - column2
--More columns
)
Because that way, the column3 value is always correct.
You can create function that subtracts values and use this function in insert. This is the right way to do such things:
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', your_function() ) from table1 where conditions)
/
Using the "INSERT INTO" would do this:
INSERT INTO Table1Name (column1, column2, column3,)
(select 'X', 'Y', X - Y as Z)
Here is a link to SQL Authority with more examples of INSERT INTO
Another method would to be add a trigger to the table, where on insert of data, the third column would be updated with the difference of the first two columns.

Populating temporary table with result of independent Sql Query

I want to return a temporary table from stored procedure which populates with data retrieved from two independent sql query
Select column1,column2 FROM TABLE1 WHERE someCondition
Select column3,column4 FROM TABLE1 WHERE someOtherCondition
INSERT INTO Temp_table(column1,column2,column3,column4) values VALUE from those two table
Some of the result from table contains null as well.Also i am using some mathematical function like sum on some column as well
Thanks in advance
Try out with following code:
INSERT INTO Temp_table (column1, column2, column3, column4)
SELECT column1, column2, ISNULL(column3,0), ISNULL(column4,0) FROM TABLE1 WHERE someCondition
UNION ALL
SELECT ISNULL(column1,0), ISNULL(column2,0), column3, column4 FROM TABLE1 WHERE someOtherCondition
You want to do something like:
INSERT INTO Temp_table (column1, column2, column3, column4)
SELECT column1, column2, NULL AS column3, NULL AS column4 FROM TABLE1 WHERE someCondition
UNION
SELECT NULL AS column1, NULL AS column2, column3, column4 FROM TABLE1 WHERE someOtherCondition

T-SQL what does "<tablename> is not accessible in this context" mean? (SQL Code: 1001042)

Using Sybase IQ 12.5,
When I try to insert the result of a stored proc into a table like this:
INSERT mytable (column1, column2, column3)
SELECT column1, column2, column3
FROM (myproc('AAA'))
I get the following:
ASA Error -1001042: Table, 'mytable', is not accessible in this context.
The Sybase website has no further explanation
Can you post the definition of your stored procedure? The test below was successful for me, though I used Sybase SQL Anywhere (I tried versions 12.0.1 and 11.0.1). The Sybase IQ server is based on the SQL Anywhere execution engine so this should be an equivalent test, though I'm unsure what version of SQL Anywhere corresponds to IQ 12.5.
create table mytable (column1 int, column2 int, column3 int);
create procedure myproc( parm varchar(10))
result ( column1 int, column2 int, column3 int)
begin
select 1,2,3;
end;
INSERT mytable (column1, column2, column3)
SELECT column1, column2, column3
FROM (myproc('AAA'));
Full disclosure: I work for Sybase in SQL Anywhere engineering.
Problem was caused by my target table having a check constraint.
Here's an example to recreate the problem:
create table mytable (column1 int,
column2 int,
column3 int CHECK (column3 in (1,2,3)));
create procedure myproc( parm varchar(10))
result ( column1 int, column2 int, column3 int)
begin
select 1,2,3;
end;
INSERT mytable (column1, column2, column3)
SELECT column1, column2, column3
FROM (myproc('AAA'));