Compare sums of two columns in sql - sql

I have two tables with a column named no_id, I want to test that the values in both columns on the tables are the same.
So I´m trying to sum the values of both columns and compare the result.
SELECT
CASE
WHEN SUM (cast(a.no_id as bigint)) = SUM(cast(b.no_id as bigint)) THEN 'YES'
ELSE 'NO'
END as no_id
FROM table_a as a
,table_b as b
The result of the query is NO, but when I select each sum:
SELECT
SUM (cast(a.no_id as bigint)),
SUM(cast(b.no_id as bigint))
FROM table_a as a
,table_b as b
I got two nulls, one in each column. Instead of the sums of the columns.
I have to do this with other twenty columns of both tables.
no_id is a varchar(16) in both tables.
------UPDATE------
no_id only contains numeric strings,
I did the next query to ensure that null would be treated as 0:
SELECT
SUM(cast(ISNULL(a.no_id,0) as bigint)),
SUM(cast(ISNULL(b.no_id,0) as bigint))
FROM table_a as a
,table_b as b
But I keep getting the same result.
If I select the result from just one table, it works, I get the result of the sum:
SELECT
SUM(cast(ISNULL(a.no_id,0) as bigint))
FROM table_a as a
Then, why it doesn't work with both tables?

As I said in the comments:
Don't use the comma(,) operator in the FROM clause, it's been obsolete for over twenty years, use the JOIN keyword syntax instead. Because if you did you would know that ...
You are CROSS JOIN-ing your tables which is very bad and logically wrong 99% of the time. You need to use column subqueries instead.
Like this:
SELECT
CASE
WHEN (SELECT SUM(cast(no_id as bigint)) FROM table_a)
= (SELECT SUM(cast(no_id as bigint)) FROM table_b) THEN 'YES'
ELSE 'NO'
END as no_id

The problem is you don't say how table_a and table_b are related, so every row in table_a is paired with every row in table_b if either table has even one null value, then the total sum will be null.
If you just want the SUMS in both tables to match, then I;m not sure SUM is the best indicator. If table A had the values 1 and 4, and table b had 2 and 3, then the SUM would match but obviously the values are different.

Related

Merge two different tables with Access

I’d would like to merge two different tables with similar and different columns. The only different columns are : Amount-F21 and Amount A-21. My issue is when I write the SQL request (UNION ALL) with Access, it deletes the column Amount A-21 but I need this one though. Thanks.
SELECT * FROM [Source Alloc-A21]
UNION ALL
SELECT * FROM [Source Alloc-F21]
To use the star notation Table.* with UNION, the columns in both tables must be equal. If they are not, you need to select individual columns and provide default values for the columns that are missing for both tables.
For example:
SELECT TableA.A, TableA.B, TableA.[Amount-F21], 0 AS [Amount-A21]
FROM TableA
UNION ALL
SELECT TableB.A, TableB.B, 0 AS [Amount-F21], TableB.[Amount-A21]
FROM TableB
This will report 0 for any of the missing columns (Amount-F21 or Amount A-21).
You can then sum the results to hide the zero (default) values.
SELECT T.A, T.B, SUM(T.[Amount-F21]) AS [Amount-F21], SUM(T.[Amount-A21]) AS [Amount-A21]
FROM (
SELECT TableA.A, TableA.B, TableA.[Amount-F21], 0 AS [Amount-A21]
FROM TableA
UNION ALL
SELECT TableB.A, TableB.B, 0 AS [Amount-F21], TableB.[Amount-A21]
FROM TableB
) AS T
GROUP BY T.A, T.B

Combining separate queries from the same table as separate columns

I have a query that I am trying to combine but with different columns. The specifics of this are:
Same table
Different Where clause
Oracle DB
Ran in DBVisualizer
This is what I was trying to do: it runs but it does not join the two columns. they are being outputted into separate Result tabs in DBVisualizer
Select count (distinct CODE) AS Comp_PCT
from cons.GM
Where POLICY='NR'
And PCT is null
UNION
Select count (distinct CODE) AS Comp_DTY
from cons.GM
Where POLICY='NR'
And DTY is null
I don't know that this answer is a correct form:
SELECT
(Select count (distinct CODE)
from cons.GM
Where POLICY='NR'
And PCT is null) AS Comp_PCT,
(Select count (distinct CODE)
from cons.GM
Where POLICY='NR'
And DTY is null) AS Comp_DTY

Oracle SQL Developer - Count function

This is the output of a select * from table1, I have a doubt with count function... I want to count that NULL, in order to do that the proper option is to do this:
select count(*) from table1 where fecha_devolucion is null --> This gives me the proper answer counting 1 however if i do:
select count(fecha_devolucion)
from table1
where fecha_devolucion is null --> this returns 0, why? Isn't the same syntax?
What's the difference between choosing a specific field and * from a table?
From the documentation (http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions032.htm):
If you specify expr, then COUNT returns the number of rows where expr
is not null. ...
If you specify the asterisk (*), then this function returns all rows...
In other words, COUNT(fecha_devolucion) counts non-NULL values of that column. COUNT(*) counts the total number of rows, regardless of the values.
This is the another way how you can get the count :
SELECT SUM(NVL(fecha_devolucion,1)) FROM table1 WHERE fecha_devolucion IS NULL;
Let's compare the two queries:
select count(*)
from table1
where fecha_devolucion is null;
select count(fecha_devolucion)
from table1
where fecha_devolucion is null;
I think you misunderstand the count() function. This function counts the number of non-NULL values in its argument list. With a constant or *, it counts all rows.
So, the first counts all the matching rows. The second counts all the non-NULL values of fecha_devolucion. But there are no such values because of the where clause.
By the way, you can also do:
select sum(case fecha_devolucion is null then 1 else 0 end) as Nullfecha_devolucion
from table1;

SQL query - Selecting distinct values from a table

I have a table in which i have multiple entries against a FK. I want to find out the value of FK which do not have certain entries e.g
my table has following entries.
PK----------------FK-----------------Column entries
1----------------100-----------------ab1
2----------------100-----------------ab2
3----------------100-----------------ab4
4----------------200-----------------ab1
5----------------200-----------------ab2
6----------------200-----------------ab3
7----------------300-----------------ab1
8----------------300-----------------ab2
9----------------300-----------------ab3
10---------------300-----------------ab4
Now, from this table i want to filter all those FK which do not have ab3 or ab4 in them. Certainly, i expect distinct values i.e. in this case result would be FK= 100 and 200.
The query which i am using is
select distinct(FK)
from table1
where column_entries != 'ab3'
or column_entries != 'ab4';
Certainly, this query is not fetching the desired result.
try the following :-
select distinct fk_col from table1
minus
(select distinct fk_col from table1 where col_entry='ab3'
intersect
select distinct fk_col from table1 where col_entry='ab4')
This will show all those FKs which do not have 'ab3' and 'ab4'. i.e. 100 and 200 in your case
The below script may be the solution if I got your question in a right way.
SELECT DISTINCT(TableForeignKey)
FROM Test
WHERE TableForeignKey NOT IN (
SELECT T1.TableForeignKey
FROM Test T1 INNER JOIN Test T2 ON T1.TableForeignKey = T2.TableForeignKey
WHERE T1.TableEntry = 'ab3' AND T2.TableEntry = 'ab4')
SQLFiddle Demo
You could use GROUP BY with conditional aggregation in HAVING:
SELECT FK
FROM table1
GROUP BY FK
HAVING COUNT(CASE column_entries WHEN 'ab3' THEN 1 END) = 0
OR COUNT(CASE column_entries WHEN 'ab4' THEN 1 END) = 0
;
The two conditional aggregates count 'ab3' and 'ab4' entries separately. If both end up with results greater than 0, then the corresponding FK has both 'ab3' and 'ab4' and is thus not returned. If at least one of the counts evaluates to 0, then FK is considered satisfying the requirements.

Find Rows where the Same Two Column Values Recur

Given a table in SQL-Server like:
Id INTEGER
A VARCHAR(50)
B VARCHAR(50)
-- Some other columns
with no index on A or B, I wish to find rows where a unique combination of A and B occurs more than once.
I'm using the query
SELECT A+B, Count(A+B) FROM MyTable
GROUP BY A+B
HAVING COUNT(A+B) > 1
First Question
Is there a more time-efficient way to do this? (I cannot add indices to the database)
Second Question
When I attempt to gain some formatting of the output by including a , in the concatenation:
SELECT A+','+B, Count(A+','+B) FROM MyTable
GROUP BY A+','+B
HAVING COUNT(A+','+B) > 1
The query fails with the error
Column 'MyDB.dbo.MyTable.A' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
with a similar error for Column B.
How can I format the output to separate the two columns?
It would seem more natural to me to write:
SELECT A, B, Count(*) FROM MyTable
GROUP BY A, B
HAVING COUNT(*) > 1
And it's the most efficient way of doing it (and so is the query in the question).
Similarly to the above query, you can rewrite your second query:
SELECT A + ',' + B, Count(*) FROM MyTable
GROUP BY A, B
HAVING COUNT(*) > 1