sql query to count the rows - sql

i have a table like this
enter image description here

this will return you number of records in a table:
select count(*) from tablename;

SELECT
base.*, aaa.the_count
FROM
TABLE base
INNER JOIN (
SELECT
GROUP,
count(*) AS the_count
FROM
TABLE
GROUP BY
1
) aaa ON aaa. GROUP = base. GROUP

Related

Count on Table 1 based on Count with Clause on Table 2, sql

Table 1
Table 2
I need to find the Count of total number of unique stores that have "Achieved Date" not Null that achieved all of the "Achievement Ids" "enabled" on Table 2.
So far I can find the count of stores that achieved a hard coded number, but I'm not breaking through the part where I use the Count of Enabled Ids on table 2 to define what the number is.
SELECT
COUNT(*) AS count
FROM
(SELECT
StoreNumber, COUNT(*) as Achievements
FROM
StoreAchievementProgress
WHERE
AchievedDate IS NOT NULL
GROUP BY
StoreNumber) count
maybe this query
SELECT S.StoreNumber
FROM StoreAchievementProgress S
RIGHT JOIN (SELECT Id FROM Table2 WHERE Enabled=1 )T
ON T.Id=S.AchievementId
AND AchievedDate IS NOT NULL
GROUP BY S.StoreNumber
HAVING COUNT(1) = (SELECT COUNT(Id) FROM Table2 WHERE Enabled=1 )
Joining the stores with a count of their enabled achievements to how many they can get
SELECT COUNT(*) AS StoresFullAchievements
FROM
(
SELECT p.StoreNumber, COUNT(*) AS TotalEnabledAchievements
FROM StoreAchievementProgress p
JOIN Achievements a ON a.id = p.AchievementId
WHERE p.AchievedDate IS NOT NULL
AND a.Enabled = 1
GROUP BY p.StoreNumber
) AS s
JOIN
(
SELECT COUNT(*) AS TotalEnabled
FROM Achievements
WHERE Enabled = 1
) a
ON a.TotalEnabled = s.TotalEnabledAchievements

COUNT of GROUP of two fields in SQL Query -- Postgres

I have a table in postgres with 2 fields: they are columns of ids of users who have looked at some data, under two conditions:
viewee viewer
------ ------
93024 66994
93156 93151
93163 113671
137340 93161
92992 93161
93161 93135
93156 93024
And I want to group them by both viewee and viewer field, and count the number of occurrences, and return that count
from high to low:
id count
------ -----
93161 3
93156 2
93024 2
137340 1
66994 1
92992 1
93135 1
93151 1
93163 1
I have been running two queries, one for each column, and then combining the results in my JavaScript application code. My query for one field is...
SELECT "viewer",
COUNT("viewer")
FROM "public"."friend_currentfriend"
GROUP BY "viewer"
ORDER BY count DESC;
How would I rewrite this query to handle both fields at once?
You can combine to columns from the table into a single one by using union all then use group by as below:
select id ,count(*) Count from (
select viewee id from vv
union all
select viewer id from vv) t
group by id
order by count(*) desc
Results:
This is a good place to use a lateral join:
select v.viewx, count(*)
from t cross join lateral
(values (t.viewee), (t.viewer)) v(viewx)
group by v.viewx
order by count(*) desc;
You can try this :
SELECT a.ID,
SUM(a.Total) as Total
FROM (SELECT t.Viewee AS ID,
COUNT(t.Viewee) AS Total
FROM #Temp t
GROUP BY t.Viewee
UNION
SELECT t.Viewer AS ID,
COUNT(t.Viewer) AS Total
FROM #Temp t
GROUP BY t.Viewer
) a
GROUP BY a.ID
ORDER BY SUM(a.Total) DESC

Selecting Muliple Itme from Single Id from Sql Table

i have multiple products from 1 vendor, vendor id is falling into Product table, i want select vendor having multiple product, help me! my mind is not working!
I think you can do what you want with aggregation:
select supplierid
from table t
where MAMaterial in ('BUN', 'BEEF')
group by suppierid
having count(*) = 2; -- number of materials in list
You can use EXISTS :
SELECT t.*
FROM table t
WHERE EXISTS (SELECT 1 FROM table t1 WHERE t1.supplierid = t.supplierid AND t1.materialid <> t.materialid);
You can try this
Select supplerid from table
where MAMaterial in ('BUN', 'BEEF')
or
select TOP 1 supplerid from table
where MAMaterial in ('BUN', 'BEEF')

DB2 - how to find count multiple occurrences of column value

Im new to DB2 , and tried based on some similar posts, I have a table where I need to find the count of IDs based on where status=P and
the count of(primary=1) more than once.
so my result should be 2 here - (9876,3456)
Tried:
SELECT id, COUNT(isprimary) Counts
FROM table
GROUP BY id
HAVING COUNT(isprimary)=1;
Try the query below:
select ID as IDs,Count(isPrimary) as isPrimary
From Table
where Status = 'p'
Group by ID
Having Count(isPrimary) >1
You are close, I think all you need to do is to add a where clause like:
SELECT id, COUNT(*) as Counted
FROM table
WHERE PrimaryFlag = 1
AND[status] = 'P'
GROUP BY id
EDIT: if you need to count only the distinct IDs, then try:
SELECT COUNT(t.ID) FROM
(
SELECT id, COUNT(*) as Counted
FROM table
WHERE PrimaryFlag = 1
AND[status] = 'P'
GROUP BY id
) as t

Count single occurrences of a row item

I would like to count the number of times an item in a column has appeared only once. For example if in my table I had...
Name
----------
Fred
Barney
Wilma
Fred
Betty
Barney
Fred
...it would return me a count of 2 because only Wilma and Betty have appeared once.
Here is SQLFiddel Demo
Below is the Query which you can try:
select count(*) from
(select Name
from Table1
group by Name
having count(*) = 1) T
Till Above my post was for your actual Post.
Below is the post for modified question:
In oracle you can try below query:
select sum(count(rownum))
from Table1
group by "Name"
having count(*) = 1
OR
Here is SQLFiddel Demo
In SQL Server you can try below query:
SELECT COUNT(*)
FROM Table1 a
LEFT JOIN Table1 b
ON a.Name=b.Name
AND a.%%physloc%% <> b.%%physloc%%
WHERE b.Name IS NULL
OR
Here is the SQLFiddel Demo
In Sybase you can try below query:
select count(count(name))
from table
group by name
having count(name) = 1
as per #user2617962's answer.
Thank you
select count(*) from
(select count(*) from Table1
group by Name
having count(*) =1) s
SqlFiddle
Since you just need the count of column values appearing once without the actual value of the column, the query should be:
select count(count(name)) from table group by name having count(name)
= 1
Try following.
select name from (select name, count(name) as num from tblUsers group by name)
tblTemp where tblTemp.num=1
Mark it if this works..:)