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.
Related
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;
I need help with writing a query to generate a result that will provide me with all record numbers NOT assigned to Group D from a table structured like the below example. From the below table my desired result would be record number "3" .
Record_Number Assigned_To_Group
1 A
1 B
1 C
1 D
2 A
2 E
2 D
3 A
3 B
3 E
One method uses aggregation:
select Record_Number
from t
group by Record_Number
having sum(case when Assigned_To_Group = 'D' then 1 else 0 end) = 0;
I must do a query where I select those groups, given by de concatenation between sample and serial that could be defined as household, where at least one in the variable bplcountry = 1
sample serial bplcountry
1 1 2
1 1 1
1 3 2
2 1 2
2 2 2
2 3 2
3 1 2
3 3 2
3 3 1
I have made some research but I'm very amateur on SQL. I get some hint like this:
SELECT *
FROM latinCensus
GROUP BY sample AND serial
HAVING COUNT(bplcountry NOT IN ('1') OR NULL) = 0
Also I got some idea in this way
SELECT *
FROM latinCensus
GROUP BY CONCAT(sample,serial)
HAVING COUNT(bplcountry NOT IN ('1') OR NULL) = 0
I would expect something like this:
sample serial bplcountry
1 1 2
1 1 1
3 3 2
3 3 1
I will appreciate your help!
You want the pairs where bplcountry is 1. You can use window functions:
select lc.*
from (select lc.*,
sum(case when bplcountry = 1 then 1 else 0 end) over (partition by sample, serial) as cnt_1
from latincensus lc
) lc
where cnt_1 > 0;
Or use exists:
select lc.*
from latincensus lc
where exists (select 1
from latincensus lc2
where lc2.sample = lc.sample and lc2.serial = lc.serial and
lc2.bplcountry = 1
);
You haven't tagged your db, but something along these lines should work (can also be expressed using joins)
select sample, serial, bplcountry
from t
where (sample,serial) in (select sample,serial
from t
where bplcountry=1);
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
Table Z has A-M columns:
I have to display columns A,C,F,G,K,L,M with following conditions:
a. Search for selected date range(column A is date column)
b. Display top 10 rows where K> 5000
c. If the above returns less than 10 rows then find remaining rows where L> 500
d. If both above results return less than total of 10 rows then find remaining rows where M>50
PS: Rows will be total of 10 made out of result from b,c and d
You should be able to use an analytic function
SELECT <<whatever you want>>
FROM (SELECT z.*,
row_number() over (order by case when k > 5000 then 1 else 2 end,
case when l > 500 then 1 else 2 end,
case when m > 50 then 1 else 2 end) rn
FROM z
WHERE a BETWEEN <<lower date>> and <<upper date>>)
WHERE rn <= 10