PostgreSQL: how to use NOT IN without WHERE? - sql

I have two queries:
select * from tableA
and
select a,b from tableA
group by a,b
the first query returns 2101 rows
the second query returns 2100 rows
I want to know which row is in the first but not in the second. It should be simple with NOT IN, but I can't find the correct syntax as NOT IN should be in WHERE statement. but I don't have a WHERE statement in my case.

There are N ways to do that and one of the simplest should be to find the rows that have a count > 1 when grouped on a,b.
select a,b from tableA
group by a,b
having count(*) > 1
Here is a sample:
with tableA as
(
select * from (values
(1,1,1),
(1,1,1),
(1,2,1)
) as t(a,b,c)
)
select a, b from tableA
group by a, b
having count(*) > 1;

You can get duplicates this way:
select a,b from tableA
group by a,b having count(1) > 1

Related

Update column as Duplicate

I have a table with three columns, A, B, and status.
first, I filter the table to get only duplicate value
using this query
SELECT A
FROM Table_1
GROUP BY A
HAVING COUNT(A) >1
the output :
In the second step, I need to check if column B has a duplicate value or not, if have duplicate I need to update the status as D.
I try this query
UPDATE Table_1
SET status = 'D'
WHERE exists
(SELECT B
FROM Table_1
GROUP BY B
HAVING COUNT(B) >1)
but it is updated all the rows.
The following does what you need using row_number to identify any group with a duplicate and an updateable CTE to check for any row that's part of a group with a duplicate:
with d as (
select *, row_number() over(partition by a,b order by a,b) dn
from t
)
update d set d.status='D'
where exists (select * from d d2 where d2.a=d.a and d2.b=d.b and d2.dn>1)
You can do this with an updatable CTE without any further joins by using a windowed COUNT
WITH d AS (
SELECT *,
cnt = COUNT(*) OVER (PARTITION BY a, b)
FROM t
)
UPDATE d
SET status = 'D'
WHERE cnt > 1;

how can I merge SUM and COUNT in select of SQL

I want to merge sum and count in SQL
select sum(A),B from TableA
group by B
having c>3
.
select Count(A),B from TableA
group by B
having c>3
result of select 1 and select 2 is more than one records
How can I have something like this on one select?
sum(A)*count(A)
Something like this?
SELECT SUM(A) as [Sum], COUNT(A) as [Count], SUM(A)*COUNT(A) as [Multiply],B
FROM TableA
GROUP BY B
HAVING C > 3

Keep Track of already summed tuples sql

If we have a table with values for a and b, is there a way to only add up the b's if its not a duplicate a? For example
a b
1 2
2 3
2 3
so we would get only 5 (instead of 8)
A sort of
select sum(b if unique a),
from table
where ...
The following query selects the lowest value of b for each group a
select min(b) min_b
from mytable
group by a
You can then sum those values by selecting the sum from a derived table
select sum(min_b) from (
select min(b) min_b
from mytable
group by a
) t
http://sqlfiddle.com/#!9/d82c5/1
You haven't specified your RDBMS, but if you are using a database which supporting window functions like SQL Server, you can query the unique rows first by using WITH clause and ROW_NUMBER() function and then get the SUM out of that.
;WITH C AS(
SELECT a, b,
ROW_NUMBER() OVER (PARTITION BY a ORDER BY a) AS Rn
FROM Table1
)
SELECT SUM(b) FROM C
WHERE Rn = 1
SQL Fiddle

how to SUM two columns in different table between two date

this my query but result false where number row different , that's to say whenever tableA select 2 row and tableB select 3 result is false
select sum(tableA.value)+sum(tableB.value1) )
from tableA,tableB
where tableA.data between '2016-01-21' and '2016-03-09'
and tableB.date2 between '2016-01-21' and '2016-03-09'
You need to do the sums in subqueries before joining. A simple rule: never use commas in the from clause.
select coalesce(avalue, 0) + coalesce(bvalue, 0)
from (select sum(a.value) as avalue
from tableA a
where a.data between '2016-01-21' and '2016-03-09'
) a cross join
(select sum(b.value) as bvalue
from tableB b
where b.data between '2016-01-21' and '2016-03-09'
) b;
OK . So here's what my understanding is.
You are trying to sum up two columns from two different tables and get the sum of the summed up columns. isn't ?? Correct me if I am wrong.If this is the case then
A Simple Subquery Can Come To Your Rescue.
Select
(Select SUM(value) From tableA
where data between '2016-01-21' and '2016-03-09') +
(Select SUM(value1) From tableB
where date2 between '2016-01-21' and '2016-03-09') FinalValue

How to combine two query's results into one row?

I have two queries that return one result each i.e one number
Select Count(*) as StockCountA from Table_A where dept='AAA'
Results
StockCountA
550
.
Select Count(*) as StockCountB from Table_B where dept='BBB'
Results
StockCountB
450
I wish to join the two results into one row record i.e
| StockCountA | StockCountB
| 550 | 450
You can use:
select
(Select Count(*) as StockCountA from Table_A where dept='AAA') as StockCountA,
(Select Count(*) as StockCountB from Table_B where dept='BBB') as StockCountB
Explanation: you can select single value as a field in a select statement, so you could write something like
select
x.*,
(select Value from Table_Y y) as ValueFromY
from
Table_X x
This will work only with scalar queries, meaning that the sub-query should have exactly 1 column, and at most 1 row. With 0 rows ValueFromY will return NULL and with more than 1 row, the query will fail.
An additional feature of select (in SQL Server, MySQL and probably others) is that you can select just values without specifying a table at all, like this:
Select
3.14 as MoreOrLessPI
You can combine both those facts to combine the two counts into a single result, by writing a query that looks like:
Select
(Select query that returns at most 1 row) as Result1,
(Select another query that returns at most 1 row) as Result2
This should give you the desired result:
SELECT * FROM(
(Select Count(*) as StockCountA from Table_A where dept='AAA') StockCountA ,
(Select Count(*) as StockCountB from Table_B where dept='BBB') StockCountB
);
While not always the best practice, it is possible to do a CROSS JOIN..
SELECT
COUNT(Table_A.SOME_COLUMN) as StockCountA
,COUNT(Table_B.SOME_COLUMN) as StockCountB
FROM Table_A, Table_B WHERE Table_A.dept='AAA' AND Table_B.dept='BBB'
Try below SQL :
select (Select Count(*) as StockCountA from Table_A where dept='AAA') StockCountA,
(Select Count(*) as StockCountB from Table_B where dept='BBB') StockCountB
Hope This Helps :)