Sql Result two columns - sql

I have the following table:
Full name status
ricardo 1 2
ricardo 2 4
How do I make a select to return like this:
name totalstatus1 totalstatus2 total
ricardo 2 4 6

You did not include the name of the column with the 2 and 4 but you could use something similar to this:
select name,
sum(case when status = 1 then value end) totalStatus1,
sum(case when status = 2 then value end) totalStatus2,
sum(value) Total
from yourtable
group by name;
See SQL Fiddle with Demo

Related

How to check the count of each values repeating in a row

I have two tables. Data in the first table is:
ID Username
1 Dan
2 Eli
3 Sean
4 John
Second Table Data:
user_id Status_id
1 2
1 3
4 1
3 2
2 3
1 1
3 3
3 3
3 3
. .
goes on goes on
These are my both tables.
I want to find the frequency of individual users doing 'status_id'
My expected result is:
username status_id(1) status_id(2) status_id(3)
Dan 1 1 1
Eli 0 0 1
Sean 0 1 2
John 1 0 0
My current code is:
SELECT b.username , COUNT(a.status_id)
FROM masterdb.auth_user b
left outer join masterdb.xmlform_joblist a
on a.user1_id = b.id
GROUP BY b.username, b.id, a.status_id
This gives me the separate count but in a single row without mentioning which status_id each column represents
This is called pivot and it works in two steps:
extracts the data for the specific field using a CASE statement
aggregates the data on users, to make every field value lie on the same record for each user
SELECT Username,
SUM(CASE WHEN status_id = 1 THEN 1 END) AS status_id_1,
SUM(CASE WHEN status_id = 2 THEN 1 END) AS status_id_2,
SUM(CASE WHEN status_id = 3 THEN 1 END) AS status_id_3
FROM t2
INNER JOIN t1
ON t2.user_id = t1._ID
GROUP BY Username
ORDER BY Username
Check the demo here.
Note: This solution assumes that there are 3 status_id values. If you need to generalize on the amount of status ids, you would require a dynamic query. In any case, it's better to avoid dynamic queries if you can.

Enquiry on Query in Oracle SQL

I have data as below:
Category | Type | Rank
Milk 1 1
Milk 2 2
Milk 3 3
Chocolate 1 2
Candy 1 1
Any idea to achieve the output of below with a flat SQL query:
Category
Milk
Query must satisfy the below conditions:
1. Only Type 1 and Rank 1 will be selected.
2. Only Category that has Type 1 and Type 2 will be selected.
In the sample data above, only Milk that satisfy the conditions mentioned above.
My query is below. But it's incorrect, because it will return Candy as well.
SELECT DISTINCT Category
FROM table
WHERE Type = 1 AND rank = 1
Thanks in advance!
You can try below -
DEMO
select distinct category
from table a
WHERE Type = 1 AND rank = 1
and exists
(select 1 from table b where a.category=b.category and type in (1,2)
group by category having count(distinct type)=2)
OUTPUT:
category
Milk
You can use aggregation:
select category
from t
group by category
having sum(case when type = 1 and rank = 1 then 1 else 0 end) > 0 and
sum(case when type = 2 then 1 else 0 end) > 0;
Assuming no duplicates, this can be simplified to:
select category
from t
where (type = 1 and rank = 1) or type = 2
group by category
having count(distinct type) = 2;

SQL table data horizontally using PIVOT

My SQL table is
GUID Step_ID Value
----------------------------------------------
ADFE12-ASDER-... 1 10
ADFE12-ASDER-... 2 20
ADFE12-ASDER-... 3 30
ADFE12-ASDER-... 4 160
CD4563-FG567-... 1 20
CD4563-FG567-... 2 80
Q23RT5-GH678... 1 30
Q23RT5-GH678-... 2 80
Q23RT5-GH678-... 3 20
And Expected result should be
GUID 1 2 3 4
---------------------------------------------------
ADFE12-ASDER-... 10 20 30 160
CD4563-FG567-... 20 80 NULL NULL
Q23RT5-GH678-... 30 80 20 NULL
Here I need to get the details on the basis of column whose data type is GUID. I tried using PIVOT table but getting an exception because I cannot use an aggregate function on GUID column. Is there any other alternative or approach I can use to get the above desired result.
Try this:
select [GUID],[1],[2],[3],[4]
from
(
select [GUID], Step_ID, Value
from test
) d
pivot
(
max(Value)
for Step_ID in ([1],[2],[3],[4])
) piv;
SQL FIDDLE DEMO
You could try manually pivotting, I don't have a SQL Server handy to check the behavior on a GUID but this has worked well for me in the past.
select guid,
max(case when step_ID = 1 then value else null end) step_1,
max(case when step_ID = 2 then value else null end) step_2,
max(case when step_ID = 3 then value else null end) step_3,
max(case when step_ID = 4 then value else null end) step_4
from your_table
group by guid;
HTH

SQL find total count of each type in a column

I'm learning SQL and am stumped on what should be a simple query. I have a table with the following pattern:
Id | Type
------------
1 | Red
2 | Blue
3 | Blue
4 | Red
..
I would like to write a query to return a table that counts the total number of instances of each type and returns a table with the following pattern, for example, if 'Blue' occurs in 12 rows, and 'Red' occurs in 16 rows in the table above, the result would be:
Blue | Red
-----------
12 | 16
You could do it this way:
SELECT Type, COUNT(*) FROM TABLE GROUP BY Type
If you'd like to see the Types in separate columns, you could do this:
SELECT SUM(CASE WHEN Type = 'Blue' THEN 1 ELSE 0 END) AS Blue, SUM(CASE WHEN Type = 'Red' THEN 1 ELSE 0 END) AS Red FROM TABLE
I suggest using count over partition by. Here's a code I wrote to help my company check for duplicate Technician EmployeeID's and Pincodes, including count and YES/NO columns to allow filtering in excel so they can see what corrections need to be made:
select
t.TechnicianId, t.TechnicianName, t.Pincode, t.EmployeeID
, [Pincode Count] = count(t.Pincode) over (partition by t.Pincode)
, [Duplicate Pincode?] = case count(t.Pincode) over (partition by t.Pincode) when 1 then 'NO' else 'YES' end
, [EmployeeID Count] = count(t.EmployeeID) over (partition by t.EmployeeID)
, [Duplicate EmployeeID?] = case count(t.EmployeeID) over (partition by t.EmployeeID) when 1 then 'NO' else 'YES' end
from Technicians t
group by t.TechnicianId, t.TechnicianName, t.Pincode, t.EmployeeID
order by 4

SQL QUERY MERGE TWO ROW DATA

Suppose My Database is like this :
MemberName MemberID ResultsEligibilityID
Thuso 2 1
Thuso 2 1
Maubane 3 2
Maubane 3 1
CDeveloper 5 2
CDeveloper 5 2
Now is it possible to write a query to display (The Below output) based on this:
if both ResultsEligibilityID for a single Member is 1 then Eligibile,
Otherwise Non-Eligible.
OUTPUT
MemberName MemberID ResultsEligibilityID Results
Thuso 2 1 Eligible
Maubane 3 2 Non-Eligible
CDeveloper 5 2 Non-Eligible
Thanks in advance for the help.
Please try:
select
MemberName,
MemberID,
MAX(ResultsEligibilityID) ResultsEligibilityID ,
(case when sum(case when ResultsEligibilityID=1 then 1 else 0 end)= COUNT(*)
then 'Eligible' else 'Non-Eligible' end) Results
From
YourTable
group by MemberName,MemberID