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
Related
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
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
ID Name FatherID Birthyear
1 Bart NULL 1756
2 Franz 1 1796
3 Josef 2 1835
4 Zohan 3 1887
Suppose I have this table, I would like to know if Zohan is the son of Bart, which can be gotten if I compare the values from the column "FatherID" with the ID of the previous rows up until I get to Bart. But how do I compare the values in the same table but of different rows and columns
You could self join the table:
SELECT s.name AS son_name, f.name AS father_name
FROM mytable s
JOIN mytable f ON s.fatherID = f.id
-- possibly add a where clause with conditions on son/father names
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
I have to table.
Both table has nric field.
I want to select the nric field which does not inside in table.
si_isccourse table
enter code here
ID NRIC
1 456
2 457
3 458
si_results table
ID NRIC
1 456
si_isc_class table
ID NRIC
1 456
2 457
my results like this
ID NRIC
3 458
this is my sql query
SELECT DISTINCT(isc.isc_nric) from si_isccourse iscLEFT JOIN si_results re ON re.re_nric=isc.isc_nric LEFT JOIN si_isc_class cla ON isc.isc_nric!=cla.isc_class_nric WHERE (isc.isc_second_choice='FPS') AND ( re.re_year IN('2010','2009')) AND ( re.re_code IN('VETCA1','VETCA2')) AND isc.isc_nric!=cla.isc_class_nric ORDER BY re.re_mark desc
I want to get data not in si_isc_class table.
I want to select data from si_isccourse and compare with si_results and not in si_isc_class
SELECT first.*
FROM first
LEFT JOIN second ON(first.id = second.id)
WHERE second.id IS NULL
You might want to change the position of the tables.
some thing like
...
FROM si_isc_class ..
Left JOIN ....
Then you will get the desired value.