Table1 data:
ACType Dualhr P1hr Total
A320 2 2 4
B787 1 2 3
B777 3 1 4
Table2 data:
ACType P1hr Total
A320 5 5
on comparing the 2 tables,what sql query will give me result:
ACType Dualhr P1hr Total
A320 2 7 9
B787 1 2 3
B777 3 1 4
which should also factor case when Table1 is empty,so to give result:
ACType Dualhr P1hr Total
A320 0 5 5
Use union and subquery:
select ACType, sum(Dualhr) as Dualhr, sum(P1hr) as P1hr,sum(Total) as Total
from
(
select ACType, Dualhr, P1hr, Total from table1
union all
select ACType, 0, P1hr, Total from table2)a
group by ACType
Related
can anyone help me to group every 3 counts rows???
I have data like this
num
score
1
3
1
3
1
3
1
3
1
3
1
3
4
3
4
3
4
3
2
3
2
3
2
3
and i want result like this
num
count(num)
1
3
1
3
2
3
4
3
You want to group every 3 rows. But without giving supplementary conditions, we can only take assumptions on how you would like to group. Supposing each row in the 3-rows group has the same content and the score value from the base table has no influence on grouping, I guess you just want to get the num once for every 3 rows as the 3 rows have the same num. Besides, we use a constant value 3 for count(num) column. If all my assumptions come true, try this:
select num, 3 as 'count(num)'
from (select num,#row_id:=#row_id+1 as row_id
from test,(select #row_id:=0) t1 ) t2
where row_id mod 3 =0
order by num
;
I have MySQL table with name users. SQL returns following result
select user_id, vegetarian, count(*) from users group by user_id, vegetarian;
Current SQL Output
user_id vegetarian count(*)
2 1 15
3 0 131
3 1 6
4 1 6
5 0 113
5 1 7
6 1 6
7 0 107
7 1 11
Required Output
My required output is:
user_id vegetarian count(*)
2 1 15
4 1 6
6 1 6
Kindly help me for SQL query to get my required output.
Kind Regards
SELECT user_id, 1 vegetarian, COUNT(*)
FROM users
GROUP BY user_id
HAVING NOT SUM(NOT vegetarian);
NOT vegetarian converts 0 to 1 and backward. Hence SUM() calculates the amount of rows which are not vegetarians. And HAVING NOT SUM() removes the rows where this sum is above zero.
I the following table:
ID TIMESLOT_ID SLOTS_AVAILABLE
1 1 10
2 3 2
3 8 3
4 9 10
5 2 10
6 6 10
7 4 10
I want to perform a query where I can get all rows where SLOTS_AVAILABLE is equal or greater than (user_input_1) AND the next (user_input_2) consecutive rows following to that one meet the same condition (SLOTS_AVAILABLE is equal or greater than (user_input_1))
So if (user_input_1) is 3 and (user_input_2) is 3 the result would be:
ID TIMESLOT_ID SLOTS_AVAILABLE
3 8 3
4 9 10
Hmmm . . . If I understand correctly, you want window functions:
select t.*
from (select t.*,
sum(case when slots_available >= :user_input_1 then 1 else 0 end) over
(order by id
rows between current row and :user_input_2 following
) as cnt
from t
) t
where cnt = :user_input_2 + 1;
This requires MySQL 8+.
Here is a db<>fiddle.
Please help me with writing a query for the following condition. I have a table which I have listed below
ID Wt1 Wt1_Type Wt2 Wt2_Type Wt3 Wt3_Type Wt4 Wt4_Type
--------------------------------------------------------------
1 200 1 220 1 300 2 400 3
2 100 4 150 3 100 5 120 1
3 100 3 110 1 200 5 100 4
I want a query to sum all the the weights (wt1, wt2, wt3, wt4) grouped on the weight type (wt1_type, wt2_type, wt3_type, wt4_type).
The output should look like
Wt_type Total
1 650
2 300
3 650
4 200
5 300
Can someone please help me draft a mysql query to get this result ?
Thanks
You can try below - using union all and subquery
select Wt_Type,sum(Wt) as total from
(
select Wt1_Type as Wt_Type,Wt1 as Wt from tablename
union all
select Wt2_Type ,Wt2 from tablename
union all
select Wt3_Type ,Wt3 from tablename
union all
select Wt4_Type ,Wt4 from tablename
)A group by Wt_Type
Rather than giving the answer by #fa06, which should work for you, I am going to suggest using a better table design. Here is how you should be storing your data:
ID Type Wt
-------------
1 1 200
2 4 100
3 3 100
4 1 220
5 3 150
6 1 110
7 2 300
8 5 100
9 5 200
10 3 400
11 1 120
12 4 100
Note that there is a single column which stores the type and a single column for that type's weight. Now your expected output just requires a very simple query:
SELECT Type, SUM(Wt) AS Total
FROM yourTableUpdated
GROUP BY Type;
Databases are really good at performing operations across rows, much less so across columns.
Use this it should be work
select Wt_Type,sum(Wt) as total from ( select Wt1_Type as Wt_Type,Wt1 as Wt from tablename union all select Wt2_Type ,Wt2 from tablename union all select Wt3_Type ,Wt3 from tablename union all select Wt4_Type ,Wt4 from tablename )A group by Wt_Type
i have a problem concerning a select query in MYSQL
i have two different tables and i want to obtain a certain result
i used COUNT method which gave me only the results (>=1)
But in reality , i want to use all counts with zero included how to do it?
My query is:
SELECT
first.subscriber_id,
second.tag_id,
COUNT(*)
FROM
content_hits first
JOIN content_tag second ON first.content_id=second.content_id
GROUP BY
second.Tag_id,first.Subscriber_id<br>
First table:Content_hits
CONTENT_ID SUBSCRIBER_ID
30 1
10 10
34 4
32 2
40 3
28 3
30 6
31 8
12 3
Second table:Content_tag
CONTENT_ID TAG_ID
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 2
12 2
13 2
14 2
Result but incomplete For example:Subsrciber6 for tag_id=1 should have a count(*)=0
subscriber_id tag_id COUNT(*)
1 1 4
2 1 7
3 1 2
4 1 1
5 1 3
7 1 2
8 1 1
9 1 1
10 1 3
1 2 2
2 2 3
3 2 2
Now that you have further elaborated on what you actually want to achieve, it can be seen that the problem is much more complex. You actually want all combinations of subscriber_id and tag_id, and then count the number of actual entries in the joined table product. whew. So here goes the SQL:
SELECT combinations.tag_id,
combinations.subscriber_id,
-- correlated subquery to count the actual hits by tag/subscriber when joining
-- the two tables using content_id
(SELECT count(*)
FROM content_hits AS h
JOIN content_tag AS t ON h.content_id = t.content_id
WHERE h.subscriber_id = combinations.subscriber_id
AND t.tag_id = combinations.tag_id) as cnt
-- Create all combinations of tag/subscribers first, before counting anything
-- This will be necessary to have "zero-counts" for any combination of
-- tag/subscriber
FROM (
SELECT DISTINCT tag_id, subscriber_id
FROM content_tag
CROSS JOIN content_hits
) AS combinations
Not sure, but is this what you want?
SELECT first.subscriber_id, second.tag_id, COUNT(*) AS c
FROM content_hits first JOIN content_tag second ON first.content_id=second.content_id
GROUP BY second.Tag_id,first.Subscriber_id HAVING c = 0