SQL counting values and selecting Column Name as result - sql

I am new to SQL and a little embarrassed to ask this.
I have A table that contains 2 columns A and B
A B
0 2
1 3
3 1
I want a query that will return
Category | Sum
A 4
B 6
What is the best way to write this query?

select 'A', sum(A) from table
union
select 'B', sum(B) from table

Or this...
SELECT SUM(A) A
, SUM(B) B
FROM #MyTable

If UNPIVOT is supported by the SQL product you are using:
SELECT Category, SUM(Value) AS Sum
FROM atable
UNPIVOT (Value FOR Category IN (A, B)) u
GROUP BY Category
;
In particular, the above syntax works in Oracle and SQL Server.

Related

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

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

Which select is faster in Sqlite?

a is a table index, b is a normal column.
select a,b from ( select a,b from table where a in (*listA*) ) where b in (*listB*)
or
select a,b from table where (a=listA[0] and b=listB[0]) or (a=listA[1] and b=listB[1])...
I am using pseudocode to represent a list declaration.
The first query is wrong because it never looks at the combinations of a and b.
To use a temp table, you have to join with it:
SELECT a, b
FROM MyTable
JOIN (SELECT 1 AS a, 2 AS b UNION ALL
SELECT 3, 4 UNION ALL
SELECT 5, 6 ...)
USING (a, b)
Which version is better optimized depends on too many factors; the only way to find out is to measure with representative data.

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)

get subset of a table in SQL

I want to get a subset of a table, here's the example:
1 A
2 A
3 B
4 B
5 C
6 D
7 D
8 D
I want to get the unique record, but with the smallest id:
1 A
3 B
5 C
6 D
How can I write the SQL in SQL Server? Thanks!
Use a common-table expression like this:
;WITH DataCTE AS
(
SELECT ID, OtherCol,
ROW_NUM() OVER(PARTITION BY OtherCol ORDER BY ID) 'RowNum'
FROM dbo.YourTable
)
SELECT *
FROM DataCTE
WHERE RowNum = 1
This "partitions" your data by the second column you have (A, B, C) and orders by the ID (1, 2, 3) - smallest ID first.
Therefore, for each "partition" (i.e. each value of your second column), the entry with RowNum = 1 is the one with the smallest ID for each value of the second column.
select min(id), othercol
from thetable
group by othercol
and maybe with
order by othercol
... at the end if thats important
Try this:
SELECT MIN(Id) AS Id, Name
FROM MyTable
GROUP BY Name
select min(id), column2
from table
group by column2
It helps if you provide the table information in the question - I've just guessed at the column names...