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
Related
i have this query
SELECT PersonalInfo.id, PersonalInfo.[k-commission], Abs(Not IsNull([PersonalInfo]![k-commission].[Value])) AS CommissionAbsent
FROM PersonalInfo;
and the PersonalInfo.k-commission is a multi value field. the CommissionAbsent shows duplicate values for each k-commission value. when i use DISTINCT i get an error saying that the keyword cannot be used with a multi value field.
now i want to remove the duplicates and show only one result for each. i tried using a WHERE but i dont know how.
edit: i have a lot more columnes and in the example i only showed the few i need.
You can use GROUP BY and COUNT to solve your problem, here is an example for it
SELECT clmn1, clmn2, COUNT(*) as count
FROM table
GROUP BY clmn1, clmn2
HAVING COUNT(*) > 1;
the query groups the rows in the table by the clmn1 and clmn2 columns, and counts the number of occurrences of each group. The HAVING clause is then used to filter the groups and only return the groups that have a count greater than 1, which indicates duplicates.
If you want to select all, then you can do like this
SELECT *
FROM table
WHERE (clmn1, clmn2) IN (SELECT clmn1, clmn2
FROM table
GROUP BY clmn1, clmn2
HAVING COUNT(*) > 1)
SELECT PersonalInfo.id, PersonalInfo.[k-commission], Abs(Not IsNull([PersonalInfo]![k-commission].[Value])) AS CommissionAbsent
FROM PersonalInfo
GROUP BY PersonalInfo.id, PersonalInfo.[k-commission], Abs(Not IsNull([PersonalInfo]![k-commission].[Value]))
HAVING COUNT(*) > 1
Can you filter a SQL table based on an aggregated value, but still show column values that weren't in the aggregate statement?
My table has only 3 columns: "Composer_Tune", "_Year", and "_Rank".
I want to use SQL to find which "Composer_Tune" values are repeated in each annual list, as well as which ranks the duplicated items had.
Since I am grouping by "Composer_Tune" & "Year", I can't list "_Rank" with my current code.
The image shows the results of my original "find the duplicates" query vs what I want:
Current vs Desired Results
I tried applying the concepts in this Aggregate Subquery StackOverflow post but am still getting "_Rank is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause" from this code:
WITH DUPE_DB AS (SELECT * FROM DB.dbo.[NAME] GROUP BY Composer_Tune, _Year HAVING COUNT(*)>1)
SELECT Composer_Tune, _Year, _Rank
FROM DUPE_DB
You need to explicitly declare the columns used in the Group By expression in the select columns.
You can use the following documentation if you are using transact sql for the proper use of Group By.
Simply join the aggregated resultset to original unit level table:
WITH DUPE_DB AS (
SELECT Composer_Tune, _Year
FROM DB.dbo.[NAME]
GROUP BY Composer_Tune, _Year
HAVING COUNT(*) > 1
)
SELECT n.Composer_Tune, n._Year, n._Rank
FROM DB.dbo.[NAME] n
INNER JOIN DUPE_DB
ON n.Compuser_Tune = DUPE_DB.Composer_Tune
AND n._Year = DUPE_DB._Year
ORDER n.Composer_Tune, n._Year
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
I am doing a selection on a table, but also need the count of the items. Apparently I have to use window functions but can't get it work.
I am doing a simple select query and want to count how many entries are there, so something like:
SELECT * FROM "myTable"
COUNT(*) OVER() AS total
WHERE name='John Doe';
This is not working and I am getting the following error: ERROR: ERROR: syntax error at or near "COUNT" LINE 2: COUNT(*) OVER(name) AS _total
How do I use the window functions to count my entries in a table while doing a query? Am I way off base here?
The COUNT(*) is a column in the SELECT:
SELECT t.*, COUNT(*) OVER() AS total
FROM "myTable" t
WHERE name = 'John Doe';
The FROM clause follows the SELECT clause and ends the definitions of the columns in the result set.
I have a problem here in that I don't know how to execute this S Q L query as I am not sure of the correct syntax...
I am trying to select all records from a record set (populated by a table), where a particular field is entered twice...
please what query should i use to get all records which showing double records in a field.
Try doing something like this:
SELECT "YourFieldName(s)", COUNT(*) AS RecordCount
FROM "YourTableName"
GROUP BY "YourFieldName(s)"
HAVING COUNT(*) > 1
"YourFieldName(s)" can be multiple columns if that is what you are checking duplicates on.
Select columnname,count(*) as cnt
from dbo.table
group by columnname
having count(*) > 1
this gives you the rows which has duplicates in the column " columnname" . edit to your requirement.