there are 4 tables:A,B,C,D
there is an id:4(input)
the query should print the number of times the id is present in the tables
EX:
in table A id:4 occurs 5 times
in table B id:4 occurs 7 times
in table C id:4 occurs 3 times
in table D id:4 occurs 1 times
output:
Table name No of occurence
A 5
B 7
C 3
D 1
you can try something like that:
select 'A', count(*) from a where id = 4
union all
select 'B', count(*) from b where id = 4
union all
select 'C', count(*) from c where id = 4
union all
select 'D', count(*) from d where id = 4
select id as Name,count(id) as occurence from tableA where id = 4 group by id
union all
select id as Name,count(id) as occurence from tableB where id = 4 group by id
union all
select id as Name,count(id) as occurence from tableC where id = 4 group by id
union all
select id as Name,count(id) as occurence from tableD where id = 4 group by id
Do the union all
select 'A' as Name, count(*) as occurence from tablea where id = ?
union all
select 'B' as Name, count(*) as occurence from tableb where id = ?
union all
select 'C' as Name, count(*) as occurence from tablec where id = ?
union all
select 'D' as Name, count(*) as occurence from tabled where id = ?
Related
I have a table having three columns:
A B C
1 2 2
2 2 2
3 1 1
4 1 2
I want the count of those values which have C equal to 2 but with distinct values of B
So in this case for C = 2, count = 2 (B=2 and B=1)
I used the following command:
Select count(*) from mytable where C=2 group by (B)
but it yields:
count(*)
3
I have tried using "distinct" but it can't be use to select from one column
Have you tried
SELECT COUNT(DISTINCT B) FROM mytable WHERE C = 2;
Use sub query like this:
Select count(*) from (
select distinct B where c=2
)
Assuming I have this table
tableA
ID value
1 5
1 5
3 10
2 4
2 2
1 2
tableB
ID Name
1 apple
2 carrot
3 banana
If the expected max value of apple is 10, carrot is 5, and banana is 15 the output table would be
table output
ID Name value
1 apple 12
2 carrot 6
what SQL statement I need to solve this?
what I have done so far:
SELECT a,ID, b.name , sum(a.valueSUM) AS value FROM tableA a
INNER JOIN tableB b
ON a.id = b.id
GROUP BY id
what options i need on the WHERE clause to pull this off?
The inner subquery groups them normally and then the main query is what deals with limiting the results.
SELECT * FROM
(select
b.id,
b.name as name,
SUM(a.value) as the_sum
from tableA a inner join tableB b
on a.Id = b.id
group by b.name, b.id
) y
where (name = 'apple' and the_sum >= 10) OR
(name = 'banana' and the_sum >= 15) OR
(name = 'carrot' and the_sum >= 5)
It seems your sample data has changed, please try this. I thought the ID doesnt have to follow tableA/tableB's id and the id is auto-generated as per the results.
Would be nice if you have another table that sets the threshold per name
Assuming threshold can be specified in tableB (makes sense):
SELECT a.ID, b.name, sum(a.value) AS value
FROM tableA a
INNER JOIN tableB b
ON a.id = b.id
GROUP BY a.ID, b.name, b.Threshold
HAVING sum(a.value) > b.Threshold;
Demo: http://rextester.com/ICOQF10295
SELECT TableB.id, TableB.Name, MAX(TableA.value) AS Value
FROM TableA INNER JOIN TableB ON
TableA.id = TableB.id
GROUP BY TableB.id, TableB.Name
Instead of SUM, use MAX aggregate function
This works in SQL Server
--Existing tables
create table #tableA (ID int, value int)
create table #tableB (ID int, Name varchar(30))
insert into #tableA
select 1 , 5 union all
select 1 , 5 union all
select 3 , 10 union all
select 2 , 4 union all
select 2 , 2 union all
select 1 , 2
insert into #tableB
select 1 , 'apple' union all
select 2 , 'carrot' union all
select 3 , 'banana'
--Create new temporary table #tableC
create table #tableC (ID int, MAXvalue int)
insert into #tableC
select 1 , 10 union all
select 2 , 5 union all
select 3 , 15
select c.ID,b.Name, a.value from #tableC c
inner join #tableB b on b.ID = c.ID
inner join (
select ID,SUM(value) as value from #tableA
group by ID
) a on a.ID = c.ID
where a.value >= c.MAXvalue
drop table #tableA
drop table #tableB
drop table #tableC
I am using MYSQL and am looking for a way to only select a row where it's previous and next row have a field called fav_num with a value of '3'
For example I have 6 rows and two fields. The fields are ID and fav_number.
ID| fav_num
1 | 3
2 | 2
3 | 3
4 | 7
5 | 2
6 | 9
I'd like to find a way to return the ID from the table where the previous and next row have a fav_num of 3.
This query would then return ID 2.
I apologize if my question sounds confusing.
I think this could work..
SELECT id
FROM tab t
WHERE t.id BETWEEN
(SELECT MAX(id) FROM tab tp WHERE tp.id < t.id AND tp.fav_num = 3)
AND (SELECT MIN(id) FROM tab tn WHERE tn.id > t.id AND tn.fav_num = 3)
Please try this:
SELECT a.id FROM test1 a WHERE
3=(SELECT fav_num FROM test1 WHERE id = (SELECT MAX(id) FROM test1 WHERE id <a.id)) AND
3=(SELECT fav_num FROM test1 WHERE id = (SELECT MIN(id) FROM test1 WHERE id >a.id)) ;
SELECT a.id FROM test1 a WHERE
EXISTS (SELECT 1 FROM test1 WHERE id = (SELECT MAX(id) FROM test1 WHERE id <a.id) AND fav_num=3) AND
EXISTS (SELECT 1 FROM test1 WHERE id = (SELECT MIN(id) FROM test1 WHERE id >a.id) AND fav_num=3) ;
I am not sure about the perfornance.
SELECT ID FROM tbl WHERE fav_num BETWEEN '1' AND '3';
http://sqlfiddle.com/#!9/2b6fb0/12
SELECT t1.*
FROM t1
INNER JOIN (SELECT
#d1:=0+#d2 d1,
#d2:=0+#d3 d2,
#d3:=fav_num d3,
#previd:=#id prevID,
#id:=id
FROM (
SELECT *
FROM t1
ORDER BY id) t
) filter
ON filter.d1 = 3
AND filter.d3 = 3
AND t1.id = filter.prevID
;
Ok I have siple table which contains data:
id cat_id title (with random values)
1 1 test
2 1 tstt
3 3 tewt
4 2 4324
5 3 rterter
Now, I need to create a query which selects only ONE raw per category (cat_id)
(possibly with lowest ID and ordered by cat_id)
So the result should be:
1 1 test
4 2 4324
3 3 tewt
Use GROUP BY :
SELECT MIN(id), cat_id, title FROM table GROUP BY cat_id
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT cat_id, MIN(id) id
FROM tableName
GROUP BY cat_id
) b ON a.cat_id = b.cat_id AND
a.id = b.id
ORDER BY a.cat_id
I have a query that returns data in the following format:
id | name | number
1 John 12545
1 John 50496
2 Mary 23443
3 Mark 54
3 Mark 5600
3 Mark 50206
I would like to find out the number of distinct ids that appear in the result set. For example, for the result above. I would like to obtain the value 3.
Is there any way to add a column so the result looks like this instead?
count | id | name | number
3 1 John 12545
3 1 John 50496
3 2 Mary 23443
3 3 Mark 54
3 3 Mark 5600
3 3 Mark 50206
My query is:
SELECT * FROM (
SELECT id FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) totalCount,
id,name,number
FROM tableName
or by using CROSS JOIN
SELECT x.totalCount,
a.id, a.name, a.number
FROM tableName a, (SELECT COUNT(DISTINCT id) totalCount
FROM tableName) x
You should try :
SELECT id,name,number, (SELECT COUNT(DISTINCT name) FROM YourTableName) FROM YourTableName
Good luck
SELECT COUNT(DISTINCT id) would be faster than using column name.
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) as 'count',
id,name,number
FROM tableName
SELECT COUNT(id) AS count , id, name, number
FROM
(
SELECT id
FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
GROUP BY id, name, number