I have three tables: tblFuel, tblDoXang, tblDrivingTime2. Now I want to show the fuelLevel field in tblFuel that satisfy some condition in tblDrivingTime2 and if in specific of time(base on timestamp in tblFuel) if I check there are adding fuel action in tblDoXang, I have to insert it(nhienLieu field in tblDoXang) to report that show fuelLevel above. The goal is likely:
tblFuel:
timestamp fuelLevel
123456 10
123467 8
123478 50
123489 20
tblDrivingTime2:
stopTime
123456
123478
123489
this will print:
10
50
20
and now we check if
tblDoXang
thoiGian nhienLieu
123457 15
123466 10
it will insert to result above and finally, the result will be:
10
15
10
50
20
I have written two separated queries to do these tasks:
SELECT distinct from_unixtime(F.timestamp), F.fuelLevel FROM gtse.tblFuel F
INNER JOIN gtse.tblDrivingTime2 D
ON D.accountID = F.accountID and D.deviceID = F.deviceID
where (from_unixtime(F.timestamp) between '2014-10-10 10:52:02' and '2014-10-30 10:52:02')
and F.accountID = 'vinhnghia'
and F.deviceID = '14C-00263'
and (D.reportType = '2' or D.reportType = '3')
and F.timestamp = D.stopTime
order by F.timestamp asc;
this will print the result in the first time(10,50 and 20) and this:
SELECT distinct from_unixtime(D.thoiGian), D.nhienLieu
FROM gtse.tblDoXang D
inner join gtse.tblFuel F
on D.accountID = F.accountID and D.deviceID = F.deviceID
where D.accountID = 'vinhnghia' and D.deviceID = '14C-00263'
and D.thoiGian <= F.timestamp
order by D.thoiGian asc;
will show the value(15,10). And my question is how to join two queries to show the final result?:
10
15
10
50
20
Maybe you could try with the UNION clause:
(
SELECT distinct
from_unixtime(F.timestamp) As col_1,
F.fuelLevel as col_2
FROM
gtse.tblFuel F
INNER JOIN gtse.tblDrivingTime2 D
ON D.accountID = F.accountID
and D.deviceID = F.deviceID
where
(from_unixtime(F.timestamp) between '2014-10-10 10:52:02' and '2014-10-30 10:52:02')
and F.accountID = 'vinhnghia'
and F.deviceID = '14C-00263'
and (D.reportType = '2' or D.reportType = '3')
and F.timestamp = D.stopTime
order by
F.timestamp asc
)
UNION
(
SELECT distinct
from_unixtime(D.thoiGian) as col_1,
D.nhienLieu as col_2
FROM
gtse.tblDoXang D
inner join gtse.tblFuel F
on D.accountID = F.accountID
and D.deviceID = F.deviceID
where
D.accountID = 'vinhnghia'
and D.deviceID = '14C-00263'
and D.thoiGian <= F.timestamp
order by
D.thoiGian asc;
)
I hope I understood well and this could help.
Related
I have two SQL queries that I would like to join into one:
select d.full_month, COUNT(*) amount
from fact_ticket t
join dim_queue q on t.queue_id = q.queue_id
join vt_scopes s on t.scope_id = s.scope_id
join dim_date d on t.create_date_id = d.date_id
where q.name = 'Support'
and year(GETDATE()) = YEAR(t.create_date)
and s.statusname not in ('discarded', 'closed')
group by d.full_month
order by 1;
and
select d.full_month, COUNT(*) amount
from fact_ticket t
join dim_queue q on t.queue_id = q.queue_id
join vt_scopes s on t.scope_id = s.scope_id
join dim_date d on t.create_date_id = d.date_id
where q.name = 'Support'
and year(GETDATE()) = YEAR(t.create_date)
and s.statusname in ('closed')
group by d.full_month
order by 1;
Both gives me now a result with a date column and an amount column, but I would like to get everything in one query where I would get date, amount 1, amount 2.
Is there an easy to do this?
You can use below query-
SELECT d.full_month,
COUNT(IF(s.statusname NOT IN ('discarded', 'closed'),1,NULL)) amount1,
COUNT(IF(s.statusname IN ('closed'),1,NULL)) amount2
FROM fact_ticket t
JOIN dim_queue q ON t.queue_id = q.queue_id
JOIN vt_scopes s ON t.scope_id = s.scope_id
JOIN dim_date d ON t.create_date_id = d.date_id
WHERE q.name = 'Support'
AND YEAR(GETDATE()) = YEAR(t.create_date)
GROUP BY d.full_month
ORDER BY 1;
2nd Edition: Even you can get benefit of index if exist on create_date by below query-
SELECT d.full_month,
COUNT(IF(s.statusname NOT IN ('discarded', 'closed'),1,NULL)) amount1,
COUNT(IF(s.statusname IN ('closed'),1,NULL)) amount2
FROM fact_ticket t
JOIN dim_queue q ON t.queue_id = q.queue_id
JOIN vt_scopes s ON t.scope_id = s.scope_id
JOIN dim_date d ON t.create_date_id = d.date_id
WHERE q.name = 'Support'
AND t.create_date>= DATE_FORMAT(NOW(),'%Y-01-01 00:00:00') AND t.create_date <= DATE_FORMAT(NOW(),'%Y-12-31 23:59:59');
GROUP BY d.full_month
ORDER BY 1;
Another way using sum function
SELECT
d.full_month,
SUM(s.statusname NOT IN ('discarded', 'closed')) amount,
SUM(s.statusname = 'closed') amount_closed
FROM
fact_ticket t
JOIN dim_queue q
ON t.queue_id = q.queue_id
JOIN vt_scopes s
ON t.scope_id = s.scope_id
JOIN dim_date d
ON t.create_date_id = d.date_id
WHERE q.name = 'Support'
AND YEAR(GETDATE ()) = YEAR(t.create_date)
GROUP BY d.full_month
ORDER BY 1 ;
I have two queries giving two sets of result, i want to divide one query's result with another.
Here is my first query :
SELECT COUNT(*)
FROM survey_event_answers a JOIN survey_events e ON a.eventid = e.eventid
WHERE e.event_status = 'Closed' AND
e.survey_date BETWEEN '2013-05-01' AND '2014-01-31' AND
a.response_options IN (10,11) AND a.questionid = 7
GROUP BY MONTH(e.survey_date) DESC;
Here is the result of this query :
279
443
664
743
785
1312
1085
915
231
Here is my another query :
SELECT COUNT(*),e.survey_date
FROM `survey_event_answers` a INNER JOIN survey_events e ON a.eventid = e.eventid
WHERE e.event_status='Closed' AND
e.survey_date BETWEEN '2013-05-01' AND '2014-01-31' AND
a.questionid=7
GROUP BY MONTH(e.survey_date) DESC
Here is the query result :
351
539
826
926
984
1654
1378
1165
844
I want that first row of first result set should divide by first row of second row set.
Please help me out how can i do this.
Thanks
is this what are you looking for?
SELECT rd1.r1/rd2.r2 `result`
FROM (
SELECT COUNT(*) r1,MONTH(e.survey_date) d1
FROM `survey_event_answers` a
INNER JOIN survey_events e
ON (a.eventid = e.eventid)
WHERE e.event_status='Closed' AND e.survey_date BETWEEN '2013-05-01' AND '2014-01-31'
AND a.response_options IN (10,11) AND a.questionid=7
GROUP BY MONTH(e.survey_date) DESC ) rd1
JOIN (
SELECT COUNT(*) r2, MONTH(e.survey_date) d2
FROM `survey_event_answers` a
INNER JOIN survey_events e
ON (a.eventid = e.eventid)
WHERE e.event_status='Closed' AND e.survey_date BETWEEN '2013-05-01' AND '2014-01-31'
AND a.questionid=7
GROUP BY MONTH(e.survey_date) DESC ) rd2
ON rd1.d1=rd2.d2
You could do:
SELECT SUM(IF(a.response_options IN (10,11), 1, 0))/COUNT(*)
FROM survey_event_answers a
JOIN survey_events e
ON a.eventid = e.eventid
WHERE e.event_status = 'Closed'
AND e.survey_date BETWEEN '2013-05-01' AND '2014-01-31'
AND a.questionid = 7
GROUP BY MONTH(e.survey_date) DESC;
This may not be fast (but it is faster than doing the query twice and joining the results), but probably do what you want (ratio of answers 10 or 11). You can add the MONTH(e.survey_date) as a second column.
I would like to know how can i limit the output by the number of id_offers and not the number of rows. For example
SELECT A.id_offer, T.tags
FROM offer A
INNER JOIN offer_has_tags Z
ON A.id_offer = Z.offer_id_offer
INNER JOIN tags T
ON Z.tags_id_tags = T.id_tags
WHERE state = 0
ORDER BY date
DESC LIMIT 0, 10
output:
id_offer tags
77 xx
76 xx
76 xx
75 xx
75 xx
74 xx
74 xx
73 xx
73 xx
72 xx
Edit: In this case only should be count as 6 offers.
I'm not sure if this is exactly what you want, but I think it is:
SELECT A.id_offer, T.tags
FROM offer A
JOIN offer_has_tags Z
ON A.id_offer = Z.offer_id_offer
JOIN tags T
ON Z.tags_id_tags = T.id_tags
JOIN (
SELECT DISTINCT id_offer
FROM offer
WHERE state = 0
ORDER BY date DESC
LIMIT 10
) L
ON A.id_offer = L.id_offer
or the more simple:
SELECT A.id_offer, T.tags
FROM
( SELECT *
FROM offer
WHERE state = 0
ORDER BY date DESC
LIMIT 10
) A
JOIN offer_has_tags Z
ON A.id_offer = Z.offer_id_offer
JOIN tags T
ON Z.tags_id_tags = T.id_tags
you can try this:
SELECT A.id_offer, T.tags
FROM offer A
INNER JOIN offer_has_tags Z
ON A.id_offer = Z.offer_id_offer
INNER JOIN tags T
ON Z.tags_id_tags = T.id_tags
WHERE (state = 0) AND
(A.id_offer >= 72 AND A.id_offer <= 77)
ORDER BY date
You simply have to use DISTINCT:
SELECT DISTINCT A.id_offer, T.tags
FROM offer A
INNER JOIN offer_has_tags Z
ON A.id_offer = Z.offer_id_offer
INNER JOIN tags T
ON Z.tags_id_tags = T.id_tags
WHERE state = 0
ORDER BY date
DESC LIMIT 0, 10
Please help me with this:
I have a table like:
id_feature id_product id_feature_value
1 1 50
2 1 54
5 1 67
And I want to select from this table like this:
select count(id_product) from table where (id_feature = 1 AND id_feature_value = 50) AND (id_feature = 2 AND id_feature_value = 54) AND (id_feature = 5 AND id_feature_value = 67)
my query must meet the conditions. Like having count(condition) = 3
I don't know how to write this!
Please help me! and sorry my english!
SELECT count( pf.id_product ) AS nr_product, value, fv.id_feature_value, filter
FROM `nk_category_features` cat_f
INNER JOIN `nk_feature_value` fv ON fv.id_feature = '11'
AND fv.value IS NOT NULL
INNER JOIN `nk_product_features` pf ON pf.id_feature = '11'
AND pf.id_feature_value = fv.id_feature_value
INNER JOIN `nk_product` p ON p.id_product = pf.id_product
AND p.product_active = '1'
INNER JOIN `nk_product_features` pf1 ON ( pf1.id_feature = '14'
AND (
pf1.id_feature_value = '21'
) )
WHERE cat_f.id_feature = '11'
AND filter >0
GROUP BY pf.id_feature, pf.id_product
ORDER BY abs( fv.value ) ASC
this is my query that i used now, but i don't like the solution width inner join, inner join on the same table
try this:
select id_product,count(*)
from table
where (id_feature = 1 AND id_feature_value = 50)
OR (id_feature = 2 AND id_feature_value = 54)
OR (id_feature = 5 AND id_feature_value = 67)
group by id_product
having count(*) = 3
To get just the total of products meeting the where clause
select count(*)
from
( select id_product
from table
where (id_feature = 1 AND id_feature_value = 50)
OR (id_feature = 2 AND id_feature_value = 54)
OR (id_feature = 5 AND id_feature_value = 67)
group by id_product
having count(*) = 3
) tmp
I have a problem with a MySQL Query:
I have two tables:
- clustercategories
- domains
Now I have a SQL Query which lists all Domains of a specific category with category name - this is my Query:
SELECT domains.*, clustercategories.clustercategoryname
FROM (domains, clustercategories)
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10)))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
The Problem is now, that I also have a third table "subpages" where I want to count all entries of a specific domain with status = '1' and I don't know how to modify my query to work - I have tried this query, but I do not get any results:
SELECT domains.*, clustercategories.clustercategoryname
FROM (domains, clustercategories)
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3) AND (SELECT COUNT(*) AS total FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1'))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10) AND (SELECT COUNT(*) AS total FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1')))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
Has anyone any ideas?
I think that you'll want to put your subquery into your projection, like this:
SELECT domains.*, clustercategories.clustercategoryname,
(SELECT COUNT(*) FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1') AS total
FROM domains, clustercategories
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10)))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
It looks to me your first query can be rewritten like this
SELECT d.*
, cc.clustercategoryname
FROM domains d
INNER JOIN clustercategories cc
ON cc.id = d.cluster1id
OR cc.id = d.cluster2id
WHERE cc.id IN (3, 10)
AND d.status = '1'
GROUP BY
d.name
ORDER BY
d.name
thus adding the count of subpages can be done like this
SELECT d.*
, cc.clustercategoryname
, sp.total
FROM domains d
INNER JOIN clustercategories cc
ON cc.id = d.cluster1id
OR cc.id = d.cluster2id
LEFT OUTER JOIN (
SELECT COUNT(*) AS total
, domainid
FROM subpages
WHERE subpages.status = '1'
GROUP BY
domainid
) sp ON sp.domainid = d.domainid
WHERE cc.id IN (3, 10)
AND d.status = '1'
GROUP BY
d.name
ORDER BY
d.name