How to get multiple counts based on different criterias - sql

I need help making a query so I found this site for the first time and hoping someone here knows how to do it.
Lets say my database has 3 columns (Name,Type,and Decision). In the Name field there are 100 records and there can be duplicates. There are 2 different types (online and offline), and 2 kinds of decisions (Match or Mismatch).
What I need is to get a count of the matches and mismatches for each type, grouped by the Name.
The table would look like this with the columns below:
Name|Online Match Count|Online Mismatch Count|Offline Match Count|Offline Mismatch Count|
Also if any of the fields have a count of 0, I want it to display it as 0 as well.
Does anybody know how to do this? I would greatly appreciate it.

This is a common technique and is called a pivot query.
SELECT
Name,
SUM(CASE WHEN Type='online' AND Decision='Match' THEN 1 ELSE 0 END) as "Online Match Count",
SUM(CASE WHEN Type='online' AND Decision='Mismatch' THEN 1 ELSE 0 END) as "Online Mismatch Count",
SUM(CASE WHEN Type='offline' AND Decision='Match' THEN 1 ELSE 0) as "Offline Match Count",
SUM(CASE WHEN Type='offline' AND Decision='Mismatch' THEN 1 ELSE 0 END) as "Offline Mismatch Count"
FROM TableName
GROUP BY Name

Related

SQL CASE WHEN Date

I am trying to create a table that gives me the number of users that have accessed an app within the last 3 months as follows:
last 3 months: (number)
last 2 months: (number)
last month: (number)
the number is coming from another table result that has the value day_p which indicates the day the unique user accessed the app. I just want to place them within the month frame. Below is the code I am trying to run, but I keep getting an error that name is expected, but I have named them as 'Three_Month', 'Two_Month', 'Last_Month'.
CREATE TABLE Last_Access AS
SELECT
count(CASE WHEN t1.day_p BETWEEN '20210518' AND '20210618' THEN 1 END) AS 'THREE_MONTH',
count(CASE WHEN t1.day_p BETWEEN '20210619' AND '20210718' THEN 1 END) AS 'TWO_MONTH',
count(CASE WHEN t1.day_p BETWEEN '20210719' AND '20210819' THEN 1 END) AS 'LAST_MONTH',
End
FROM Result AS t1;
Any suggestions / tips would be appreciated! :)
To create a TABLE you need to give your variables names. You could also attach a label to the variable if you wanted. You don't need to include the label= keyword, but I find it makes it clearer what the string is there for.
CREATE TABLE Last_Access AS
SELECT
count(CASE WHEN t1.day_p BETWEEN '20210518' AND '20210618' THEN 1 END)
AS THREE_MONTH LABEL='THREE_MONTH'
,count(CASE WHEN t1.day_p BETWEEN '20210619' AND '20210718' THEN 1 END)
AS TWO_MONTH LABEL='TWO_MONTH'
FROM Result AS t1
;
PS Don't put continuation characters at the end of the lines. Places them at the start of the line and you will be less likely to include an extra one like in your example because they will be more visible to the humans reading the code.

Turn row with 2 different value into 2 column oracle

I'm having trouble tunring a row with only 2 possible value (heads or tails for example ) into 2 column. I found Pivot but it doesn't seems to fit the problem.
Here's a little shema to help you understand what i'm trying to do :
Into this :
It's probably not very complicated yet I can't find the good words to explain my problem to google so he give's me the right solution !
You can use aggregation:
select name, date,
sum(case when type = 'Heads' then result else 0 end) as heads,
sum(case when type = 'Tails' then result else 0 end) as tails
from t
group by name, date;

Count number of + and - values based on another column

I am trying to get a simple count of all the negative and positive values of a specific column on my database. I want to be able to count these values based on another column. What would be the best way to go about handling this problem.
enter image description here
Something like this:
SELECT
SUM(CASE WHEN column > 0 THEN 1 ELSE 0 END) as count_positive,
SUM(CASE WHEN column < 0 THEN 1 ELSE 0 END) as count_negative
FROM
table
You must put the column name, table name, and decide whether 0 is positive, negative or excluded (my choice)

SQL How to Count Number of Specific Values in a Row

Lets say I have a table that has 50 Fields. 20 of those fields can contain the Value "YES" "NO" or "N/A". How do I query the number of "YES"s for a given row?
You write a long statement that adds up the values:
select ((case when value1 = 'Yes' then 1 else 0 end) +
(case when value2 = 'Yes' then 1 else 0 end) +
. . .
(case when value50 = 'Yes' then 1 else 0 end)
) as NumYesses
This would be much easier if you normalized the data, so each value was in a separate row. You would do this by having a separate table, called a junction or association table.
Also, you can generate this code in a spreadsheet, such as Excel, by using formulas on the columns names (or by writing a query that uses metadata in your database).
Note: this is generic ANSI SQL, because you don't specify the database. There may be some shortcuts to writing the code in different databases.

select sum(a), sum(b where c=1) from db; sql conditions in select statement

i guess i just lack the keywords to search, but this is burning on my mind:
how can i add a condition to the sum-function in the select-statement like
select sum(a), sum(b where c=1) from db;?
this means, i want to see the sum of column a and the sum of column b, but only of the records in column b of which column c has the value 1.
the output of heidi just says "bad syntac near WHERE". may there be any other way?
thanks in advance and best regards from Berlin, joachim
The exact syntax may differ depending on the database engine, however it will be along the lines of
SELECT
sum(a),
sum(CASE WHEN c = 1 THEN b ELSE 0 END)
FROM
db
select sum(case when c=1 then b else 0 end)
This technique is useful when you need a lot of aggregates on the same set of data - you can query the entire table without applying a where filter, and have a bunch of these which give you aggregated data for a specific filter.
It's also useful when you need a lot of counts based on filters - you can do sums of 1 or 0:
select sum(case when {somecondition} then 1 else 0 end)