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;
Related
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;
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 10 months ago.
Improve this question
I need to copy one value of a row to another by using its symbol (group id). Another row is declared with a different language id.
Example table:
ID
SYMBOL
IDLANGUAGE
CATEGORIES
0
T1
0
✔ good-category1
1
T1
1
bad-category1
2
T2
0
✔ good-category2
3
T2
1
bad-category2
4
T3
0
✔ good-category3
5
T3
1
bad-category3
What i would expect a query to do:
ID
SYMBOL
IDLANGUAGE
CATEGORIES
0
T1
0
✔ good-category1
1
T1
1
✔ good-category1
2
T2
0
✔ good-category2
3
T2
1
✔ good-category2
4
T3
0
✔ good-category3
5
T3
1
✔ good-category3
What would be the best approach to this problem?
You could use an update join here:
UPDATE yourTable t1
INNER JOIN yourTable t2
ON t2.SYMBOL = t1.SYMBOL
SET t1.CATEGORIES = t2.CATEGORIES
WHERE t2.IDLANGUAGE = 0 AND t1.IDLANGUAGE = 1;
This approach is called a self join. For each pair of records having the same symbol, we match the correct category with the incorrect category, and then replace the latter's value with the former.
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
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 7 years ago.
Improve this question
I have a table tmp.
Query:
select * from tmp
I want the result in following way:
customer_id | subscriber_id | totalSubscribers
320 | 433 | 3
320 | 434 | 3
Can you tell me how to achieve this?
Here is solution for you question
SELECT customer_id , subscriber_id , count(*) AS totalSubscribers
FROM `tmp` GROUP BY 1,2
or
SELECT customer_id , subscriber_id , count(*) AS totalSubscribers
FROM `tmp` GROUP BY customer_id , subscriber_id
Here is screnshot for executed query
Here is the answer
SELECT customer_id , subscriber_id , count(*) as [TOTAL]
FROM tmp
GROUP BY customer_id , subscriber_id
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