Turn row with 2 different value into 2 column oracle - sql

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;

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.

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)

How to get multiple counts based on different criterias

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

SQL-Server - Ability to pass subset parameter in the select?

I'm trying create a query that will output a total number, as well as a subset of the total number in SQL-Server. I can think of a way to do this via subqueries, but that seems like a ton of work. Is there a faster way to write this query?
Table name: orders
OrderID Date Type OrderSize
1 1/1/2012 Electronics $282.02
2 1/1/2012 Electronics $1,000.56
3 1/1/2012 Books $17.25
4 1/1/2012 Books $10.00
What I am trying to output would look like this:
Date ElectronicOrders ElectronicOrderSize BookOrders BookOrderSize
1/1/2012 2 $1,282.58 2 $27.25
I could create a temp table, then run 2 update queries - 1 WHERE Type = 'Electronics' and 1 WHERE Type = 'Books'.
What I have seen in some programming languages, such as R, is the ability to subset a variable. Is there a way for me to say something like:
count(OrderID, Type = 'Electronics) as ElectronicOrders, sum(OrderSize, Type = 'Electronics') as ElectronicOrderSize
Or am I stuck with subqueries and UPDATE queries?
I haven't ever gotten the new PIVOT syntax to make sense in my head but you can do a pivot table by grouping, and taking aggregate functions in a case statement.
select [date], sum( case when type = 'Electronics' then (ordersize) else 0 end) AS ElectronicsSum,
sum( case when type = 'Electronics' then 1 else 0 end) AS ElectronicsCount,
sum( case when type = 'Books' then (ordersize) else 0 end) AS BooksSum,
sum( case when type = 'Books' then 1 else 0 end) AS BooksCoumt
from orders
group by [date]
I put a fiddle thing up to test it out. If Aaron B. posts up a solution, give him the answer credit, I might not have even recognized the pivotyness of it.

How to count two fields by using Select statement in SQL Server 2005?

Total records in table are 10.
Select count(ID) from table1 where col1 = 1 (Result is 8)
Select count(ID) from table1 where col1 = 0 (Result is 2)
So its a same table but count is based on different condition. How am i gonna get two results (counts) using one select statement?
Also Performance is a big concern here.
PS: I am using Stored procedure...
EDIT:
I wanna clear the above query is just a part of a big SP logic (for me at least). Since i got these following answers, it gave another idea to achieve it in different way. My above question is a bit changed now.....Please help here? Its a same col (bool type) with true or false state.
Use CASE:
SELECT
SUM(CASE col1 WHEN 1 THEN 1 ELSE 0 END) AS Count1,
SUM(CASE col1 WHEN 0 THEN 1 ELSE 0 END) AS Count0
FROM table1
You should use subselects or UNIONS, I don't see the other way...