SQLite query Total - sql

I've got issue how to make a query total SUM from a table when need multiple 2 columns and then calculate a total sum of all rows.
enter image description here

select column1,sum(column2*column3)as col from yourtable
group by column1
union all
select '' column1,sum(column2*column3)as col from yourtable
order by column1

It is just a simple sum over Quantity*Price:
SELECT SUM(Quantity*Price) AS result FROM tablename
You don't seem to need any grouping.

Related

i want to get the sum of one or two column in one select query

please help me to query
i want to sum one column in the table between entry date
example:
SELECT * FROM `transaction` WHERE entryDate BETWEEN '2019-11-05' AND '2019-11-31'
The simplest way to do this for just a single column is to just take the sum:
SELECT SUM(some_col)
FROM `transaction`
WHERE entryDate BETWEEN '2019-11-05' AND '2019-11-31';
If you wanted to take a number of sums, each with different criteria, then use conditional aggregation:
SELECT
SUM(CASE WHEN entryDate BETWEEN '2019-11-05' AND '2019-11-31'
THEN some_col END) AS some_col_sum,
-- other conditional sums here
FROM `transaction`;
SELECT ID, col1+ col2
FROM TableName

COUNT() doesn't work with GROUP BY?

SELECT COUNT(*) FROM table GROUP BY column
I get the total number of rows from table, not the number of rows after GROUP BY. Why?
Because that is how group by works. It returns one row for each identified group of rows in the source data. In this case, it will give the count for each of those groups.
To get what you want:
select count(distinct column)
from table;
EDIT:
As a slight note, if column can be NULL, then the real equivalent is:
select (count(distinct column) +
max(case when column is null then 1 else 0 end)
)
from table;
Try this:
SELECT COUNT(*), column
FROM table
GROUP BY column

SQL - combination of MIN and COUNT

This is in Oracle database. Say I have the following table A:
column1 column2
id1 a
id2 a
id3 a
id4 b
id5 b
id6 c
So what I want the sql does is:
First count there's three As and two bs and one c, then based on the counts return me the smallest number of these counts, in this case is 1 (because we only have one c)
Can this be achieved somehow by using the combination of MIN and COUNT?
In Oracle you can do this directly; count per group and use MIN on the results to get back one row with the desired value.
select min(count(*))
from tablea
group by column1;
Try this:
SELECT MIN(Count) as MinVal
FROM
(SELECT column2,COUNT(column2) as Count
FROM TableA
GROUP BY column2) T
Explanation:
Inner query select the counts of column2 for each value of column2 in the table. Then with the outer query, the minimum count is selected.
If you are using Oracle 12, you can do this without a subquery:
select count(*) as cnt
from table t
group by cnt
order by cnt asc
fetch first 1 row only;
For those familiar with MySQL or Postgres, fetch first 1 row only is equivalent to limit, and allows you to limit the number of output rows without using a subquery.
This should work for you
SELECT *
FROM(
SELECT Column2, COUNT(Column1)
FROM TableA
GROUP BY Column2
ORDER BY COUNT(Column1))
WHERE Rownum = 1
SELECT MIN(cnt)
FROM (SELECT COUNT(*) AS cnt
FROM my_table
GROUP BY column2)
EDIT:
As ElmoVanKielmo noted, it's somewhat pointless to offer a solution without explaining it.
The inner query groups the data by column2 values, and return the number of rows for each one. The outer query treats these as just a bunch of numbers, and returns the minimal value among them.

Sum values from one column if Index column is distinct?

How do I sum values from one column when index column is distinct?
Initially, I had this SQL query:
SELECT COALESCE(SUM(ISNULL(cast(Quantity as int),0)),0) AS QuantitySum FROM Records
Also tried to do this, but this is incorrect when some Quantity values happen to be the same:
SELECT COALESCE(SUM(DISTINCT ISNULL(cast(Quantity as int),0)),0) AS QuantitySum FROM Records
How can I fix this query to sum only records quantity that is distinct by Index value?
Example of Table:
Index Quantity
AN121 40
AN121 40
BN222 120
BN111 20
BN2333 40
So.. I want to return 220
I have duplicate Ids, but quantity can be the same for different records
Do you mean that you only want to sum one value of quantity for each individual value of the index column?
select sum(case when row_number() over (partition by `index` order by newid()) = 1
then cast(Quantity as int)
end) as QuantitySum
from Records;
Or, do you mean that you only want to sum values of quantity when there is exactly one row with a given index value:
select sum(case when count(*) over (partition by `index`) = 1
then cast(Quantity as int)
end) as QuantitySum
from Records;
Both of these use window functions to restrict the values being processed.
Also, a column called quantity should be stored as a numeric type, so conversion isn't needed to take the sum.
You can try something like:
SELECT DISTINCT COL1
, SUM(COL2)
FROM MYTABLE
GROUP BY COL1
You can use this, if you have duplicated Ids and Quantity:
SELECT COALESCE(SUM(DISTINCT ISNULL(cast(Quantity as int),0)),0) AS QuantitySum
FROM (SELECT Id, Min(Quantity) From Records group by Id)

unique count of the columns?

i want to get a unique count of the of multiple columns containing the similar or different data...i am using sql server 2005...for one column i am able to take the unique count... but to take a count of multiple columns at a time, what's the query ?
You can run the following selected, getting the data from a derived table:
select count(*) from (select distinct c1, c2, from t1) dt
To get the count of combined unique column values, use
SELECT COUNT(*) FROM TableName GROUP BY UniqueColumn1, UniqueColumn2
To get the unique counts of multiple individual columns, use
SELECT COUNT(DISTINCT Column1), COUNT(DISTINCT Column2)
FROM TableName
Your question is not clear what exactly you want to achieve.
I think what you're getting at is individual SUMS from two unique columns in one query. I was able to accomplish this be using
SELECT FiscalYear, SUM(Col1) AS Col1Total, SUM(Col2) AS Col2Total
FROM TableName
GROUP BY FiscalYear
If your data is not numerical in nature, you can use CASE statements
SELECT FiscalYear, SUM(CASE WHEN ColA = 'abc' THEN 1 ELSE 0 END) AS ColATotal,
SUM(CASE WHEN ColB = 'xyz' THEN 1 ELSE 0 END) AS ColBTotal
FROM TableName
GROUP BY FiscalYear
Hope this helps!