advice needed for SQL query - sql

Can some one help to provide the SQL query should be used to pull out the "columna" value which has max number "columnb" value as "Active". Means in columnb there is a value "Active" , I want to pull the columna value which has max of value Active n columnb.
I am looking output to be columna = M1 and Count = 4
columna columnb
M1 Active
M1 Active
M1 Active
M1 Active
M2 failed
M2 failed
M2 failed
M3 pending
M3 pending
M3 pending

The results you request would be produced by:
SELECT columna,COUNT(*)
FROM Table
WHERE columnb = 'Active'
GROUP BY columna

SELECT top 1 columna,COUNT(*) as cnt
FROM Table1
WHERE columnb = 'Active'
GROUP BY columna
order by cnt desc
FIDDLE

SELECT columna,count(*) FROM TABLE_NAME where columnb = "Active" GROUP BY columna

Syntax is slightly different between RDMBS-es, but logic remains. Filter your rows based on columnb, group them by columna, order them on count(*) and select top 1
SQL Server:
SELECT TOP 1 columna, COUNT(*) AS Count
FROM YourTable
WHERE columnb = 'Active'
GROUP BY columna
ORDER BY COUNT(*) DESC
SQLFiddle DEMO
MySQL:
SELECT columna, COUNT(*) AS Count
FROM YourTable
WHERE columnb = 'Active'
GROUP BY columna
ORDER BY COUNT(*) DESC
LIMIT 1
SQLFiddle DEMO

Related

Finding Specific Rows in SQL which must Includes Value From ColumnB

I have a table similar to below table. I want to select all ColumnA Values which has the Value "X" for ColumnB but also other possible ColumnB values.
ColumnA
ColumnB
One
X
One
Y
Two
Y
Two
Z
Three
X
Three
Z
So basically the query should result like this. Can you help me to achieve this?
ColumnA
ColumnB
One
X
One
Y
Three
X
Three
Z
One solution: once you know what Table1 values you want you can select them. So resolve that in a subquery. We'll alias the original table as X and the subquery as Y:
select X.*
from Table1 X
inner join
(
select
ColumnA
from
Table1
Where
ColumnB = 'X'
) Y
on X.ColumnA = Y.ColumnA
you could use min() over a window:
select columnA, columnB
from (
select *, min(columnb) over(partition by columna) mb
from t
)t
where mb = 'X';
I believe a subquery would be the simplest way.
Step 1 - find all the columnA values where columnB = 'x'
SELECT DISTINCT ColumnA
FROM table_name
WHERE ColumnB = 'x'
step 2 - select all the records where the value in ColumnA is in this list
SELECT *
FROM table_name
WHERE ColumnA in (SELECT DISTINCT ColumnA
FROM table_name
WHERE ColumnB = 'x')

Selecting certain value from row based on another value in same row

I have a following table with following data:
Table
Now I want to get all those users (distinct only) who do not have value 5 in Column B. What I mean is user1 has a value 5 in some row, then all user 1 rows are dismissed.
Following result should be produced:
user2 (because value is null)
user3 (no value 5)
How can I do that?
Perhaps the easiest way to do this would be aggregation by user:
SELECT ColumnA
FROM yourTable
GROUP BY ColumnA
HAVING COUNT(CASE WHEN ColumnB = 5 THEN 1 END) = 0;
One method is aggregation:
select columnA
from t
group by columnA
having sum(case when columnB = 5 then 1 else 0 end) = 0;
You can do this by Minus operator
SELECT distinct colA
FROM have
WHERE colB not in(5)
MINUS
SELECT distinct colA
FROM have
WHERE colB=5;
Using NOT EXISTS you can able to get the result
SELECT DISTINCT T1.ColumnA
FROM TableName T1
WHERE NOT EXISTS (
SELECT * FROM TableName T2 WHERE T2.ColumnA = T1.ColumnA AND T2.ColumnB <> 5
)
One more way -
SELECT DISTINCT T1.ColumnA
FROM TableName T1
WHERE T1.ColumnA NOT IN
(
SELECT T2.ColumnA FROM TableName T2 WHERE T2.ColumnB = 5
)

Find Average from recent most data when there are multiple

Hi I need to find out a way to find average on data set when there multiple values but consider a specific value based on recent most
Table 1
ColumnA ColumnB ColumnC
A 3 07/21/2017
A 2 08/08/2017
B 1 07/22/2017
Calculate averae of ColumnB
It should 2 from A, 1 from B, which should be (2+1)/2 =1.5 and not (3+2+1)/3=2. The criteria is based on recent most time stamp of each category of columnA
I would use row_number():
select avg(columnB * 1.0)
from (select t.*,
row_number() over (partition by columnA order by columnC desc) as seqnum
from t
) t
where seqnum = 1;
http://sqlfiddle.com/#!6/daea4/3
Select
ColumnA, Avg(ColumnB)
from
table1
group by
ColumnA

Group BY on Condition basis

I have data in following way....
ColumnA ColumnB
7675 22838
7675 24907
7675 NULL
I want the results in following way.....
ColumnA ColumnB
7675 2 (need total count for Not Null value)
7675 0 (need count 0 for NULL value)
SELECT ColumnA, COUNT(ColumnB) ColumnB
FROM YourTable
GROUP BY ColumnA
UNION ALL
SELECT ColumnA, 0
FROM YourTable
WHERE ColumnB IS NULL
GROUP BY ColumnA
You could introduce a calculated column indicating whether ColumnB is null or not and use it as a grouping criterion together with ColumnA:
SELECT
t.ColumnA,
ColumnB = COUNT(t.ColumnB)
FROM
dbo.YourTable AS t
CROSS APPLY
(SELECT CASE WHEN t.ColumnB IS NULL THEN 1 ELSE 0 END) AS x (SubGroup)
GROUP BY
t.ColumnA,
x.SubGroup
ORDER BY
t.ColumnA,
x.SubGroup
;
The COUNT(t.ColumnB) expression would always be NULL for a null subgroup, and for the corresponding non-null subgroup it would return the number of the non-null entries.
select columnA,
count(columnB) as non_null_count,
sum(columnB is null) as null_count
from your_table
group by ColumnA
you could easily do with a count and sum which may be faster if there are a lot of rows rather than selecting all of the rows twice with a UNION
SELECT columna, columnb, SUM(mycount)
FROM
( SELECT *, COUNT(columnb) as mycount
FROM test
GROUP BY columnb
)t
GROUP BY mycount
ORDER BY CASE WHEN mycount = 0 THEN 1 ELSE 2 END DESC;
Fiddle Demo

Duplicate Checks with Multiple Values

I am doing some manual duplicate checks on my database, and have a complicated case.
I need to check for duplicate rows based on a value in Column A, which I have done. However, in this specific case, there might be multiple records that have the same value for Column A but a different value for Column E.
Here is my original query:
SELECT ColumnA, COUNT(*) TotalCount
FROM TableA
INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
GROUP BY ColumnA
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
I now need to filter out duplicates for ColumnA where ColumnE is different, or unique. I have added psuedocode to my original query
SELECT ColumnA, COUNT(*) TotalCount
FROM TableA
INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
AND ColumnE is not unique
GROUP BY ColumnA
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
I hope this makes sense.
You need a GROUP BY clause on a ColumnA column and HAVING clause on DISTINCT ColumnE
SELECT ColumnA, COUNT(*) TotalCount
FROM TableA INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
GROUP BY ColumnA
HAVING COUNT(DISTINCT ColumnE) > 1
ORDER BY COUNT(*) DESC
You could just add ColumnE into the grouping, as shown below:
SELECT ColumnA, ColumnE, COUNT(*) TotalCount
FROM TableA
INNER JOIN TableA_1 on fID = hID
WHERE dateCreated > '2013-05-08 00:00:00'
GROUP BY ColumnA, ColumnE
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC