I'm trying to change the values of 150 columns to the following;
'0 = Not provided'
' 1 = Yes '
' 2 = No '
I was able to do this using a case statement for each column. But the problem is it creates puts everything into one column. Is there a way to do it for each individual column without writing 150 case statements? The columns need to be in a specific order.
example:
SELECT CASE
WHEN Answer.Question1_ID is null THEN 'Not Provided'
WHEN Answer.Question1_ID = 1 THEN 'Yes'
WHEN Answer.Question1_ID = 2 Then 'No'
End as 'Question1',
CASE
WHEN Answer.Question2_ID is null THEN 'Not Provided'
WHEN Answer.Question2_ID = 1 THEN 'Yes'
WHEN Answer.Question2_ID = 2 Then 'No'
End as 'Question2'
.
.
.
From Answer
Is there a way to do it for each individual column without writing 150 case statements?
No.
You can use a program to write the case statements if need be.
Related
Parameter Values
I am trying to display all the records with any status. When the user select 'AllLogs' from the dropdwon in report.
But when I run the report it is excluding the records with blank
status.
How can i pull the records with any status(blank,NULL,Approved,etc) ?
Label: Status
Value: %
I have also enabled NULL and Blank values in the parameter properties.
This is my where clause.
( ( l.Status LIKE '%' + #status + '%' ) OR
( l.Status LIKE #status ) OR
( LTRIM(RTRIM(l.Status))='') OR
( l.status IS NULL ) )
The most surefire way to include all records is to simply not filter in your where, which can be achieved with a short-circuiting case statement:
where case when #status = '%' -- This can be any value, such as 'All'
then 1
else case when l.Status = #status
then 1
else 0
end
end = 1
Whilst this is a little verbose, it has the added benefit of not actually running the comparison if your All option is selected, which will improve query performance.
Iam struck up with a query. When i edit a data in Table A, it needs to check if the same data is present in Table B, If data is present in Table B, just ignore updation, if data is empty or Null, then the data from Table A needs to Update in Table B. With the following query iam almost achieving it but the issue is When the Data is present in Table B, its just deleting the data. It should actually ignore it when data is present. A small issue with the case statement i guess. Pls help me on this.
$strSQLInsert2 = "UPDATE Table B
SET
`tender_intendername` = CASE WHEN `tender_intendername`='' or `tender_intendername` IS NULL
THEN '".$values["intendername1"]."' END,
`no_of_participants` = CASE WHEN `no_of_participants`='' Or `no_of_participants` IS NULL
THEN '".$values["no_of_participants"]."' END
WHERE tender_id=" . $values["tender_id"];
You have to add an ELSE to the CASE expression:
UPDATE Table B
SET `tender_intendername` = CASE
WHEN `tender_intendername`='' or
`tender_intendername` IS NULL
THEN '".$values["intendername1"]."'
ELSE `tender_intendername` END,
`no_of_participants` = CASE
WHEN `no_of_participants`='' Or
`no_of_participants` IS NULL
THEN '".$values["no_of_participants"]."'
ELSE `no_of_participants` END
WHERE tender_id=" . $values["tender_id"];
The ELSE statement sets each field equal to itself, hence it guarantees that the field will not change when the field is not empty and not NULL.
I solved it. But need to write two queries to achieve the result.
$strSQLInsert2 = "UPDATE `TableB`
SET
`tender_intendername` = '".$values["intendername1"]."'
WHERE
`tender_intendername` = ''
OR `tender_intendername` IS NULL AND tender_id=" . $values["tender_id"];
I'm about to build some sort of function or query where I can check if a certain record already exists in the database. The following rules apply:
The table has 6 columns
My yet-to-build-query has access to a complete row-object (all 6 values)
This query should find each row with at least 4 out of 6 corresponding values from the object I passed
Using MySQL
Is it even possible to build a query like this? My goal is to have a function which can return true if it's likely that a row like the passed object is already existing in the database.
Is my only option to make a query with multiple where-statements (where I try for each combination 4 different values)?
pseudo:
function getSimilarRow(Row_Object $row)
{
//select *
//from table_x
//where 4 out of 6 properties from object $row apply
}
You could use a case statement in the where clause for each property you are trying to match. If it meets the criteria then give the case statement a value of 1; if it doesn't then give it 0. The sum of the cases should then be >= 4.
I'm not that familiar with MySQL but the following will work (I knocked up a quick SQL Fiddle to show it working):
select * from SomeTable where
(case when propertyOne = 'value1' then 1 else 0 end) +
(case when propertyTwo = 'value2' then 1 else 0 end) +
(case when propertyThree = 'value3' then 1 else 0 end) +
(case when propertyFour = 'value4' then 1 else 0 end) +
(case when propertyFive = 'value5' then 1 else 0 end) +
(case when propertySix = 'value6' then 1 else 0 end) >= 4
Obviously you could change your logic in each clause if you'd prefer them to be likes or anything. You could even apply a weighting to each column by using something other than just 1 if you needed to get really creative.
Working on a view that pulls from two table however in one table I need to select either one field or another depending on a third..it's the if else that has me stubbed.
Create view as
select
pens.PartNo,
pens.Title,
ranges.weight
if(pens.SpeacialOffer = 1 then pens.offer as Price else pens.Price)
from
pens, ranges
where
pens.penrange = ranges.id;
If the specialoffer is falged the the view needs to pull in the offer else it needs to pull in the Price.
What you need is a CASE operator:
CASE
WHEN condition
THEN value_a
ELSE value_b
END
So in your case:
CASE
WHEN pens.SpeacialOffer = 1
THEN pens.offer
ELSE pens.price
END
This replaces the entire column definition in your SELECT statement, so the whole view becomes:
Create View as
Select
pens.PartNo,
pens.Title,
ranges.weight,
Case
When pens.SpeacialOffer = 1
Then pens.offer
Else pens.price
End as Price
From
pens, ranges
Where
pens.penrange = ranges.id;
Use CASE, also converted the query to explicit join instead of implicit join
select pens.PartNo,
pens.Title,
ranges.weight,
(Case when
pens.SpeacialOffer = 1 then
pens.offer else pens.Price
end ) as Price
FROM pens,
JOIn ranges
ON pens.penrange = ranges.id;
Here's one way:
Create view as select
pens.PartNo,
pens.Title,
ranges.weight,
(pens.SpeacialOffer * pens.offer + (1 - pens.SpeacialOffer) * pens.price) as Price
from
pens,
ranges
where
pens.penrange = ranges.id;
I have a table that, due to the third party system we are using, sometimes has duplicate data. Since the model uses an EAV method there's no way to filter this the "right" way, so I am aggregating the data into a View - I know this is a data collection problem but it's easier for me to fix it on the display end than go through this system and potentially break existing data and forms. I need to check one of two fields to see if one or both are entered, but only pick one (otherwise the name displays twice like this: "John,John" instead of just "John"). Here's my code for the relevant part:
group_concat(
(
case when (`s`.`fieldid` = 2) then `s`.`data`
else
case when (`s`.`fieldid` = 35) then `s`.`data`
else NULL end
end
) separator ','),_utf8'') as first_name
If both fieldid 2 and fieldid 35 are entered, I would expect this to just return the value from fieldid = 2 and not the value from fieldid = 35, since the Else clause shouldn't execute when the original case when is true. However it's grabbing that, and then still executing the case when inside of the else clause?
How can I fix this code to give me either fieldid = 2 OR fieldid = 35, but avoid globbing them both together which results in the name being duplicated?
EDIT
Here is the table structure:
table: subscribers_data
subscriberid (int) fieldid (int) data (text)
It uses an E-A-V structure so a sample record might be:
subscriberid fieldid data
1 2 John
1 3 Smith
1 35 John
1 36 Smith
with fieldid 2 and 35 being the custom field "First Name" (defined in a separate table) and fieldid 3 and 36 being "Last Name".
Here is the full view that I'm using:
select `ls`.`subscriberid` AS `id`,
left(`l`.`name`,(locate(_utf8'_',`l`.`name`) - 1)) AS `user_id`,
ifnull(group_concat((
case when (`s`.`fieldid` = 2) then `s`.`data`
when (`s`.`fieldid` = 35) then `s`.`data`
else NULL end) separator ','),_utf8'') AS `first_name`,
ifnull(group_concat((
case when (`s`.`fieldid` = 3) then `s`.`data`
when (`s`.`fieldid` = 36) then `s`.`data`
else NULL end) separator ','),_utf8'') AS `last_name`,
ifnull(`ls`.`emailaddress`,_utf8'') AS `email_address`,
ifnull(group_concat((
case when (`s`.`fieldid` = 81) then `s`.`data`
else NULL end) separator ','),_utf8'') AS `mobile_phone`,
ifnull(group_concat((
case when (`s`.`fieldid` = 100) then `s`.`data`
else NULL end) separator ','),_utf8'') AS `sms_only`
from ((`list_subscribers` `ls`
join `lists` `l` on((`ls`.`listid` = `l`.`listid`)))
left join `subscribers_data` `s` on((`ls`.`subscriberid` = `s`.`subscriberid`)))
where (left(`l`.`name`,(locate(_utf8'_',`l`.`name`) - 1)) regexp _utf8'[[:digit:]]+')
group by `ls`.`subscriberid`,`l`.`name`,`ls`.`emailaddress`
The view is being used as the Model for a Ruby on Rails application, so I'm using some creative hacking to fake out a "user_id" that Rails expects (we name the field list.name in the Lists table using a numeric ID that our front-end Rails app generates when we add a new user, so I'm extracting just this number to make the view look like a Rails-convention database table)
I am not a mysql guy, but in a sql server case statement, you could do it without the first 'else'
case
when fieldid = 2 then data
when fieldid = 35 then data
else null
end
Also, you seem to be returning the same 'data' field in both cases
Anything inside group_concat() doesn't have a way to see the context in which it's running. So, you have have two rows in a single group, one with fieldid=2 and second with fieldid=35, it will do the following:
processing row with fieldid=2...
s.fieldid = 2 is true, return s.data
processing row with fieldid=35...
s.fieldid = 2 is false, try the else part
s.fieldid = 35 is true, return s.data
This explains why is "John" returned multiple times. The only way to fix it is to run a different query outside of group_concat().
EDIT:
Ih you really have to do it this way, use something like this instead:
SELECT ...
min(CASE WHEN s.fieldid IN (2,35) THEN s.data ELSE NULL END) AS first_name
...
Alternatively you can do group_concat(DISTINCT ...) if the two values can't be different (otherwise you would get e.g. "John,Johny"). Why do you have two values for first_name/last_name though?