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;
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 2 years ago.
Improve this question
I am beginner in sql. Anyone please tell me the problem of my customised query.
select *
from Follow_My_Doct.tbl_hospital_registration h
INNER JOIN Follow_My_Doct.tbl_subscribed_packages s
ON s.hospitalId = h.id
AND s.active = 1
AND ( select max(sp.expireDate), sp.hospitalId
from Follow_My_Doct.tbl_subscribed_packages sp
where sp.active = 1
group by sp.hospitalId )
where h.hospital_active = 1
Error Code: 1241. Operand should contain 1 column(s)
Subscription table
hospital Id expireDate
145 2021-07-10
146 2021-06-10
147 2021-09-10
146 2021-10-10
You should put that subquery with the max and group by inside an INNER JOIN clause.
select *
from Follow_My_Doct.tbl_hospital_registration h
INNER JOIN ( select max(sp.expireDate) maxexpiredate, sp.hospitalId
from Follow_My_Doct.tbl_subscribed_packages sp
where sp.active = 1
group by sp.hospitalId ) as s
ON s.hospitalId = h.id
where h.hospital_active = 1
Since I don't have your data tables, I made up one environment to test that query by using table variables. The example below is for SQL Server, but the query works fine for MySQL, which currently I don't have installed on my machine.
declare #tbl_subscribed_packages TABLE(
hospitalId int,
active bit,
expiredate datetime
)
declare #tbl_hospital_registration table(
id int,
hospital_active bit)
Now populate tables with data:
insert #tbl_hospital_registration
values (145,1),(146,1),(147,1)
insert #tbl_subscribed_packages
values (145,1,'2021-07-10')
,(146,1,'2021-06-10')
,(147,1,'2021-09-10')
,(146,1,'2021-10-10')
Then, I test the query against these data
select *
from #tbl_hospital_registration h
INNER JOIN ( select max(sp.expireDate) maxexpiredate, sp.hospitalId
from #tbl_subscribed_packages sp
where sp.active = 1
group by sp.hospitalId ) as s
ON s.hospitalId = h.id
where h.hospital_active = 1
Note that using subquery as a view in INNER JOIN, I should add an alias name for max(expireDate) column.
The result is:
id
hospital_active
maxexpiredate
hospitalId
145
1
2021-07-10 00:00:00.000
145
146
1
2021-10-10 00:00:00.000
146
147
1
2021-09-10 00:00:00.000
147
Is that what you want?
6th line has issues. After AND you should have condition but you have sub query returns two column values
You can try the below - using row_number()
select * from
(
select *,row_number() over(partition by s.hospitalId order by expireDate desc ) as rn from
Follow_My_Doct.tbl_hospital_registration h INNER JOIN Follow_My_Doct.tbl_subscribed_packages s
ON s.hospitalId = h.id
where h.hospital_active = 1 and s.active = 1
)A where rn=1
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 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
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
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;