How to multiply values of the columns based on the value in one column.
Example
Col1 Col2 Col3 Col4
10 10 10 casea (multiply col1 * col2)
20 20 20 caseb (multiply col1 * col3)
30 30 30 casec (multiply col2 * col3)
A query like:
select col1, col2, col3, col4, total
would return
10,10,10,casea,100
20,20,20,caseb,400
30,30,30,casec,900
Of course performance is an important issue as always.
Thanks for participating.
select
Col1,
Col2,
Col3,
Col4,
CASE Col4
WHEN 'casea' then col1*col2
WHEN 'caseb' then col1*col3
WHEN 'casec' then col2*col3
END AS Total
FROM YourTable
Related
I have following table in PostgreSQL 11.0
col1 col2 col3 col4 col5 col6
98262898184 NCT01243502 0.01% ct327 (or placebo) false placebo term (null)
98262898184 NCT01243502 0.01% ct327 (or placebo) false no mapping (null)
98262898185 NCT01268527 0.01% e6201 false no mapping (null)
98262898165 NCT01654484 0.0015% tafluprost true (null) (null)
98263196888 NCT02925793 vehicle cream false vehicle term (null)
98263196888 NCT02925793 vehicle cream false no mapping (null)
I would like select those rows where for a common values of col1, col2, col3 and col5 value is placebo term, vehicle term and excluding is counter row.
The desired table is:
col1 col2 col3 col4 col5 col6
98262898184 NCT01243502 0.01% ct327 (or placebo) false placebo term (null)
98262898185 NCT01268527 0.01% e6201 false no mapping (null)
98262898165 NCT01654484 0.0015% tafluprost true (null) (null)
98263196888 NCT02925793 vehicle cream false vehicle term (null)
I have no idea at the moment how to get this desired output. Any help is highly appreciated.
What I understood from your question is you want distinct rows from your table based on col1, col2, col3 and prefer the row having col5 value as placebo term or vehicle term.
Try this:
with cte as (
select * ,
case when col5='placebo term' or col5='vehicle term'
then 1 else 0
end as "flag_"
from my_table
)
select distinct on (col1,col2,col3)
col1, col2, col3 col4, col5, col6
from cte
order by col1, col2, col3, flag_ desc
DEMO
I have following table in postgres 11. I am joining following two tables using 'union'
Table1
col1 col2 col3 col4
4894775 NCT00000172 drug galantamine
4894772 NCT00000174 drug rivastigmine
4895618 NCT00000241 drug flupenthixol
Table2
col1 col2 col3 col4 col5 col6
4894775 NCT00000172 drug galantamine galantamine {N06DA}
4895618 NCT00000241 drug flupenthixol flupenthixol {N05AF}
I would like to have the two tables joined such that whenever col5 and col6 are not null then pick those rows else pick the null rows.
I have tried following query so far:
select distinct on (col1, col2, col3, col4, col5)
col1,
col2,
col3,
col4,
coalesce(b.col5, a.col5),
coalesce(b.col6, a.col6),
from (
SELECT
col1,
col2,
col3,
col4,
null as col5,
null as col6
from table1 a
union
select col1,
col2,
col3,
col4,
col5,
col6
from table2 b
order by col1
The desired output is:
col1 col2 col3 col4 col5 col6
4894775 NCT00000172 drug galantamine galantamine {N06DA}
4894772 NCT00000174 drug rivastigmine (null) (null)
4895618 NCT00000241 drug flupenthixol flupenthixol {N05AF}
It looks like you want a left join:
select col1, col2, col3, col4, t2.col5, t2.col6
from table1 t1
left join table2 t2 using(col1, col2, col3, col4)
If there may be "missing" rows in both tables, then you can just change the left join to a full join.
How can I Union two DB2 tables with the same structure except four additional columns in one of the tables?
I have gone through other similar questions and tried the following options. None of them worked. I am working on DB2.
1)
Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, Null as Col4, Null as Col5 from Table2
SQL Error [42703]: NULL IS NOT VALID IN THE CONTEXT WHERE IT IS USED. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.62.56
2)
Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, '' as Col4, '' as Col5 from Table2
SQL Error [42825]: THE CORRESPONDING COLUMNS, 17, OF THE OPERANDS OF A SET OPERATOR ARE NOT COMPATIBLE. SQLCODE=-415, SQLSTATE=42825, DRIVER=3.62.56
Please advice.
Try
cast(NULL as varchar(20)) as Col4
Instead of varchar(20) use whatever type the column has in the other table
I am trying to make an IF after a WITH and it is giving me the error Incorrect syntax near the keyword 'IF'
below an example of the stored procedure I am writing
CREATE PROCEDURE [dbo].[Proc_MyProc]
#MODE INT = null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
WITH ss as (
select col1, col2, col3, col4, col5
from TableTest
)
IF #MODE = 1
select col1 as A, col2, sum(col5) as col5sum
from ss
group by col1, col2
order by col5sum desc
ELSE IF #MODE = 2
select col1, col2, sum(col5) as col5sum
from ss
group by col1, col2, col3
order by col5sum desc
ELSE IF #MODE = 3
select col1, col2, sum(col5) as col5sum
from ss
group by col1, col2, col4
order by col5sum desc
END
GO
I tried to remove the WITH and the syntax error gone, but off course it is not a solution
Thanks :)
The WITH goes with the SELECT, so you need to repeat the WITH or use a temporary table.
In this case, though, the WITH really has no value -- let me assume that it is simplified.
The first method would be:
if #MODE = 1
with ss as (
select col1, col2, col3, col4, col5
from TableTest
)
select col1 as A, col2, sum(col5) as col3sum
from #ss
group by col1, col2
having SnQuantity > 0
order by availability desc;
. . .
The second method would be:
select col1, col2, col3, col4, col5
into #ss
from TableTest;
if #MODE = 1
select col1 as A, col2, sum(col5) as col3sum
from #ss
group by col1, col2
having SnQuantity > 0
order by availability desc;
. . .
I'm trying to get the output of queries within the with clause of my final query as csv or some sort of text files. I only have query access, I'm not allowed to create tables for this database. I have a set of queries that do some calculations on a data set, another set of queries that compute on the previous set and yet another that calculates on the final set. I don't want to run all of it as three seperate queries because the results from the first two are actually in the last one.
WITH
Q1 AS(
SELECT col1, col2, col3, col4, col5, col6, col7
FROM table1
),
Q2 AS(
SELECT AVG(col1) as col1Avg, MAX(col1) as col1Max, col2, col3,col4
FROm Q1
GROUP BY col2, col3, col4
)
SELECT
AVG(col1AVG), col3
FROM
Q2
GROUP BY col3
I would like the results from Q1, Q2 and the final select statement as preferably 3 csv files but I could live with all of it in one csv file. Is this possible?
Thanks!
Edit: Just to clarify, the columns from the queries are very different. I'm definitely pulling more columns from my first query than my second. I've edited the above code a bit to make this more clear.
To combine all the results together you'd use UNION ALL, but the number and data types of the columns must match.
select col1, col2, col2
from blah
union all
select col1, col2, col2
from blah2
union all
... etc
You can reference CTE's in there of course ...
with
cte_1 as (
select ... from ...),
cte_2 as (
select ... from ... cte_1),
cte_3 as (
select ... from ... cte_2)
select col1, col2, col2
from cte_1
union all
select col1, col2, col2
from cte_2
union all
select col1, col2, col2
from cte_3
If your final output is a csv then it looks like you have multiple row formats in there -- checksums? If so, in the queries that you union all together you might like to combine all the columns from each query into one string ...
with
cte_1 as (
select ... from ...),
cte_2 as (
select ... from ... cte_1),
cte_3 as (
select ... from ... cte_2)
select col1||','||col2||','||col2
from cte_1
union all
select col1||','||col2
from cte_2
union all
select col1
from cte_3