I currently have such a query:
SELECT
sec_to_time(avg(t1.sessiontime)) as aloc,
count(*) as calls
FROM
table1 AS t1
inner join
table2 as t2 ON t1.destination = t2.prefix
WHERE
t1.card_id = '101'
AND
t1.terminatecauseid = 1
group by t1.destination
Example result:
The 'calls' data is bound to 't1.terminatecauseid = 1' (meaning only answered calls)
I'd like to have a percentage of answered calles from the total calls made.
the same query without the condition (t1.terminatecauseid = 1) will give me the total calls made.
I'd like to know what is the best way to add another column called 'Average Success Rate' that will do:
total-calls*successful-calls/100
Is a subquery what's needed here? or a brand new and different query?
SELECT
sec_to_time(avg(t1.sessiontime)) as aloc,
sum(t1.terminatecauseid = 1) * 100 / count(*) as Average_Success_Rate,
sum(t1.terminatecauseid = 1) as calls
FROM
table1 AS t1
inner join
table2 as t2 ON t1.destination = t2.prefix
WHERE
t1.card_id = '101'
group by t1.destination
Related
I'm trying to find out if I can make one query to out of three I have now.
$sql = "SELECT *
FROM joblist WHERE customer = :customer"; // this is a bindParam
$sql_val = "SELECT a.customer, SUM(a.value) AS tvalue
FROM joblist AS a
INNER JOIN joblist AS b
ON a.customer = b.customer
GROUP BY a.customer";
$sql_bal = "SELECT *
FROM (SELECT SUM(balance) AS tbalance
FROM joblist GROUP BY customer) AS total_balance
WHERE tbalance = :tbalance";
The table that I am using is customer and following is are the columns.
ID - work_order - customer - description - value - balance - status - notes
I'm using php PDO and HTMl of course.
Basically, I have a search function used to query a company. The result are displayed, but I need the totals for the value and balance columns. The only thing that really works is pulling in the data from the table. I've had no success at getting the totals for value and balance.
Current code page
May not understand your question well so I'll make some guess:
SELECT
t1.*,
t2.tvalue,
t3.tbalance
FROM
(SELECT
*
FROM
joblist
WHERE customer = :customer) t1
JOIN
(SELECT
a.customer,
SUM(a.value) AS tvalue
FROM
joblist AS a
GROUP BY a.customer) t2
ON t1.customer = t2.customer
JOIN
(SELECT
b.customer,
SUM(b.balance) AS tbalance
FROM
joblist AS b
GROUP BY b.customer
HAVING tbalance = :tbalance) t3
ON t3.customer = t1.customer;
another annoying student here!
Today I spend hours trying to combine (select) 2 already joined SQL outputs + the ID of the original table in a single table output. which ultimately resulted in this query:
SELECT * FROM(
SELECT fd1.User_idUser,avg(fd1.caloryIntake)
AS 'workdays'
FROM fact_dailysnapshot fd1
INNER JOIN dim_day dd1 ON dd1.DATE_SK = fd1.DATE_SK
WHERE dd1.weekend_ind = 'N'
GROUP BY fd1.User_idUser
ORDER BY fd1.User_idUser) A,
(SELECT avg(fd1.caloryIntake) AS 'weekend'
FROM fact_dailysnapshot fd1
INNER
JOIN dim_day dd1 ON dd1.DATE_SK = fd1.DATE_SK
WHERE dd1.weekend_ind = 'Y'
GROUP BY fd1.User_idUser
ORDER BY fd1.User_idUser) B;
Which translates into…
Now this is a false result, the second column gives an almost constant value for all user entries. I think this must be solved with some kind of EXTRA join but I literally ran out of ideas. Thanks in advance..!
Your JOIN is missing an ON clause to relate dUser_idUser.
But, the simplest way to write the query uses conditional aggregation:
SELECT fd1.User_idUser,
avg(case when dd1.weekend_ind = 'N' then fd1.caloryIntake end) as weekday_avg,
avg(case when dd1.weekend_ind = 'Y' then fd1.caloryIntake end) as weekend_avg
FROM fact_dailysnapshot fd1 INNER JOIN
dim_day dd1
ON dd1.DATE_SK = fd1.DATE_SK
GROUP BY fd1.User_idUser
ORDER BY fd1.User_idUser;
This is one query instead of two.
If I understand correctly, this is what you are looking for:
SELECT A.User_idUser, A.workdays, B.weekend
FROM (
SELECT fd1.User_idUser, avg(fd1.caloryIntake) AS 'workdays'
FROM fact_dailysnapshot fd1
INNER JOIN dim_day dd1
ON dd1.DATE_SK = fd1.DATE_SK
WHERE dd1.weekend_ind = 'N'
GROUP BY fd1.User_idUser
ORDER BY fd1.User_idUser) A
JOIN
(SELECT fd1.User_idUser, avg(fd1.caloryIntake) AS 'weekend'
FROM fact_dailysnapshot fd1
INNER JOIN dim_day dd1
ON dd1.DATE_SK = fd1.DATE_SK
WHERE dd1.weekend_ind = 'Y'
GROUP BY fd1.User_idUser
ORDER BY fd1.User_idUser) B
ON A.User_idUser = B.User_idUser
Each query gives you all users by ID and their workdays or weekends. You need to JOIN the results of the two query on the user ID.
I have the follow sql query but when I execute It gives me a message error: Unknown column 't1.ip' in 'where clause'.
If I hard code t1.ip in subquery just for testing, It works perfectly.
UPDATE
report_a t1,
(SELECT
location.country, region.name, location.city
FROM
geoip
INNER JOIN
location
ON
geoip.locId = location.locId
INNER JOIN
region
ON
region.country = location.country
AND
region.region = location.region
WHERE
INET_ATON(t1.ip) BETWEEN startIpNum AND endIpNum
LIMIT 1) AS t2
SET
t1.country = t2.country,
t1.city = t2.city,
t1.state = t2.name;
Someone have any idea how i can do this query?
Thanks
You are looking for a join. I am a bit unclear on what the LIMIT 1 is supposed to be doing. I moved that into the outer query, although that might not be correct:
UPDATE report_a t1,
(SELECT location.country, region.name, location.city, startIpNum, endIpNum
FROM geoip INNER JOIN
location
ON geoip.locId = location.locId INNER JOIN
region
ON region.country = location.country AND
region.region = location.region
) glr
ON INET_ATON(t1.ip) BETWEEN glr.startIpNum AND glr.endIpNum
SET t1.country = t2.country,
t1.city = t2.city,
t1.state = t2.name
LIMIT 1;
As I think about it, you probably do not want the LIMIT 1. Why would there be multiple matches to the subquery?
I have the following query:
SELECT int_intrebari.id, COUNT( id_raspuns ) AS nr_raspunsuri
FROM int_intrebari, int_raspunsuri
WHERE int_intrebari.id = int_raspunsuri.id
GROUP BY id
Is it possible to update first table with nr_raspunsuri from the query, without writing a foreach statement?
You can UPDATE with JOIN like so:
UPDATE int_intrebari i1
INNER JOIN
(
SELECT id, COUNT( id_raspuns ) AS nr_raspunsuri
FROM int_intrebari
GROUP BY id
) i2 ON i1.id = i2.id
SET i1.nr_raspunsuri = i2.nr_raspunsuri
You can do it like -
update int_intrebari left join int_raspunsuri on int_intrebari.id =int_raspunsuri.id
set int_intrebari.column_to_update = int_raspunsuri.column_from_update_second_table
UPDATE
(SELECT int_intrebari.id, COUNT( id_raspuns) AS nr_raspunsuri
FROM int_intrebari, int_raspunsuri
WHERE int_intrebari.id = int_raspunsuri.id
GROUP BY id) t1,
int_raspunsuri t2
SET
t2.nr_raspunsuri=t1.nr_raspunsuri
WHERE
t1.id=t2.id
I have two tables that I believe I want to JOIN. I'm very new to this and am not completely sure…
The first table is called venues with the variables id, slug, name, etc. The second table is venue_terms with the variables id, option, venue, value. The matching variables are obviously venues.id and venue_terms.venue.
What I want to do is query venue_terms for matching values and then SELECT * FROM venues that match.
I've been working with the following query, but haven't been able to get it to work. I know INTERSECT isn't the solution, but I'm nut sure which JOIN I should use.
SELECT venue
FROM venue_terms
WHERE `option` = '1' AND `value` = '10'
INTERSECT
SELECT venue
FROM venue_terms
WHERE `option` = '2' AND `value` = '4';
I want to match those venue_terms.venue to the venues table. Can someone point me in the right direction?
UPDATE: To clarify, I'm trying to search multiple option/value combinations that ultimately have the same venue.id's. Basically, I want to able to find all of the venues where (option = 1 and value = 4) AND (option = 2 and value = 10) AND etc… where all of these are true.
You want to find venues that match conditions in two rows in table venue_terms. This can be accomplished by various methods. The most usual is by joining that table twice (another would be by a grouping query).
Here's the first way. Join twice to the venue_terms table:
SELECT v.id --- whatever columns you need
, v.slug --- from the venues table
, v.name
FROM venues AS v
INNER JOIN venue_terms AS vt1
ON vt1.venue = v.id
INNER JOIN venue_terms AS vt2
ON vt2.venue = v.id
WHERE ( vt1.option = 1 AND vt1.value = 10 )
AND ( vt2.option = 2 AND vt2.value = 4 ) ;
If you have 3 conditions, join thrice. If you have 10 conditions, join 10 times. It would be good for the efficiency of the query to have a compound index on (option, value, venue) in the terms table.
try this
SELECT venue.*, venue_terms.*
FROM venue
INNER JOIN venue_terms ON venue.id = venue_terms.venue
WHERE venue_terms.option IN ( 1 ,2)
AND venue_terms.value IN (10,4)
GROUP BY venue.id
How about this?
SELECT t1.*, t2.*
FROM venue t1 JOIN venue_terms t2
ON t1.id = t2.venue
WHERE (t2.option = 1 AND t2.value = 10)
NOTE: I believe option and value are of type INT.
If they are of type varchar then change above query to
SELECT t1.*, t2.*
FROM venue t1 JOIN venue_terms t2
ON t1.id = t2.venue
WHERE (t2.option = '1' AND t2.value = '10')
Update 1
As per your new requirement, you will just need to add that condition with OR option as shown below.
SELECT t1.*, t2.*
FROM venue t1 JOIN venue_terms t2
ON t1.id = t2.venue
WHERE
(t2.option = 1 AND t2.value = 10)
OR
(t2.option = 3 AND t2.value = 14)
This will join the two tables and print out the venues which matches the attributes (option, value) in venue_terms:
SELECT v.* FROM venue v, venue_terms vt
WHERE v.id = vt.venue
AND vt.option = 1
AND vt.value = 10