I have searched all over the internet and I have had no luck in finding the answer to my question. I hope this has not specifically been posted yet, but if you can point to where i can find the answer I would appreciate it.
I was able to take the following table data below:
id lead_id form_id field_number value
1 1 1 1 Hosker
2 1 1 7 b**********#yahoo.com
3 1 1 6 Hyundai Tournament of Champions
4 1 1 3 Adam Scott
5 1 1 4 Harris English
6 1 1 5 2014-01-02 23:59:47
7 1 1 8 5b409692-e9ed-486e-8d77-7d734f1e023d
And get the result below using this query string:
SELECT id, lead_id, form_id,
MAX(case when field_number = 1 then value end) username,
MAX(case when field_number = 7 then value end) email,
MAX(case when field_number = 6 then value end) tournament_name,
MAX(case when field_number = 3 then value end) primary_golfer,
MAX(case when field_number = 4 then value end) backup_golfer,
MAX(case when field_number = 5 then value end) date,
MAX(case when field_number = 8 then value end) tournament_id
FROM `wp_rg_lead_detail` GROUP BY lead_id
id lead_id form_id field_number value value value value value value value
1 1 1 1 Hosker b**********#yahoo.com Hyundai Tournament of Champions Adam Scott Harris English 2014-01-02 23:59:47 5b409692-e9ed-486e-8d77-7d734f1e023d
I just want that query string to only pull the related data when the value of the tournament_id column equals a specific value. How would I do that?
try this query. I think you can use the HAVING clause.
SELECT id, lead_id, form_id,
MAX(case when field_number = 1 then value end) username,
MAX(case when field_number = 7 then value end) email,
MAX(case when field_number = 6 then value end) tournament_name,
MAX(case when field_number = 3 then value end) primary_golfer,
MAX(case when field_number = 4 then value end) backup_golfer,
MAX(case when field_number = 5 then value end) date,
MAX(case when field_number = 8 then value end) tournament_id
FROM `wp_rg_lead_detail` GROUP BY lead_id
HAVING tournament_id = '5b409692-e9ed-486e-8d77-7d734f1e023d'
sqlFiddle In the sqlFiddle I have sample data for 2 different tournament_id and the HAVING clause is applied so that only values for that tournament_id would be returned, you can try removing the HAVING and see that it returns 2 rows.
Your Group By is not standard since you're selecting id and form_id as well. I suggest you drop the the id or use MIN(id) or MAX(id) and GROUP BY lead_id,form_id
If I understand what you're asking, try:
SELECT * FROM TABLE_NAME
WHERE tournament_id = specific_value;
Related
Trying to create a mysql query where it count the names of the filters database. Successful, getting names from a certain period is the main goal.
$query = query("
SELECT count(CASE WHEN filter='冬越しする(現在地検索)' THEN 1 END) as filter,
count(CASE WHEN filter='開花期で選ぶ' THEN 1 END) as filter2,
count(CASE WHEN filter='日当たりで選ぶ' THEN 1 END) as filter3,
count(CASE WHEN filter='株幅で選ぶ' THEN 1 END) as filter4,
count(CASE WHEN filter='背丈で選ぶ' THEN 1 END) as filter5,
count(CASE WHEN filter='機能、タイプで選ぶ' THEN 1 END) as filter6,
count(CASE WHEN filter='ビギナーおすすめ' THEN 1 END) as filter7 FROM
filter_clicked WHERE filter_date < DATEADD(day, -30, GETDATE())");
you've to user now()-now()-interval 30 day - what you've used it is sql server function
$query = query("
SELECT count(CASE WHEN filter='冬越しする(現在地検索)' THEN 1 END) as filter,
count(CASE WHEN filter='開花期で選ぶ' THEN 1 END) as filter2,
count(CASE WHEN filter='日当たりで選ぶ' THEN 1 END) as filter3,
count(CASE WHEN filter='株幅で選ぶ' THEN 1 END) as filter4,
count(CASE WHEN filter='背丈で選ぶ' THEN 1 END) as filter5,
count(CASE WHEN filter='機能、タイプで選ぶ' THEN 1 END) as filter6,
count(CASE WHEN filter='ビギナーおすすめ' THEN 1 END) as filter7 FROM
filter_clicked WHERE filter_date < date(now())-interval 30 day")
I'm now doing four separate queries to select the information i need.
f.e.
SELECT value FROM data where sensor=123 AND value_id=a
SELECT value FROM data where sensor=123 AND value_id=b
SELECT value FROM data where sensor=123 AND value_id=c
SELECT value FROM data where sensor=123 AND value_id=d
Now i'm experementing with some code. I don't know if on my right way but is such a thing possible:
SELECT value180,value181,value182,value183
FROM (
SELECT
MAX(CASE WHEN DATE(time) = subdate(CURDATE(), 1) THEN value ELSE 0 END) as value180,
MAX(CASE WHEN DATE(time) = subdate(CURDATE(), 1) THEN value ELSE 0 END) as value181,
MAX(CASE WHEN DATE(time) = subdate(CURDATE(), 1) THEN value ELSE 0 END) as value182,
MAX(CASE WHEN DATE(time) = subdate(CURDATE(), 1) THEN value ELSE 0 END) as value183
FROM data
WHERE sensor_id = 1605850
AND value_id IN ("1.8.0","1.8.1","1.8.2","1.8.3")
) a
It would be nice to have a single query...thx for helping in advance!
note: i have to use max value of each day per sensor per value. In the above first example this max function was just left out for simplification.
This is the table how it looks like:
as you can see there are hourly taken values for each value_id.
What i need:
I need the highest value of yesterday for defined value_id's.
f.e. 1.8.0 = 3726.12, 1.8.1 = 663.69, ...
With my combined query i'm getting wrong values but the format how i want to get the values is correct:
try
SELECT value FROM data
WHERE sensor=123 AND value_id IN ('a', 'b', 'c' , 'd');
or
SELECT value FROM data
WHERE sensor=123 AND value_id IN (SELECT DISTINCT value_id FROM data);
After a short walk through the nature to take a pause i was able to solve my problem.
SELECT
(SELECT MAX(CASE WHEN DATE(time) = subdate(CURDATE(), 1) THEN value ELSE 0 END) FROM data WHERE sensor_id = 1605850 AND value_id = "1.8.0") as value180,
(SELECT MAX(CASE WHEN DATE(time) = subdate(CURDATE(), 1) THEN value ELSE 0 END) FROM data WHERE sensor_id = 1605850 AND value_id = "1.8.1") as value181,
(SELECT MAX(CASE WHEN DATE(time) = subdate(CURDATE(), 1) THEN value ELSE 0 END) FROM data WHERE sensor_id = 1605850 AND value_id = "1.8.2") as value182,
(SELECT MAX(CASE WHEN DATE(time) = subdate(CURDATE(), 1) THEN value ELSE 0 END) FROM data WHERE sensor_id = 1605850 AND value_id = "1.8.3") as value183
that was all.
I want to do math in a query, and was wondering if its better to do it in PHP or MYSQL.
Also, if I choose MYSQL can anyone help me with the query.
So far I have
SELECT COUNT(*) as total, booker, appdate,
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) book,
SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) tot
FROM appts WHERE WEEK(app_date)= WEEK(CURDATE()) GROUP BY booker
I want one more stat from this query.
I want to do book / (book+tot)
But obviously only if book!=0 or tot!=0, since obviously I don't want to divide anything by zero.
Is there a way to do this in a MYSQL query??
I want my output to be.....
book | 14
tot | 25
hold | 35%
Id also like to ORDER BY the hold percent from highest to lowest. Is this possible????
You can achieve what you ask for using a subquery, like this:
SELECT *, IF(book + tot, 100*book/(book + tot), NULL) AS hold
FROM (
SELECT COUNT(*) as total, booker, appdate,
SUM(status='DNS') book, SUM(status!='DNS') tot
FROM appts WHERE WEEK(app_date)= WEEK(CURDATE()) GROUP BY booker
) AS subquery
ORDER BY hold DESC
Note that in several places I'm using the fact that MySQL uses numbers for logical values. So you can sum up conditions without CASE, and you can write a formula for IF without <> 0 check.
Naive method:
SELECT
COUNT(*) as total,
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) book,
SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) tot,
IF( ( SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) +
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) ) = 0,
0,
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) /
( SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) +
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) )
) AS hold
FROM appts
WHERE WEEK(app_date) = WEEK(CURDATE()) GROUP BY booker
ORDER BY hold;
Or in order not to repeat your aliases, use a subquery:
SELECT *, IF (book + tot = 0, 0, book / (book + tot) * 100)
FROM (
SELECT
COUNT(*) as total,
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) book,
SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) tot,
FROM appts
WHERE WEEK(app_date) = WEEK(CURDATE()) GROUP BY booker
) AS subq
ORDER BY hold;
Or, more cleverly :) (book + tot = total)
SELECT *, IF (total = 0, 0, book / total * 100)
FROM (
SELECT
COUNT(*) as total,
SUM(CASE WHEN status='DNS' THEN 1 ELSE 0 END) book,
SUM(CASE WHEN status!='DNS' THEN 1 ELSE 0 END) tot,
FROM appts
WHERE WEEK(app_date) = WEEK(CURDATE()) GROUP BY booker
) AS subq
ORDER BY hold;
And, just for fun, the hackishly compactest form:
SELECT *, COALESCE(book / total * 100, 0) AS hold -- a division by 0 returns NULL
FROM (
SELECT
COUNT(*) total,
SUM(status='DNS') book, -- boolean "true" is internally integer "1"
SUM(status!='DNS') tot,
FROM appts
WHERE WEEK(app_date) = WEEK(CURDATE()) GROUP BY booker
) AS subq
ORDER BY hold;
I have 5 different columns with over 100 rows each having an assortment of numbers.
I have been able to count all values from column 1 that equal 3
I want to be able to count all the number 3s from all 5 different columns and add them
$countquery = "SELECT COUNT(*) FROM PowerBall WHERE W1=3";
$countresult = mysql_query($countquery) or die(mysql_error());
while($countrow = mysql_fetch_array($countresult)) {
echo "<br />";
echo "There are ". $countrow['COUNT(*)']."-3s";
}
You can use an aggregate function with a CASE expression to get the total number of 3 values in all of your columns.
Select
sum(case when col1 = 3 then 1 else 0 end) TotalCol1,
sum(case when col2 = 3 then 1 else 0 end) TotalCol2,
sum(case when col3 = 3 then 1 else 0 end) TotalCol3,
sum(case when col4 = 3 then 1 else 0 end) TotalCol4,
sum(case when col5 = 3 then 1 else 0 end) TotalCol5
from PowerBall
If you want this in one column, then you can use:
select TotalCol1 + TotalCol2 + TotalCol3 + TotalCol4 + TotalCol5
from
(
Select
sum(case when col1 = 3 then 1 else 0 end) TotalCol1,
sum(case when col2 = 3 then 1 else 0 end) TotalCol2,
sum(case when col3 = 3 then 1 else 0 end) TotalCol3,
sum(case when col4 = 3 then 1 else 0 end) TotalCol4,
sum(case when col5 = 3 then 1 else 0 end) TotalCol5
from PowerBall
) src
Or even:
select sum(Total)
from
(
Select count(col1) Total
from PowerBall
where col1 = 3
union all
Select count(col2)
from PowerBall
where col2 = 3
union all
Select count(col3)
from PowerBall
where col3 = 3
union all
Select count(col4)
from PowerBall
where col4 = 3
union all
Select count(col5)
from PowerBall
where col5 = 3
) src
SELECT SUM(CASE WHEN COL1 = 3 THEN 1 ELSE 0 END) AS 'Col1',
SUM(CASE WHEN COL1 = 3 THEN 1 ELSE 0 END) AS 'Col2',
....
FROM PowerBall WHERE W1 is not null
I want to fetch 2 coulmns count and do their total as a new column.
How can I do this?
i wrote this query, but this is returning wrong total.
SELECT count(case when `status`='1' then 1 else 0 end) AS HOT,
count(case when `status`='5' then 1 end)
AS Special_Case,count(case when 1=1 then 1 end) AS TOTAL
FROM `tbl_customer_conversation` group by
date(`dt_added`),user_id
COUNT will only give the times a record is matched, which in your query will always return 1. Because the values can either be 1 or 0. So count(1) is also 1 and count(0) is also 1.
AS, you want the total number of HOT cases and SPECIAL_CASE you have to use SUM.
SELECT
SUM(case when `status`='1' then 1 else 0 end) AS HOT,
SUM(case when `status`='5' then 1 end) AS Special_Case,
SUM(case when `status` = '1' or `status` = '5' then 1 end) AS TOTAL
FROM `tbl_customer_conversation`
group by date(`dt_added`),user_id