how can I merge SUM and COUNT in select of SQL - 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

Related

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

PostgreSQL: how to use NOT IN without WHERE?

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

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 :)

SELECT on two other queries in Oracle

So, lets say I want to do something like:
SELECT Query1.a,
Query2.b
FROM (
SELECT q as a
FROM somewhere
),
(
SELECT g as b
FROM elsewhere
)
where Query 1 is
(
SELECT q as a
FROM somewhere
)
and Query2 is
(
SELECT g as b
FROM elsewhere
)
So, i want to select from two other select statements.
Query 1 produces a table
a
value1
Query 2 produces a table
b
value 2
And Query 3 (the outer select statement) produces
a b
value 1 value 2
So, essentially, two result tables are combined as columns and not as rows.
Thank you, if you have any hints.
You basically have your solution. You are only missing the names of your queries, so do like this:
SELECT Query1.a,
Query2.b
FROM (
SELECT q as a
FROM somewhere
) Query1,
(
SELECT g as b
FROM elsewhere
) Query2
It's not clear how you need to connect different rows from tables but it can be something like this:
select query1.a,
query2.b
FROM
(select q as a, ROW_NUMBER() OVER (ORDER BY q) as RN from a) Query1
FULL JOIN
(select q as b, ROW_NUMBER() OVER (ORDER BY q) as RN from b) Query2
ON Query1.RN=Query2.RN
SQLFiddle example
Your syntax is a bit off the SQL charts, but in essence ritgh:
It is possible to do a subquery:
select A.field from (select field from a_table) A;
It is essential that you name your query, if you want to use it in the select or where clauses.
And even possible to combine them like regular tables:
select A.field, B.other_field from (select field from table1) A, (select other_field from table2) B;
It is also possible to do al kind of where, grouping and sorting stuff on it, but not needed in your case.
I assume this is what you're looking for:
SELECT query1.a, query2.b
FROM
(SELECT q as a FROM somewhere) query1,
(SELECT g as b FROM elsewhere) query2
Here is a SQLFiddle to test the query

Counting occurrences of unique values in a column using sql

Is there a simple way for counting occurrences of unique values in a column using sql.
for e.g if my column is
a
a
b
a
b
c
d
d
a
Then the output should be
a 4
b 2
c 1
d 2
SELECT ColumnName, COUNT(*)
FROM TableName
GROUP BY ColumnName
Use GROUP BY and COUNT
SELECT column, COUNT(*)
FROM table
GROUP BY column
After searching and giving some good tought here's the correct query :
SELECT SUM(uniqueValues)
FROM (
SELECT COUNT(DISTINCT values) as uniqueValues
FROM tablename GROUP BY values)