Transpose of column to row [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
SELECT
-- '<=19'AS Age_Range,
case when gender = 1 then 'male'
when gender = 2 then 'female'
end as gender_cat,
SUM(CASE WHEN hypertension= '1' THEN 1 ELSE 0 END) AS hypertension,
SUM(CASE WHEN chronic_cardiac_disease= '1' THEN 1 ELSE 0 END) AS chronic_cardiac_disease
FROM form where gender is not null
group by gender_cat
The output is coming as Gender_Cat | hypertension| chronic_cardiac_disease
I need to transpose the column to row for the desired output as: Disease | Male | Female.
Any help would be appreciated!!!

If a given patient may have both diseases, then I would recommend union all:
select 'hypertension' as disease, sum(gender = 1) as male, sum(gender = 2) as female
from form
where hypertension = 1
select 'chronic_cardiac_disease', sum(gender = 1), sum(gender = 2)
from form
where chronic_cardiac_disease = 1
In very recent MySQL versions, you would use a lateral join:
select x.disease, sum(f.gender = 1) as male, sum(f.gender = 2) as female
from form f
cross join lateral (
select 'hypertension' as disease , hypertension as flag
union all select 'chronic_cardiac_disease', chronic_cardiac_disease
) x
where x.flag = 1
group by x.disease

Related

SQL Case of IF? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
SELECT f.user_id, f.item_id, f.status, f.shipped,
CASE
WHEN f.status = 0 THEN UPDATE f SET f.status = 1 WHERE f.user_id = 1 AND f.item_id = 1
WHEN f.status = 1 THEN UPDATE f SET f.status = 0 WHERE f.user_id = 1 AND f.item_id = 1
ELSE 'ERROR'
END CASE
FROM business AS f;
Hi, I want to update the status of an item based on its value ( 0 or 1). I still got syntax error. Am I using the right conditional statements or should I use IF()? If so, how can I put another query inside the IF()?
Thank you!
If you want to update the actual data in the table, then use an UPDATE statement, not a SELECT statement:
UPDATE business
SET status =
CASE WHEN status = 1 THEN 0
WHEN status = 0 THEN 1
ELSE -1 --Likely this has to be numeric since you are setting a numeric column, right?
END
WHERE user_id = 1 AND item_id = 1;
Running this will update your business table for the record(s) where user_id = 1 and item_id=1 switching the values of status unless the current status value isn't 1 or 0, in which case it will set status to -1. Note that the string 'error' can't be used a value unless this column is a varchar or similar string-type column.
This is very simple SQL query:
UPDATE business b
SET b.status = CASE WHEN b.status = 1 THEN 0 ELSE 1 END
WHERE b.user_id = 1 AND b.item_id = 1;
SELECT * FROM business;

Sorting mysql row data to get only a specific id [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a table as shown below
id | t_id | u_id
---+------+------
1 | 2 | 2
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 1 | 1
I am trying to get all t_id with u_id of 2 but once without the t_id ever having a u_id of 1 in the history of the whole table.
I tried
SELECT
C_Name, count(*) as count
FROM tenter
WHERE C_Date = '20200127' AND L_TID = '2';
But this gives me the record of all L_TID = 2 and does not filter out those with previous record of L_tid = 1.
Expected result: get all U_ID without the previous history of L_TID = 1, it should get only those without ever having L_tid =1.
Thanks in advance.
One method is aggregation and a having clause:
select t_id
from tenter t
group by t_id
having sum(u_id = 2) > 0 and -- has "2"
sum(u_id = 1) = 0; -- does not have "1"
If you have another table of t values, then exists/not exists might be more efficient:
select t.t_id
from t
where exists (select 1 from tenter tt where tt.t_id = t.t_id and tt.u_id = 2) and
not exists (select 1 from tenter tt where tt.t_id = t.t_id and tt.u_id = 1);
I think you want not exists:
select t.*
from tenter t
where
u_id = 2
and not exists (
select 1 from tenter t1 where t1.t_id = t.t_id and t1.u_id = 1
)
You can also use aggregation, if you just want a list of t_ids. If 1 and 2 are the only possible values, you can just do:
select t_id
from tenter t
group by t_id
having min(u_id) = 2
If there are other possible values:
having max(u_id = 1) = 0 and max(u_id = 2) = 1

Group by in complex query with cases [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a table:
Visit (FromId, ToId, VisitTime)
where FromId and ToId are FKs to table
UserProfile (uid, name, age ...)
As a user with my UID I want to select all profiles I have visited or who visited me in one result set ordered by VisitTime and with the indication of the "direction of the visit".
I get data using this select:
SELECT CASE WHEN a.FromID = 'yourIDHere'
THEN c.Name
ELSE b.Name
END Name,
CASE WHEN a.FromID = 'yourIDHere'
THEN c.Age
ELSE b.Age
END Age,
a.VisitTime,
CASE WHEN a.FromID = 'yourIDHere'
THEN 'You'
ELSE 'Friend'
END DirectionOfVisit
FROM Visit a
INNER JOIN UserProfile b
ON a.FromID = b.Uid
INNER JOIN UserProfile c
ON a.ToID = c.Uid
WHERE 'yourIDHere' IN (a.FromID, a.ToID)
ORDER BY a.VisitTime
Now it prints (pseudo output)
Jack (id1) | IN |12.12.2012
Jack (id1) | IN |11.12.2012
Jack (id1) | IN |11.12.2012
Jack (id1) | OUT | 13.12.2012
Jack (id1) | OUT | 12.12.2012
Michael (id5) | IN | 5.12.2012
Michael (id5) | OUT | 6.12.2012
Michael (id5) | OUT | 5.12.2012
I would like the list to be like this:
Jack | IN | 12.12.2012 (the most recent)
Jack | OUT | 13.12.2012 (the most recent)
Michael (id5) | IN | 5.12.2012 (the most recent)
Michael (id5) | OUT | 6.12.2012 (the most recent)
I know the GROUP command would solve it but it's too complex for me (beginner).
You could use GROUP BY along with an aggregate function to get the result. Since you want the most recent date for each name and type (IN/OUT), then you can use the max() aggregate function on the date column. You will then use a GROUP BY on the other columns you want to return:
The basic syntax will be:
select
name,
type,
max(date) date
from yourtable
group by name, type;
See SQL Fiddle with Demo
If you want to return the max date with your existing query, you can just expand the query to use:
select name, age, max(VisitTime), DirectionOfVisit
from
(
SELECT CASE WHEN a.FromID = 'yourIDHere'
THEN c.Name
ELSE b.Name
END Name,
CASE WHEN a.FromID = 'yourIDHere'
THEN c.Age
ELSE b.Age
END Age,
a.VisitTime,
CASE WHEN a.FromID = 'yourIDHere'
THEN 'You'
ELSE 'Friend'
END DirectionOfVisit
FROM Visit a
INNER JOIN UserProfile b
ON a.FromID = b.Uid
INNER JOIN UserProfile c
ON a.ToID = c.Uid
WHERE 'yourIDHere' IN (a.FromID, a.ToID)
) d
group by name, age, DirectionOfVisit;
See SQL Fiddle with Demo

How to build the following sql query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 4 table.
1.Category Table.
Fields : Id , Name , Description
Data : 1 School This is school
2.CategoryMeta Table
Field : Id , CategoryId , FieldName
Data : 1 1 Phone
2 1 Address
3.Object Table
Field : Id , CategoryId , ojectName , ObjectDesc
1 1 ABC School This is a good school
4.ObjectMeta Table
Fields : Id , CategoryId , ObjectId , CategoryMetaId , FieldValue
Data : 1 1 1 1 919475864253
2 1 1 2 ABC Road.India
I want the following output from the query.I set the category Id as a parameter
ObjectId ObjectName ObjectDesc Phone Address
1 ABC School This is a good school 919475864253 ABC Road.India
I need a list of objects.Can any one help me..
Thanks in advance..
You should be able to JOIN the tables and use an aggregate function with a CASE expression to convert the rows of values into columns:
select o.id,
o.ojectname,
o.objectdesc,
max(case when cm.fieldname = 'Phone' then om.fieldvalue end) Phone,
max(case when cm.fieldname = 'Address' then om.fieldvalue end) Address
from object o
left join objectmeta om
on o.id = om.objectid
left join categorymeta cm
on om.categorymetaid = cm.id
group by o.id, o.ojectname, o.objectdesc;
See SQL Fiddle with Demo. Depending on your RDBMS that you are using you could create a dynamic SQL version of this what will get the list of fieldnames based on what is stored in your database.
This is the query, but your schema need some Improvements.
select ph.*, om2.FieldValue as Address from
(
select o.id as ObjectId, o.ObjectName, o.ObjectDesc,om1.FieldValue as Phone
from object o
join ObjectMeta om1 on o.id = om1.ObjectID
where om1.CategoryMetaID = 1
) ph
join ObjectMeta om2 on ph.ObjectId = om2.ObjectID
where om2.CategoryMetaID = 2

Oracle select column entry from another column with and without value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table that I want to get the s# where s# has p1 but not p2;
table s
s# P#
s1 p1
s1 p2
s1 p3
s1 p4
s2 p1
s2 p3
s3 p2
s3 p3
My result should be s2. The where clauses and joins I try return s1,s2 which is wrong.
Try this:
SELECT s#
FROM (SELECT s#, SUM(CASE WHEN p# = 'p1' THEN 1 ELSE 0 END) p1ct
, SUM(CASE WHEN p# = 'p2' THEN 1 ELSE 0 END) p2ct
FROM s
GROUP BY s#)
WHERE p1ct > 0
AND p2ct = 0;