Ive come across a problem when trying to add together two column sums.
Ive created a view with all the correct data in but when i try to execute a query like:
Select ID, Sum(ColumnA),
Sum(ColumnB)
Sum(ColumnA) + Sum(ColumnB) AS ColumnC
From View1
Group by ID
The ColumnC figure is only correct when there is data in both columns, if there is only data in ColumnB then it displays that but if there is only data in ColumnA then it doesnt.
Sometime when there isnt any data in ColumnA or B it will be NULL, so maybe this is the problem.
Hope there is a way around this.
Cheers
Try using ISNULL to substitute zeros for NULLs:
Select ID, Sum(ColumnA),
Sum(ColumnB)
ISNULL(Sum(ColumnA),0) + ISNULL(Sum(ColumnB),0) AS ColumnC
From View1
Group by ID
Adding something to a null value gives a null result, the null is not converted to zero. You have to do that conversion explicitly:
Select ID, Sum(ColumnA),
Sum(ColumnB)
isnull(Sum(ColumnA), 0) + isnull(Sum(ColumnB), 0) AS ColumnC
From View1
Group by ID
You can use COALESCE to replace NULL inputs to the calculation with zero as below.
COALESCE(Sum(ColumnA),0) + COALESCE(Sum(ColumnB),0) + AS ColumnC
Or ISNULL as in the other 2 answers. Doesn't matter which if portability is not a concern.
Related
I would like to use a SQL select statement that has the condition 'where column A is NULL change column B values to be equal to column C values'. How would I be able to incorporate this logic into a SELECT statement (Not an UPDATE statement as I cant change the tables on the server but want to query them from the server).
SELECT final.*
FROM final
The actual table is in the image below, here I want to change column Old to match column DirectUse if the Change column is null.
Try Case statement:
SELECT
Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
CASE: https://www.w3schools.com/sql/sql_case.asp
Can also be incorporated by UNION ALL:
SELECT Old
FROM final where Change is not null
UNION ALL
SELECT DirectUse
FROM final where Change is null
Use a CASE expression:
SELECT Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
I think you basically you want:
SELECT
ColumnA
, CASE WHEN ColumnA IS NULL THEN ColumnC ELSE ColumnB END AS ColumnB
, ColumnC
, <any other columns>
FROM Final
I'm new in the db world and I'm trying to do my first queries.
I have two tables and I need compare the value of the columnA, in the first table, with the value of the columnB in the second table.
I would like to know what values in the columnA are in the columnB. Both columns are varchar type.
I've tried this two queries:
select *
from tableA
join tableB
on (tableA.columnA = tableB.columnB);
select *
from tableA
where columnA in (select columnB
from tableB);
But both get me back empty table.
I checked the values manually, and there are many equal values.
Maybe the = isn't the right operator with the string values?
This is a simple example of what I would do, with the expected result at the end.
TableA
columnA descriptionA
EF8236PA xyx
EF7843DV dgfd
EF6535MD dshr
EF3274LK hghg
EF6940BN fdtsg
EF3405TJ dsbfbs
TableB
columnB
EF3405TJ
EF6940BN
EF6535MD
Result:
EF3405TJ
EF6940BN
EF6535MD
Both of your queries are looking fine. you can add few more conditions in the where clause that will make it work.
Example:
select *
from tableA
join tableB
on (upper(trim(tableA.columnA)) = upper(trim(tableB.columnB)));
Trim will cut down extra spaces and upper will make the search case insensitive.
Hope this will help.
I would like to know what values in the columnA are in the columnB.
Strictly speaking, this query is a correct (and fast) query to implement what you ask:
SELECT DISTINCT columnA
FROM tableA a
WHERE EXISTS (SELECT 1 FROM tableB WHERE columnB = a.columnA);
The manual about EXISTS.
But neither of your queries should return empty sets. There may be whitespace or other invisible characters fooling you. Test with:
SELECT * FROM tableA WHERE columnA = 'EF6940BN';
SELECT * FROM tableB WHERE columnB = 'EF6940BN';
I tried all your suggestions, but no one works.
So I tried run the code on another postgre and the same code that I wrote above works.
I don't understand why, the postgre's versions are the same
I need to use case when to generate the value range for in function (in DB2).
for example, in below code, I want the columnB in (5,6)
select columnA from tableName where columnB in (
(case
when #variable=1 then '4' // specific number
when #variable=2 then '5' //specific number
when #variable=3 then '7,10' // a value range
end)
)
but tried several times and other similar solutions, never got the expected result
how to do this?
Firstly, In function does not read multiple values inside Case statement. The comma must be after every single value in the range.
Second, you can mention a valid condition in your Question, rather than just 1=1. It's always true so, doesn't make sense.
Example:
1) output of below query gives in (5, 6)
select columnA from tableName where columnB in ((case when #variable=1 then 5 end), 6);
2) this gives only records of columnB = 5, let say the second condition is false.
select columnA from tableName where columnB in ((case when #variable=1 then 5 end), (case when #variable=2 then 6 end));
try Something like this
select columnA from tableName
where columnB in (
select * from table(values 4) tmp(NewCol)
where #variable=1
union all
select * from table(values 5) tmp(NewCol)
where #variable=2
union all
select * from table(values 7, 10) tmp(NewCol)
where #variable=3
)
You cannot have string as value range unless you convert it into rowset. I'm not sure how to do this in DB2, but I have something that should work, since according to documentation, DB2 does have unnest(). There are of course other ways to create rowsets.
SELECT columnA
FROM unnest(array[2,6,8,10], array[7,5,6,28]) --create "temp table" for example purposes
WITH ORDINALITY AS a(columnA, columnB) --alias columns from temp table
WHERE
CASE WHEN true THEN --switch true to some other condition
columnB IN(SELECT * FROM unnest(array[5,6])) --unnest(array[]) will create rowset with 2 rows, each having one column holding integer value
END;
You might need to drop alias from AS a(columnA, columnB) since I'm not sure if it works in DB2 and I have not found live DB2 tester (it is required in PostgreSQL where I tested query).
I am wondering how i can display data from column B if Column A is null. The reason is if we get a product from one of our manufactures it is put in a different column. However, when i go to build the report the two columns essentially the same thing and it is throwing the graph way off. Any help would be appreciated.
Something like this maybe?
Case When column A isnull then column B?
Use ISNULL() or COALESCE(), or CASE
SELECT ISNULL(ColumnA, ColumnB) AS [YourColumn]
FROM FOO
OR
SELECT COALESCE(ColumnA, ColumnB) AS [YourColumn]
FROM FOO
OR
SELECT CASE WHEN ColumnA IS NULL THEN
ColumnB
ELSE
ColumnA
END AS [YourColumn]
FROM FOO
Say I have a table that includes column A, column B and column C. How do I write I query that selects all rows where either column A OR column B OR column C equals a certain value? Thanks.
Update: I think forgot to mention my confusion. Say there is another column (column 1) and I need to select based on the following logic:
...where Column1 = '..' AND (ColumnA='..' OR ColumnB='..' OR ColumnC='..')
Is it valid to group statements as I did above with parenthesis to get the desired logic?
Unless I'm missing something here...
SELECT * FROM MYTABLE WHERE COLUMNA=MyValue OR COLUMNB=MyValue OR COLUMNC=MyValue
I prefer this way as its neater
select *
from mytable
where
myvalue in (ColumnA, ColumnB, ColumnC)
SELECT *
FROM myTable
WHERE (Column1 = MyOtherValue) AND
((ColumnA = MyValue) OR (ColumnB = MyValue) OR (ColumnC = MyValue))
Yes, it's valid to use parentheses. However, if you're searching multiple columns for the same value, you may want to consider normalizing the database.