Using distinct and case for two separate columns - sql

I am counting cases by year using the following code:
COUNT(CASE WHEN Year(FilingDate)=2008 THEN 1 ELSE NULL END) AS '2008'
and I want to only count these cases when another column is distinct. The other column is called 'FilingDate' What I imagine would look something like this:
COUNT(CASE distinct (DocketNumber) WHEN Year(FilingDate)=2008 THEN 1 ELSE NULL END) AS '2008',
The Sample Records:
DocketNumber FilingDate
123 2008
123 2008
123 2008
111 2009
112 2009
I would just like to recieve = 1
Any Ideas?
Thanks

Use option with SUBQUERY and GROUP BY clause
SELECT COUNT(CASE WHEN YearFilingDate = 2008 THEN 1 END) AS '2008'
FROM
(
SELECT Year(FilingDate) AS YearFilingDate, DocketNumber
FROM dbo.test55
GROUP BY Year(FilingDate), DocketNumber
) x
Demo on SQLFiddle

SELECT COUNT(CASE WHEN Year(FilingDate)=2008 THEN 1 ELSE NULL END) AS '2008'
GROUP BY DocketNumber
may be.

Related

2 column with 2 different condition in one table

is it possible to create a query that will output 2 column with 2 different WHERE clause
this my ideal output:
| YEAR | WW | COUNT | *COUNT2 |
where count 1 is the result of the 1st WHERE clause. Which I already created.
What I want is to have another column where in the value will be with different WHERE clause.
here's my query:
SELECT extract(YEAR FROM EVT_TARGET) as Years, to_char(EVT_TARGET, 'ww') AS WorkWeek, COUNT(*)
FROM r5events
WHERE EVT_JOBTYPE = 'CORR' AND EVT_RSTATUS <> 'C' AND EVT_TARGET IS NOT NULL
GROUP BY extract(YEAR FROM EVT_TARGET), to_char(EVT_TARGET, 'ww');
with this query I was able to get the 1st 3 column, my problem now is how to supply value on the 4th column with the value of EVT_RSTATUS = 'C'.
Any insight?
TIA
I think you want conditional aggregation:
SELECT
extract(YEAR FROM EVT_TARGET) as Years,
to_char(EVT_TARGET,'ww') AS WorkWeek,
SUM(CASE WHEN EVT_RSTATUS <> 'C' THEN 1 ELSE 0 END) AS cnt1,
SUM(CASE WHEN EVT_RSTATUS = 'C' THEN 1 ELSE 0 END) AS cnt2
FROM r5events
WHERE EVT_JOBTYPE ='CORR' AND EVT_TARGET IS NOT NULL
GROUP BY extract(YEAR FROM EVT_TARGET), to_char(EVT_TARGET,'ww');

Counting two different values for each year

I have a table which includes Years and Genders. Here is an example of my table:
YEAR Sex
1999 M
1999 M
1999 F
1999 F
I use this query for taking my result.
SELECT YEAR,COUNT(*)
FROM athlete_events
WHERE SEX = 'M'
GROUP BY YEAR;
And I see this result on output:
YEAR COUNT(*)
1999 2
However I want to see this result:
YEAR COUNT_MALE COUNT_FEMALE
1999 2 2
Is that possible in Pracle SQL?
You can do conditional aggregation with a case expression:
select year,
count(case when sex = 'M' then year end) as count_male,
count(case when sex = 'F' then year end) as count_female
from athlete_events
group by year;
YEAR COUNT_MALE COUNT_FEMALE
---------- ---------- ------------
1999 2 2
The count function ignores nulls, so rows that don't match the specified flag (which default to null in the case expression; you can have an explicit else null if you prefer) are not counted.
(Personally I prefer to use count() rather than sum() for this sort of thing as that better reflects what you're actually doing - counting things.)
You need conditional aggregation :
select year,
sum(case when SEX = 'M' then 1 else 0 end) as count_male,
sum(case when SEX = 'F' then 1 else 0 end) as count_female
from athlete_events
group by year;
You can do subqueries, something like this:
SELECT
E.YEAR,
(select count(*) from athlete_events M where M.YEAR=E.YEAR and M.SEX='M') COUNT_MALE,
(select count(*) from athlete_events F where F.YEAR=E.YEAR and F.SEX='F') COUNT_FEMALE
FROM athlete_events E
GROUP BY E.YEAR;
This is more expensive (performance-wise) than conditional aggregation, on the other hand maybe easier to understand and maintain.

SQL get count from one table and split to two columns

I have a table like this:
Date Product
1/1/2015 Apples
1/1/2015 Apples
1/1/2015 Oranges
1/2/2015 Apples
1/2/2015 Apples
1/2/2015 Oranges
How can I do a select so I get something like this:
Date Count of Apples Count of Oranges
1/1/2015 2 1
1/2/2015 2 1
Thanks. I have tried case like this but the error is being thrown:
Select 'Date',
CASE WHEN 'Product' = 'Apples' THEN COUNT(*) ELSE 0 END as 'Count'
FROM #TEMP Group by 1,2
Each GROUP BY expression must contain at least one column that is not an outer reference.
You can do conditional aggregation like this:
select
[date],
sum(case when Product = 'Apples' then 1 else 0 end) as [Count of Apples],
sum(case when Product = 'Oranges' then 1 else 0 end) as [Count of Oranges]
from #temp
group by [date]
With conditional aggregation:
select date,
sum(case when Product = 'Apples' then 1 else 0 end) as Apples,
sum(case when Product = 'Oranges' then 1 else 0 end) as Oranges,
from table
group by date
SQL Server does not accept column references in the GROUP BY. So, "1" and "2" refer to, well, the numbers one and two.
However, you seem to be confusing string constants and columns. Only use single quotes for string and date constants. I suspect that Date and Product are the names of columns. So, the query you probably want is a conditional aggregation:
Select Date,
SUM(CASE WHEN Product = 'Apples' THEN 1 ELSE 0 END) as NumApples
FROM #TEMP
Group by Date
Order by Date;

SQL query to transform combination of values into a column

I have a table like this:
user_id, gender, sent
1 M 100
1 F 120
2 M 20
2 F 30
I want a table like this from the above:
user_id, male_sent, female_sent, total_sent
1 100 120 220
2 20 30 50
I lack the (Postgres) SQL foo to figure this one out.
You can use an aggregate function with a CASE expression to get the result:
select user_id,
sum(case when gender = 'M' then sent else 0 end) male_sent,
sum(case when gender = 'F' then sent else 0 end) female_sent,
sum(sent) total_sent
from yourtable
group by user_id
See SQL Fiddle with Demo
What database are you using?
If you are using SQL Server, you could do something like this:
SELECT user_id,sum(case when gender = 'M' then sent else 0 end) as male_sent,
sum(case when gender = 'F' then sent else 0 end) as female_sent,
sum(sent) as total_sent
FROM your_table_name
GROUP BY user_id

SQL Server : display column states by date

This is my db schema:
http://sqlfiddle.com/#!3/bbaea/3
The only important columns are CreationTime (as a timestamp) and Result.
Result can currently be 0, 1, 2 or 3
What my result should look like:
Year, Month, Day | Entries with Result 0 | With 1 | With 2 | ..
I'm new to SQL Server itself and I have no idea how to solve this problem. I tried some subselects (like in my fiddle) but they are not working as expected. I guess this is because I have to join the subselects, but how? Also I want to know the best way of how to do this..
Some help?
You don't need to union several queries. You can use a case statement:
SELECT
DAY(CreationTime) AS d, MONTH(CreationTime) AS m, YEAR(CreationTime) AS y
,count(CASE WHEN RESULT = 0 THEN 1 ELSE NULL END) AS R0
,count(CASE WHEN RESULT = 1 THEN 1 ELSE NULL END) AS R1
,count(CASE WHEN RESULT = 2 THEN 1 ELSE NULL END) AS R2
FROM History
GROUP BY DAY(CreationTime), MONTH(CreationTime), YEAR(CreationTime)
SELECT * FROM History;
Answer on SQL Fiddle.
You can try this
SELECT YEAR(CreationTime) AS y, MONTH(CreationTime) AS m, DAY(CreationTime) AS d,
COUNT(CASE WHEN Result = 0 THEN 1 END) AS WITH0,
COUNT(CASE WHEN Result = 1 THEN 1 END) AS WITH1,
COUNT(CASE WHEN Result = 2 THEN 1 END) AS WITH2
FROM History
GROUP BY YEAR(CreationTime), MONTH(CreationTime), DAY(CreationTime)