Count rows according to 2 column with Group By - sql

I have a database table of 3 columns
RecordID Deleted CardHolderID
1963 1 9
4601 1 9
6996 0 9
1532 1 11
1529 0 20
I want an sql query to output the sum of rows of Deleted column grouped by CardHolderID.
However query below outputs 2 columns for CardHolderID
select c.CardHolderID, c.Deleted, COUNT(*) as Sum
from Card c
group by c.Deleted, c.CardHolderID
CardHolderID Deleted Sum
9 0 1
9 1 2
20 0 1
11 1 1
I want to include 2 columns as Deleted0 (count of rows with Deleted column equal to 0) and Deleted1 (count of rows with Deleted column equal to 1)
CardHolderID Deleted0 Deleted1
9 1 2
20 1 0
11 1 1
How should be the SQL query for such a result?
Kind regards

Using conditional count:
select c.CardHolderID,
count( case when c.deleted > 0 then 1 else null end ) deleted0,
count( case when c.deleted = 0 then 1 else null end ) deleted1,
from Card c
group by c.CardHolderID

GROUP BY CardHolderID alone.
Use SUM(Deleted) to count the 1's.
Use SUM(1-deleted) to count the 0's.
select c.CardHolderID, sum(1-c.deleted) deleted0, sum(c.Deleted) deleted1
from Card c
group by c.CardHolderID

if you are using MSSQL
select DtlPivot.CardHolderID, isnull(DtlPivot.[0],0) as Deleted0, isnull(DtlPivot.[1],0) as Deleted1 from
(
select c.CardHolderID, c.Deleted, COUNT(*) as Total from Card c
group by c.Deleted, c.CardHolderID
) aa
PIVOT
(
sum(Total) FOR Deleted IN([0],[1])
)AS DtlPivot

Related

SUM the COUNT results from a database table with two different value

I'd like to count total events, which can have two different values, and I could not figure out how to merge them together. My query is the following:
SELECT TOP(20)
[MatchEvents].[PlayerID], [MatchEvents].[EventType],
COUNT([MatchEvents].[ID]) AS [TOTAL]
FROM
[MatchEvents]
INNER JOIN
[Match] ON [MatchEvents].[MatchID] = [Match].[ID]
AND [Match].[Season] = 1
WHERE
([MatchEvents].[EventType] = 0 OR [MatchEvents].[EventType] = 1)
GROUP BY
[MatchEvents].[PlayerID], [MatchEvents].[EventType]
ORDER BY
[TOTAL] ESC
Current output:
PlayerID
EventType
Total
1
0
8
1
1
3
2
0
8
2
1
3
3
0
8
3
1
3
Expected output:
PlayerID
Total
1
11
2
11
3
11
How could I merge my current results further?
Thanks!
From your expected results it appears you just need to remove grouping by EventType
I would suggest the following:
select top(20) me.PlayerID, Count(*) as Total
from MatchEvents me
join [Match] m on m.Id = me.MatchId and m.Season = 1
where me.EventType in (0, 1)
group by me.PlayerID
order by Total desc;

Use aggregation only on rows where count(ID) is greater than one

Hi I have the following table
Cash_table
ID Cash Rates
1 50 3
2 100 4
3 70 10
3 60 10
4 13 7
5 20 8
5 10 10
6 10 5
What I want as a result is to cumulate all the entries that have a Count(id)>1 like this:
ID New_Cash New_Rates
1 50 3
2 100 4
3 (70+60)/(10+10) 10+10
4 13 7
5 (20+10)/(8+10) 8+10
6 10 5
So I only want to change the rows where Count(id)>1 and leave the rest like it was.
For the rows with count(id)>1 I want to sum up the rates and take the sum of the cash and divide it by the sum of the rates. The Rates alone aren't a problem since I can sum them up and group by id and get the desired result.
The problem is with the cash column:
I am trying to do it with a case statement but it isn't working:
select id, sum(rates) as new_rates, case
when count(id)>1 then sum(cash)/nullif(sum(rates),0))
else cash
end as new_cash
from Cash_table
group by id
You only need group by id and aggregate:
select
id,
sum(cash) / (case count(*) when 1 then 1 else sum(rates) end) as new_cash,
sum(rates) as new_rates
from Cash_table
group by id
order by id
See the demo.
You can aggregate rate and cash columns by sum() function with grouping by id
select
id,
sum(cash)/decode( sum( nvl(rates,0) ), 0 ,1, sum( nvl(rates,0) )) as new_cash,
sum(rates) as new_rates
from cash_table
group by id
there's no nullif() function in Oracle, use nvl() instead
switch case part ( where decode() function is used ) against the
possibility of division by zero

How to declare a row as a Alternate Row

id Name claim priority
1 yatin 70 5
6 yatin 1 10
2 hiren 30 3
3 pankaj 40 2
4 kavin 50 1
5 jigo 10 4
7 jigo 1 10
this is my table and i want to arrange this table as shown below
id Name claim priority AlternateFlag
1 yatin 70 5 0
6 yatin 1 10 0
2 hiren 30 3 1
3 pankaj 40 2 0
4 kavin 50 1 1
5 jigo 10 4 0
7 jigo 1 10 0
It is sorted as alternate group of same row.
I am Using sql server 2005. Alternate flag starts with '0'. In my example First record with name "yatin" so set AlternateFlag as '0'.
Now second record has a same name as "yatin" so alternate flag would be '0'
Now Third record with name "hiren" is single record, so assign '1' to it
In short i want identify alternate group with same name...
Hope you understand my problem
Thanks in advance
Try
SELECT t.*, f.AlternateFlag
FROM tbl t
JOIN (
SELECT [name],
AlternateFlag = ~CAST(ROW_NUMBER() OVER(ORDER BY MIN(ID)) % 2 AS BIT)
FROM tbl
GROUP BY name
) f ON f.name = t.name
demo
You could use probably an aggregate function COUNT() and then HAVING() and then UNION both Table, like:
SELECT id, A.Name, Claim, Priority, 0 as AlternateFlag
FROM YourTable
INNER JOIN (
SELECT Name, COUNT(*) as NameCount
FROM YourTable
GROUP BY Name
HAVING COUNT(*) > 1 ) A
ON YourTable.Name = A.Name
UNION ALL
SELECT id, B.Name, Claim, Priority, 1 as AlternateFlag
FROM YourTable
INNER JOIN (
SELECT Name, COUNT(*) as NameCount
FROM YourTable
GROUP BY Name
HAVING COUNT(*) = 1 ) B
ON YourTable.Name = B.Name
Now, this assumes that the Names are unique meaning the names like Yatin for example although has two counts is only associated to one person.
See my SqlFiddle Demo
You can use Row_Number() function with OVER that will give you enumeration, than use the reminder of integer division it by 2 - so you'll get 1s and 0s in your SELECT or in the view.

SQL - how to replace my sum results in one number

I have a query that sums some columns
SELECT P, sum (K)
FROM table
GROUP BY P
and i want that if the sum is more than 1 i will have 1 in results. Meaning instead of:
P K
1 2
3 4
23 0
I will have:
P K
1 1
3 1
23 0
use CASE
SELECT P,
(CASE WHEN sum(K) > 0 THEN 1 ELSE 0 END) AS Result
FROM tableName
GROUP BY P
UPDATE
SQLFiddle Demo
Thanks to Luv
SELECT P, LEAST(1, sum (K))
FROM table
GROUP BY P
It sounds like there are 2 conditions sum(K) > 1 then you want want, the only option for sum(K) < 1 is 0. So this will give you that.

Return results where first entry is 1 and all subsequent rows are 0

I m working on weird SQL query
Patient_ID Count order_no
1 1 1
2 1 2
2 0 3
2 0 4
3 1 5
3 0 6
where I need to count the patient as above, for every new patient , the count column is 1.
If repeated , the below entry it should be 0
I m confused how should make that work in SQL
In order to make the first entry 1 and all subsuqent entries 0, I believe you need a ranking with partition by the order number. Please checkout the sqlfiddle below to test results.
http://www.sqlfiddle.com/#!3/4e2e2/17/0
SELECT
patient_id
,CASE WHEN r.rank = 1
THEN 1
ELSE 0
END
, order_number
FROM
(
SELECT
order_number
,patient_id
,ROW_NUMBER() OVER (PARTITION BY patient_id ORDER BY order_number)[rank]
FROM
PatientTable
)r