Get count of a column based on join from other table - mysql

I am new to SQL. I have to tables and trying to get the count of column from table 1 and join by other column in table 2.
Table 1:
credits | sec_code | student_acc_id
--------------------------------
4 TUB 2098
5 JIY 2099
6 THG 3011
Table 2:
id| sec_code | student_acc_id | stu_id
-------------------------------------
1 TUB 2098 1011
5 JIY 2099 1011
7 THG 3011 1012
I would like to get the sum of credits for the student by getting the stu_id from table2 from sec_code and get all the student_acc_id for stuId and sum the credits column in table1 for all the student account Ids found from table 2. I am not sure how can we join or make this query simple.
Normally my approach is to inlcude this two to three different SQL statements, but i am looking for this in a one sql query if possible.
For the above example lets say i want to get sum of credits for all student_acc_id where stu_id is 1011 from second table. i just have the first table. So the output should be 4+5 as both accounts belong to the same student.
So i need:
--> get the stu_id based on sec_code from table two (lets say for TUB sec_code)
--> get all student_acc_id from table where stu_id is result from above statement
-->now using those all student_acc_id's sum the credit in table 1
Any help is appreciated !

Use Join according to your Data exist.
But as per the sample data better to get Data from Right table.
select
tab2.stud_id,
SUM(tab1.credits) AS credit_sum
from
table1 as tab1
right join
table2 as tab2
on tab1.student_acc_id = tab2.student_acc_id
and tab1.sec_cd = tab2.sec_cd
where
tab2.stud_id in(
select distinct stud_id
from table2
where student_acc_id = '2098'
)
group by
tab2.stud_id;
You can check its sample output here. Please click for fiddle solution.

try like below
select t2.stu_id,
sum(t1.credits)
from t1 join t2 on t1.student_acc_id=t2.student_acc_id and
t1.sec_code=t2.sec_code
group by stu_id
Having count(stu_id)>1

Related

Retrieving data across two tables

I'm pretty new to MySQL and need to get data from a column where the id in another column of the same table matches the id in a second table and I'm not sure how to go about it.
I haven't tried anything yet as I'm too new to go about answering my own question, sorry.
So my first table looks like this
userid questionid score
-----------------------------
1 1 5
1 2 4
1 3 7
1 4 10
1 4 6
And my 2nd table looks like this
otherfields userid
---------------------
blah 1
blah 2 2
etc 3
you 4
get 5
the 6
idea 7
So what I need to do is select all the scores from table 1 where userid of table 1 matches the user id of table 2.
So you want to sum up the score for each user?
Then something like this could help:
SELECT t2.userid, t2.otherfields, SUM(t1.score) AS sum_score
FROM first_table t1
LEFT JOIN second_table t2 ON t1.userid = t2.userid
GROUP BY t2.userid
First, you join the two tables together based on userid which is the same over both tables. Then you GROUP all rows which belong to the same user (e.g. question 1-5 for user 1) together and finally you SUM up the scores of each row of a group.

Selecting data from two table with condition

I have two tables
1. studentprofile
sud_id name
1 kp
2 kishan
3 raj
2. fee_generate
fee_id stud_id fee_balance name
1 1 0 kp
2 2 10 kishan
I want to show those students whose fee is not submitted or have any balance, which means that I want to show is as following
kishan and raj
I am not able to write the query with any join. My second problem is that name columns are common in both tables.
So the selected name column should come from studentprofile table and order by name from studentprofile table.
You need to left join the fee_generate table on the student_profile table and include those records where the fee_generate.student_id is null (not submitted) or the balance is over 0:
select s.* from student_profile s
left join fee_generate f on s.stud_id=f.stud_id
where f.stud_id is null or f.balance>0
order by f.name

Select From table name obtained dynamically from the query

I have 3 Tables
campaign1 (TABLE)
id campaign_details
1 'some detail'
campaign2 (TABLE)
id campaign_details
1 'some other detail'
campaign_list (TABLE)
id campaign_table_name
1 'campaign1'
2 'campaign2'
Campaign list table contains the table name of the two tables described above. I want to Select from the Campaign List table and get the record count using the table name i get from this select
For eg.
using select i get campaign1(Table name). Then i run select query on campaign1 to count number of records.
What i'm doing right now is .
-Select from campign_list
-loop through all campaign_table_names and run select query individually
Is there a way to do this using a single query
something like this
select campaign_name,(SELECT COUNT(*) FROM c.campaign_name) as campcount from campaign_list c
SQLFiddle : http://sqlfiddle.com/#!9/b766d/2
It's not possible inside a single query to build it dynamically but it's possible to cheat. Especially if there are only two linked tables.
I've listed two options
left outer join both tables
select campaign_name,
coalesce(c1.campaign_details, c2.campaign_details)
from campaign_list c
left join campaign1 c1 using (id)
left join campaign2 c2 using (id);
union all two different selects
select campaign_name,
campaign_details
from campaign_list c
join campaign1 c1 using (id)
union all
select campaign_name,
campaign_details
from campaign_list c
join campaign2 c2 using (id);
sqlfiddle
Combine your campaign tables to 1 table and add an column named 'type' (int).
campaign_items tables:
item_id item_details item_type
1 'some detail' 1
2 'some detail' 1
3 'some other detail' 2
4 'some other detail' 2
campaign_lists table
campaign_id campaign_name
1 'campaign1'
2 'campaign2'
Then you can use the following select statement:
SELECT campaign_name, (SELECT COUNT(*) FROM campaign_items WHERE item_type = campaign_id) as campaign_count
FROM campaign_lists
Oops, writing took me so long that you got this answered by Colin Raaijmakers already. Well, I'll post my answer anyway in spite of being more or less the same answer. Maybe my elaboration helps you see the problem.
Your problem stems from a bad database design. A database is made to order data and its relations. A CD database holds albums, songs, artists, etc. A business database may hold items, warehouses, sales and so on. Your database holds table names. [... time for thinking :-) ]
(When writing a DBMS you would want to store table names, column names, constraints etc., but I guess I am right supposing that you are not writing a new DBMS.)
So create tables that deal with your actual data. E.g.:
campain_type (id_campain_type, description, ...)
campain (id_campain, id_campain_type, campain_date, ...)
campain_type
id_campain_type description
1 Type A
2 Type B
3 Type C
campain
id_campain id_campain_type date
33 1 2015-06-03
85 2 2015-10-23
97 2 2015-12-01
query
select
ct.description,
(select count(*) from campain c where c.id_campain_type = ct.id_campain_type) as cnt
from campain_type ct;
result
description cnt
Type A 1
Type B 2
Type C 0

MySQL get all related IDs from a table matching an array

I'm facing a problem while trying to retrieve all productIDs from a table if they match all items in an array, in this case, return products only if they contain every ingredient the user searched for.
Table looks like this
ID produktID ingredientID
----------------------------
1 418 1
2 418 2
3 418 3
4 416 4
5 411 1
6 411 5
7 411 6
I join this table from a products table where the main information is stored. The aim of the query should be to retreive a productID only when all ingredientIDs match with the given array. I've tried using WHERE ingredientID IN(1,5,6) but it always turns out to be an OR statement, returning every ID where any of the ingredients are matched.
So for example, if I pass (1,5,6) or (5,6) the product ID 411 should be returned, but if I pass (2,5,6) it should not.
The query I tried looks like this (simplified, it's part of a 5 way join to other relations like brands and catgories)
SELECT productID FROM products_ingredients_mm WHERE ingredientID IN (1,5,6) GROUP BY productID
but the result contains 418 aswell. How do I get it to match?
I hope I was able to describe the problem in an understandable way, it's really hard for me to wrap my head around it to ask a question.
This is called Relational Division.
SELECT produktID
FROM tableName
WHERE ingredientID IN (1,5,6)
GROUP BY produktID
HAVING COUNT(*) = 3
SQLFiddle Demo
If a unique constraint was not enforce on ingredientID for every produktID, then you need to use DISTINCT
SELECT produktID
FROM tableName
WHERE ingredientID IN (1,5,6)
GROUP BY produktID
HAVING COUNT(DISTINCT ingredientID) = 3
SQLFiddle Demo
Other Source
Relational Division
Try this:
SELECT pi.productID, p.productName
FROM products_ingredients_mm pim
INNER JOIN products p ON pim.productID = p.productID
WHERE ingredientID IN (1,5,6)
GROUP BY productID
HAVING COUNT(DISTINCT ingredientID) = 3

How to filter duplicates within row using Distinct/group by with JOINS

For simplicity, I will give a quick example of what i am trying to achieve:
Table 1 - Members
ID | Name
--------------------
1 | John
2 | Mike
3 | Sam
Table 1 - Member_Selections
ID | planID
--------------------
1 | 1
1 | 2
1 | 1
2 | 2
2 | 3
3 | 2
3 | 1
Table 3 - Selection_Details
planID | Cost
--------------------
1 | 5
2 | 10
3 | 12
When i run my query, I want to return the sum of the all member selections grouped by member. The issue I face however (e.g. table 2 data) is that some members may have duplicate information within the system by mistake. While we do our best to filter this data up front, sometimes it slips through the cracks so when I make the necessary calls to the system to pull information, I also want to filter this data.
the results SHOULD show:
Results Table
ID | Name | Total_Cost
-----------------------------
1 | John | 15
2 | Mike | 22
3 | Sam | 15
but instead have John as $20 because he has plan ID #1 inserted twice by mistake.
My query is currently:
SELECT
sq.ID, sq.name, SUM(sq.premium) AS total_cost
FROM
(
SELECT
m.id, m.name, g.premium
FROM members m
INNER JOIN member_selections s USING(ID)
INNER JOIN selection_details g USING(planid)
) sq group by sq.agent
Adding DISTINCT s.planID filters the results incorrectly as it will only show a single PlanID 1 sold (even though members 1 and 3 bought it).
Any help is appreciated.
EDIT
There is also another table I forgot to mention which is the agent table (the agent who sold the plans to members).
the final group by statement groups ALL items sold by the agent ID (which turns the final results into a single row).
Perhaps the simplest solution is to put a unique composite key on the member_selections table:
alter table member_selections add unique key ms_key (ID, planID);
which would prevent any records from being added where the unique combo of ID/planID already exist elsewhere in the table. That'd allow only a single (1,1)
comment followup:
just saw your comment about the 'alter ignore...'. That's work fine, but you'd still be left with the bad duplicates in the table. I'd suggest doing the unique key, then manually cleaning up the table. The query I put in the comments should find all the duplicates for you, which you can then weed out by hand. once the table's clean, there'll be no need for the duplicate-handling version of the query.
Use UNIQUE keys to prevent accidental duplicate entries. This will eliminate the problem at the source, instead of when it starts to show symptoms. It also makes later queries easier, because you can count on having a consistent database.
What about:
SELECT
sq.ID, sq.name, SUM(sq.premium) AS total_cost
FROM
(
SELECT
m.id, m.name, g.premium
FROM members m
INNER JOIN
(select distinct ID, PlanID from member_selections) s
USING(ID)
INNER JOIN selection_details g USING(planid)
) sq group by sq.agent
By the way, is there a reason you don't have a primary key on member_selections that will prevent these duplicates from happening in the first place?
You can add a group by clause into the inner query, which groups by all three columns, basically returning only unique rows. (I also changed 'premium' to 'cost' to match your example tables, and dropped the agent part)
SELECT
sq.ID,
sq.name,
SUM(sq.Cost) AS total_cost
FROM
(
SELECT
m.id,
m.name,
g.Cost
FROM
members m
INNER JOIN member_selections s USING(ID)
INNER JOIN selection_details g USING(planid)
GROUP BY
m.ID,
m.NAME,
g.Cost
) sq
group by
sq.ID,
sq.NAME