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;
Related
Hello i want to make a summary table where the filter is the segment and date
The columns will be the subtraction of count of two column in same table so far i have come up to this
select
case
when SEGMENT='champion'THEN SUM(SEGMENTED_DATE = '2022-10-14')-SUM(SEGMENTED_DATE='2022-10-07')
END as total_change_champion,
case
when SEGMENT='Hibernating'THEN SUM(SEGMENTED_DATE = '2022-10-14')-SUM(SEGMENTED_DATE='2022-10-07')
END as total_change_hibernate
from weekly_customer_RFM_TABLE;
This query is giving me null value for columns
select
SUM(SEGMENTED_DATE = '2022-10-14')-
SUM(SEGMENTED_DATE='2022-10-07')as total_changes_in_14th
From weekly_customer_RFM_TABLE
where
SEGMENT ='Hibernating'
This query is actually giving me the result. but when I am going to make a consolidated table using different segment subtraction i am getting null value in my result table.Kindly help me out
my sample data
which is i want to substract totalcount of 2022/10/14 - 2022/10/07 for each segment
image is not loading unfortunately
You must include the CASE expressions inside the aggregate functions:
SELECT COUNT(CASE WHEN SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-14' THEN 1 END) -
COUNT(CASE WHEN SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-07' THEN 1 END) AS total_change_champion,
COUNT(CASE WHEN SEGMENT = 'Hibernating' AND SEGMENTED_DATE = '2022-10-14' THEN 1 END) -
COUNT(CASE WHEN SEGMENT = 'Hibernating' AND SEGMENTED_DATE='2022-10-07' THEN 1 END) AS total_change_hibernate
FROM weekly_customer_RFM_TABLE;
For MySql this can be simplified to:
SELECT SUM(SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-14')-
SUM(SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-07') AS total_change_champion,
SUM(SEGMENT = 'Hibernating' AND SEGMENTED_DATE = '2022-10-14')-
SUM(SEGMENT = 'Hibernating' AND SEGMENTED_DATE='2022-10-07') AS total_change_hibernate
FROM weekly_customer_RFM_TABLE;
You may also use a WHERE clause to filter the table for only the SEGMENTs and SEGMENTED_DATEs that you want:
WHERE SEGMENT IN ('champion', 'Hibernating') AND SEGMENTED_DATE IN ('2022-10-07', '2022-10-14')
I've got a case function within my select query. Within this case I want to convert "SongLength" which is in seconds into and mm:ss format with the SEC_TO TIME function. However I get a syntax error upon putting this function in there.
select SongTitle,Artist,SongLength
case
when SongLength < 600 then
sec_to_time(SongLength)
else
sec_to_time(SongLength)
end
from Songs, Artists where Songs.ArtistId = Artists.Id
order by SongTitle;
Try SongLength to make an alias as :
select SongTitle,Artist,
(case
when SongLength < 600 then
sec_to_time(SongLength)
else
sec_to_time(SongLength)
end) as SongLength
from Songs join Artists
on ( Songs.ArtistId = Artists.Id )
order by SongTitle;
see below example works fine
select case
when 400 < 600 then
sec_to_time(400)
else
sec_to_time(300)
end as col
it returns below output
col
00:06:40
So if your datatype of column SongLength is numeric then it should work
http://www.sqlfiddle.com/#!9/835f61/150
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.
I want to make a query where where i first check the current date with the date in a column and then base on that I am writing my case. this query is working fine individually but when
i am combining them its not working.
The queires are
SELECT MONTH(CURRENT_DATE)= SUBSTRING(yearmonth,6) FROM dp;
SELECT i,
CASE
WHEN DAY(CURRENT_DATE) =1 THEN `d1_v`
WHEN DAY(CURRENT_DATE) =2 THEN `d1_v`
END VALUE
FROM dp;
combined query..
SELECT i,
CASE
WHEN((MONTH(CURRENT_DATE ))= SUBSTRING(yearmonth,6) THEN
(CASE
WHEN DAY(CURRENT_DATE) = 1 THEN `d1_v`
WHEN DAY(CURRENT_DATE) = 2 THEN `d1_v`
END VALUE)END)Y
FROM dp
please guide me
You've to delete the '(' after the THEN:
SELECT i,
CASE
WHEN ((MONTH(CURRENT_DATE )) = SUBSTRING(yearmonth,6))THEN
CASE
WHEN DAY(CURRENT_DATE) = 1 THEN `day1_value`
WHEN DAY(CURRENT_DATE) = 2 THEN `day1_value`
END
END Y
FROM dp;
The output from the sqlfiddle is 5,null right now.
Hope this work for you.
The sqlfiddle is: http://sqlfiddle.com/#!2/e59c5/10
Hi i have the following SQL question:
SELECT station_id, filling_station_status,date_created ,
case when filling_station_status="FREE" then 0
else 1 end as status
FROM efahrung.electric_station_time_status
where station_id=11
In my table have a column filling_station_status.
It can be "FREE" or "IN_USE".
I want to group elements so, that if the filling_station_status is changed (from "FREE" to "IN_USE") it will create a date range in my case, date_created.
In the next change again from ("IN_USE" to "FREE") it creates a new date range.
Thanks for a suggestions.
If you just need SQL query to generate date range in output, then try this:
Select s.station_id,
Coalesce(e.filling_station_status, s.filling_station_status) fillingStationStatus,
case e.filling_station_status
when "FREE" then 0 else 1 end status,
s.date_created startDate,
e.date_created endDate
From efahrung.electric_station_time_status s
Left Join efahrung.electric_station_time_status e
On e.station_id = s.station_id
and s.filling_station_status = 'IN_USE'
and e.filling_station_status = 'FREE'
and e.date_created =
(Select Min(date_created)
From efahrung.electric_station_time_status
Where station_id = s.station_id
and date_created > s.date_created)