I provided the ERD showing all the Tables and their info. How would I go about writing an SQL query for this report? Let me know if any other details are needed or if the ERD is sufficient.
Thanks!
SELECT date(s.showtime) as date
, s.showtime
, c.channelnumber
, p.progname
, p.proglength
, su.suppliername
FROM program as p left join schedule as s on p.progid = s.programid
left join channel as c on s.channelid = c.channelid
left join supplier as su on p.supplierid = su.supplierid
your schedule table has many pk and your structure is not according to normalized form. better restructure it
thanks
Related
I have a simple database with few tables (and some sample columns):
game (gm_id , game_name , company , desc)
plateform_master (p_id , plateform_name)
plateform_details (pd_id, gm_id, p_id, release_date)
Is there a way to create single SQL query which will return all game details with multiple plateform
Your question is not very clear. To which posts and categories are you referring yourself?
Here an example based on what I understood: Select all records from plateform_master and, for each of them, get its details from plateform_details. You can uncomment the where part in order to get one master record with its details. ("--" means comment in SQL)
select
pm.p_id
pd.*
from plateform_master as pm
inner join plateform_details as pd on pm.p_id = pd.p_id
-- where pm.p_id = 123
Sorry, my bad, it should be left join, not inner join!
With your changed requirements try this (not tested): Select all games and, for each one, get it's plateform_details and plateform_master correspondent values:
select
gm.gm_id,
gm.game_name,
gm.company,
pd.release_date,
pm.plateform_name
from game as gm
left join plateform_details as pd on pd.gm_id = gm.gm_id
left join plateform_master as pm on (pm.p_id = pd.p_id AND pd.gm_id = gm.gm_id)
-- WHERE CONDITIONS
;
Thanks for voting. I also wanted to tell you, that you have no many-to-many relationships here. You have just one-to-many. Many-to-many means the use of a middle table between two other ones.
I have 3 tables called
_partnership,
_partners,
_partnership_arm._partners = stores basic partner information
_partnership_arm = stores partnership arm details
_partnership = stores partners partnership records which includes the partner_id
arm_id which reference _partners.partner_id and _partnership_arm.arm_id.
So as an admin i want to select all details from the _partnership table which join other table reference without a where clause, but am having issue doing it.
here is my code
SELECT
_partnership.*,
_partners.names,
_partnership_arm.arm_name
FROM
`_partnership`
JOIN
`_partners`,`_partnership_arm` ON
_partnership.partner_id = _partners.partner_id
AND
_partnership.arm_id = _partnership_arm.arm_id
I also want a user to be able to select using a where clause
Please how can i achieve this?
Thank you.
SELECT
_partnership.*,
_partners.names,
_partnership_arm.arm_name
FROM
`_partnership`
JOIN
`_partners` ON _partnership.partner_id = _partners.partner_id
JOIN
`_partnership_arm` ON _partnership.arm_id = _partnership_arm.arm_id
I am writing a query for a health organization. The query is to pull patient data, where an encounter/appointment was completed but a chart note was not generated. I have the query pulling patients and their appointments; is there a way to basically say "only show the patients where 'master_im' document was not generated"?
I am using Microsoft SQL Server Management Studio.
Without seeing your table structures, etc. your could do a check to see if the master_im IS NULL.
SELECT *
FROM yourTable
WHERE appointment = 'completed'
AND master_im IS NULL
I would advise posting some additional details on your tables.
If the data is stored in separate tables, then you will want to JOIN the tables together to get the results you want.
EDIT #1 based on your comment you could do something like this:
select *
from person p
inner join appointments a
on p.enc_id = a.encid
left join patient_documents pd
on p.enc_id = pd.enc_id
where a.status = 'completed'
and pd.document_desc != 'master_im'
I created an inventory system which mostly is using a server-sided scripting language to do all the work. To try and get some performance gains I am looking to better design my database to try and minimizing the scripts.
I have a table named metal_part which has a one to one relationship with five other tables, basically the other tables are other parts, which those parts then have a one to one relationship with a few other tables.
When I query metal_part I need all the UPC numbers from each table, so its direct one to one relationships need to get their own information from their direct one to one relationship tables ect... Is it possible to make a huge query to build it all and put it in a form at like:
(###) - ####/##/##/## [a-z]
Using a query? or do I have to get all the information and concat it using a scripting language?
Thanks
You should be able to get all of the information you need using a standard join, and then, with the concat function appropriate to your database (see here http://www.1keydata.com/sql/sql-concatenate.html) you can form the string you want.
Your question is very vague.
I'm guessing you are talking about matching on a primary key called partnumber or something like that.
You can do this using a query like
SELECT mp.partnumber
, mp.UPC_number
, wp.UPC_number
, pp.UPC_number
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127';
You can also do
SELECT mp.partnumber
, group_concat(mp.UPC_number) as metal_UPCs
, group_concat(wp.UPC_number) as wood_UPCs
, group(concat(pp.UPC_number) as plastic_UPCs
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127'
GROUP BY mp.partnumber;
or
SELECT mp.partnumber
, concat_ws(','
, group_concat(mp.UPC_number)
, group_concat(wp.UPC_number)
, group(concat(pp.UPC_number)
) as UPCs_of_parts
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127'
GROUP BY mp.partnumber;
I have two tables:
Clients, and a join table that has user id, a foreign key to clients, and some other stuff.
I want to do this SQL
select TblClient.* from TblClient
inner join tblUserClientProjJoin as b on TblClient.Client_ID = b.Client_FK
where b.User_fk = 2
So getting a list of 'clients' that a specific user has access to.
I want to apply this result to collection I can bind to controls.
Anyone?
var q = From c in db.TblClient join
ucp in db.tblUserClientProjJoin on
c.Client_ID equals ucp.Client_FK
select c;
I haven't tested this so you may need to modify it a little.
Hope this helps!