How to make total after the count column values in SQL - sql

I try to find
select Code, count(Code)
from Table
group by code
and need result to look like this:

You can do it as follows:
SELECT Code, count(Code) as count
FROM Table
GROUP BY count
You just need to adjust your GROUP BY condition.

You probably need to do this somewhere other than in SQL, but it still works to have the total column. If you want to work on the ordering to make sure that the TOTAL column is at the bottom. Add an ortdering column and order by that
select Code, count(Code)
from Table
group by code
UNION
select 'All' as Code, count(Code) as COUNT
from Table

Related

SQL select column value with biggest number of duplicates

I am having a problem I can't seem to solve. I have a data table that looks like this:
Example:
http://i.stack.imgur.com/tbKEk.png
I need to select the ID_JOB value which is duplicated the most. In this particular example it would be ID_JOB = 1.
Adapt this to your specific SQL implementation. Substitute [job_table] with the table you are querying.
SELECT TOP 1 ID_JOB
FROM job_table
GROUP BY ID_JOB
ORDER BY COUNT(*) DESC
You may need to add more ORDER BY logic in case a count "ties".

Average when meeting where clause

I want to find the average amount of a field where it meets a criterion. It is embedded in a big table but I would like this average field in there instead of doing it in a separate table.
This is what I have so far:
Select....
Avg( (currbal) where (select * from table
where ament2 in ('r1','r2'))
From table
If you want to AVG only a subset of a query use case when ... then to replace value in non-matching rows with null as nulls are ignored by avg().
Select id,
sum(something) SomethingSummed,
avg(case when ament2 in ('r1','r2') then currbal end) CurrbalAveragedForR1R2
From [table]
group by id
You can put all the other sums which you want to be embedded into the AVG statement, inside the table reference inside the FROM clause. Something like:
SELECT AVG(currbal)
FROM
(
SELECT * -- other sums
FROM table
WHERE ament2 IN ('r1','r2')
) t
You can write a full sub-select into the select list:
SELECT ...,
(SELECT AVG(Currbal) FROM Table WHERE ament2 IN ('r1', 'r2')) AS avg_currbal,
...
FROM ...
Whether that will do exactly what you want depends on a number of things. You might need to make that into a correlated subquery; assuming 'ament2' is in Table, it is not a correlated sub-query at the moment.

SQL query selection help need

I have a table which has column called payment_txn_status
I have to write a query which shows distinct status code with their respective count.
My current query which is as below gives me only distinct status code but how to get count for each individual status code
select distinct payment_txn_status FROM tpayment_txn
Use a GROUP BY and a COUNT:
SELECT payment_txn_status, COUNT(*) AS num
FROM tpayment_txn
GROUP BY payment_txn_status

sqlite get records with same column value

I have a SQLite DB that has people LastName, FirstName, Department and I need to make a query that shows me any people with the same First & Last Names. I've found the following statement that supposedly does what I want for a single field, however it doesn't seem to work for me when I try to use it to pull all records with just the last name being the same. How can I do this?
Select myField From myTable Group by myField Where count(myField)>1
try:
Select
firstname,LastName,Count(*)
From myTable
Group by firstname,LastName
HAVING Count(*)>1
GROUP BY combines rows where the named values are the same.
HAVING removes groups that do not meet the condition.
The above query will list the first and last names, along with a count of duplicates for all first/last names that actually have duplicates.
Firstly, you need to use HAVING, not WHERE to qualify the GROUPed BY result:
SELECT myField FROM myTable GROUP BY myField HAVING COUNT(myField) > 1
Secondly, you can extend this to multiple columns like this:
SELECT MyCol1, MyCol2 FROM MyTable
GROUP BY MyCol1, MyCol2
HAVING COUNT(*) > 1

Can I group by something that isn't in the SELECT line?

Given a command in SQL;
SELECT ...
FROM ...
GROUP BY ...
Can I group by something that isn't in the SELECT line?
Yes.
This is often used in the superaggregate queries like this:
SELECT AVG(cnt)
FROM (
SELECT COUNT(*) AS cnt
FROM sales
GROUP BY
product
HAVING COUNT(*) > 10
) q
, which aggregate the aggregates.
Yes of course e.g.
select
count(*)
from
some_table_with_updated_column
group by
trunc(updated, 'MM.YYYY')
Yes you can do it, but if you do that you won't be able to tell which result is for which group.
As a result, you almost always want to return the columns you've grouped by in the select clause. But you don't have to.
Yes, you can. Example:
select count(1)
from sales
group by salesman_id
What you can't do, of course, if having something on your select clause (other than aggregate functions) that are not part of the group by clause.
Hmm, I think the question should have been in the other way round like,
Can I SELECT something that is not there in the GROUP BY?
It's alright to write a code like:
SELECT customerId, count(orderId) FROM orders
GROUP BY customerId, orderedOn
If you want to find out the number of orders done by a customer datewise.
But you cannot do it the other way round:
SELECT customerId, orderedOn count(orderId) FROM orders
GROUP BY customerId
You can issue an aggregate function on the column that is not there in the group by. But you cannot give it in the select line without the aggregate function. As it will not make much sense. Like for the above query. You group by just customerId for order counts and you want the date also to be printed in the output??!! You don't involve the date factor in the group for counting then will it mean something to have a date in it?
I don't know about other DBMS' but DB2/z, for one, does this just fine. It's not required to have the column in the select portion but, of course, it does have to extract the data from the table in order to aggregate so you're probably not saving any time by leaving it off. You should only select the columns that you need, aggregation of the data is a separate task from that.
I'm pretty certain the SQL standard allows this (although that's only based on the knowledge that the mainframe DB2 product follows it pretty closely).