SQL Query for calculating total without considering repeated rows - sql

I have a table in which i need to calculate total amount.But there are some rows which are getting repeated and i need to avoid those rows.
eg
Column1 Column2
1 10
2 20
3 30
1 10
2 20
3 30
The problem is i do not know how many times will the values be repeated.
How is it possible?

Use distinct clause in the inner query to avoid duplicate value
and then subquery to get total of column2
Like below -
select sum(column2) as totalamount
from
(
select distinct column1, column2
from tablename
) a

This would do
Select column1,avg(column2)
from tablename
group by column1

You can try:
SELECT
s.column1,
sum(s.column2)
FROM
(
SELECT DISTINCT
INNER.column1,
INNER.coulmn2
FROM <table> INNER
) s
GROUP BY s.column1;
This subquery should help you..

Related

How to display records in SQL Server that have same value in Column 2 but value in Column 1 should not be present in any other record?

I have a below table like this -
Policy Column1 Column2
A 4 100
B 4 100
C 3 100
D 3 100
E 2 100
F 5 100
The Output should be
Policy Column1 Column 2
E 2 100
F 5 100
Can someone please guide me.
Assuming you're after rows where the value in column1 is unique (i.e. no other row has that same value in column 1)
select max(Policy) Policy -- if we're just getting 1 row, the max is the only value
, Column1
, max(Column2) Column2 -- as above
from myTable
group by Column1 -- group by column 1 then
having count(1) = 1 -- count how many rows are in that group; if it's unique we get 1
Select * from policies
where Column1 in (select Column1
from policies
group by Column1
having count(*)=1);
DBFiddle demo is here
You want to divide the data into chunk via partition by. Then you can determine which of them has only a single row.
with data as (
select *, count(*) over (partition by Column1, Column2) as cnt
from T
)
select Policy, Column1, Column2 from data where cnt = 1;
This kind of query was harder to write twenty years ago. If you're learning SQL I'd encourage you to get a handle on some of the foundational concepts before diving into table expressions and analytic functions.

Query to show how many distinct records on column B for each record on Column A using MS Access

I am having a hard time to write the SQL query to do
something like, how many distinct record on column B for each record on Column A
Let's say:
Column A,,,,,,,,,,,,,,,,, Column B
A 1
A 1 would return 3 for A (3 distinct records 1,2,3)
A 2
A 3
B 4 Would return 2 for B (4,5)
B 4
B 5
C 6 Would return 1 for C
C 6
it will be imported to a datagridview to show the result
Column 1---------------Column 2
A 3
B 2
C 1
column 2 being the result of the query
I don't know if it is easy and I don't get it or complicated but I can't figure out.
I have tried Group By, Distinct etc... I get either not what I want or error like no value given....
Thank you.
After some research I found this query that seems to return the results expected.
SELECT ColA, COUNT(COLB) FROM (SELECT ColA, ColB FROM Table Where blablabla GROUP BY ColA,ColB) GROUP BY ColA"
You could try something like:
SELECT ColA, Count(ColB)
FROM MyTable (NOLOCK)
GROUPBY ColA
The sql query is select colA, colB, COUNT(DISTINCT colB) from t1 group by colA
You can access in SQL fiddle.
http://sqlfiddle.com/#!9/2c84d8/7
Actually, if what you're looking to partition the counts try: over (Partition by)
Partition Function COUNT() OVER possible using DISTINCT

SQL Server : how to find ids where columns have different values

I have a table like this:
Column1 Column2
---------------
1 1
1 2
1 3
1 4
2 1
2 1
2 1
2 1
In column1 one there are 2 different ids, in column2 there are different values for each id from column1.
How can I get the id from column1 where not all ids from column2 are the same? So in this instance the output should be 1 - because they have all different values in column2, where id from column1 has all 1's in column2
Just use group by and having:
select column1
from table t
group by column1
having min(column2) <> max(column2);
Note: you could also use count(distinct), but that has more overhead than min() and max().
Similar logic can be used if the second column could be NULL. That doesn't appear in the sample data so it doesn't seem worth including it in the logic unless the OP specifically says this is a possibility.
Try like this:
select Column1
from yourTable
group by Column1
having count(DISTINCT column2) > 1;
I would think something like this should do the job:
SELECT t.column1 FROM table t
GROUP BY t.column1
HAVING COUNT(DISTINCT t.column2) > 1
This approach will handle the case where a null is an acceptable value in column2.
select column1
from
(
select distinct column1, column2
from yourTable
) t
group by column1
having count(*) > 1

SQL Server Sum of column1 of all distinct values of column2

I am trying to find the SUM of column1 of all Distinct values of column2. Is it possible?
You could try something like this:
Select SUM(ColA), ColB
from table
Group by ColB
You almost wrote the query yourself in that sentence:
SELECT Column2, SUM(Column1) FROM Table GROUP BY Column2
It's not entirely clear what you're asking...
...this adds all the values in column 1 for each distinct value in column 2 then gives a total of all values in column 1:
SELECT Column2,SUM(Column1) FROM Table GROUP BY Column2 with rollup
Note: If you want the rollup at the top of the output put distinct in it.
SELECT distinct Column2,SUM(Column1) FROM Table GROUP BY Column2 with rollup

Count on multiple fields

How do I get an output from table of fields A, B, C to display
A | Count(A)
where the number of rows = distinct instances of A and the sum of the values for count(A) equals total number of rows in the database?
SELECT A, COUNT(1) FROM your_table_name GROUP BY A;
You can use the DISTINCT keyword inside the COUNT
Like this:
COUNT(DISTINCT A)
Like AI mentioned a dataset and the final outcome would be helpful. From the small example you gave, i think this might be what you are looking for
SELECT Field_A, Count(*) FROM table_name GROUP BY Field_A
UNION ALL
SELECT Field_B, Count(*) FROM table_name GROUP BY Field_B
UNION ALL
SELECT Field_C, Count(*) FROM table_name GROUP BY Field_C
The output for that would be something similiar to depending on how many unique values you have in Field_A, Field_B, Field_C
Value_In_A1 - 10
Value_In_A2 - 11
Value_In_A3 - 15
Value_In_A4 - 8
Value_In_B1 - 9
Value_In_C1 - 25
Value_In_C2 - 2