SQL - Getting Name Starting with a particular letter - sql

I have a query here that doesn't work and having trouble pin pointing my mistake.
Any help would be great.
Thanks
I am trying to retrieve records with a program name starting with 'C' but my query returns zero records.
My PROGRAM table has an entry of a ProgName of Chemistry.
SELECT P.ProgNumber, ProgName, StudID, DateEnrolled
FROM PROGRAM AS P, STUDENT AS S
WHERE P.ProgNo = S.ProgNo
AND ProgName LIKE 'C%';

Use
LIKE "C*"
MSAccess doesn't use % as the wildcard

SELECT
P.ProgNumber, P.ProgName, S.StudID, S.DateEnrolled
FROM
PROGRAM P
JOIN STUDENT S ON S.ProgNo = P.ProgNo
WHERE
P.ProgName LIKE 'C%';
should work... you said you changed it to ='Chemistry', do you get the same result if you use lowercase c in chemistry?

You need to join the different tables like this...Try this...
SELECT P.ProgNumber, P.ProgName, S.StudID, S.DateEnrolled
FROM PROGRAM P
JOIN STUDENT S
ON P.ProgNo = S.ProgNo
WHERE P.ProgName LIKE 'C*'; -- Asterisk because its Access not MS-SQL

Related

how to do if query in postgres

I want to make a query in which if the query does not find records for the query not exist, then it looks to see if these codes are present in the array field. The join will go through the code c_oplmlp/c_serv
select distinct ocf.code_head_mo , cs.c_oplmp, r."name", ocf.code_state, rm2.name_short,
ocf.id_condition_mc
from puomp.op_registry r
join puomp.op_case_finish ocf on r.id_registry =ocf.id_registry
join puomp.op_patient pt on ocf.id_patient =pt.id_patient
join puomp.op_case sl on ocf.id_case_finish =sl.id_case_finish
join puomp.op_case_ext cs on sl.id_case =cs.id_case
left join nsi.ref_mo rm2 on r.id_head_mo =rm2.id_mo
where not exists(select * from lic_app_new lm
left join ksg_p kp
where
lm.fc_mo=ocf.code_head_mo
and (lm.c_serv =cs.c_oplmp) if( (lm.c_serv !=cs.c_oplmp) then
lm.c_serv=kp.smj_prof )
endif;
)
ksg_p table look like this
ksg_p
|---id
|---name
|---c_prof(same as c_serv)(main code)
|---smj_prof(related to c_prof)
The idea is if query does not find by main code(c_serv) it search by related codes. This is codes of illneses. Smj_prof contains related codes of illness code. For example ucler has a code of 3( witch is code of gastroenterology) and it has related codes of 9(therapy) and 10(pediatrics). It means that pacient can be treated in gastroenterology, therapy and pediatrics
Help pls i dont even had an idea of how to do this.
What if you need just add public. to SELECT options
example:
select distinct public.ocf.code_head_mo, public.cs.c_oplmp, ...etc...

multiplie outputs with different wheres

What do I have to change to get different results from different names.The table should give me the debts of each of them, this is calculated by the amount and the price of the drink. Now it should show all the names with the corresponding invoice that happens after the select
%sql select name, sum(getraenk.preis*schulden.menge) schulden from schulden \
join person on (fk_person = person.id)\
join getraenk on (fk_getraenk = getraenk.id)\
where name like ("dani")
Edit: it should spend all the names with their debts, that is:
dani = 8.5
michael = 12.5
...
Just in case your problem is very simple, you should be able to see all names and values with an SQL that looks like this:
select name, getraenk.preis*schulden.menge schulden
from schulden
join person on (fk_person = person.id)
join getraenk on (fk_getraenk = getraenk.id)
Note that I removed the where clause... this was the part that limited it to one name.
You also don't need the sum clause here unless you are doing a group by
Have you considered simply using GROUP BY name at the end of this query?
https://www.w3schools.com/sql/sql_groupby.asp
This will give you the sum of total debt for all names in your table which sounds like the result you are looking for.
You're missing
GROUP BY name
in the query.

how to select a row if multiple values are in a related table

I am trying to make a filter to find all the stuffs made of various substances.
In the database, there is:
a stuffs table
a substances table
a stuffs_substances join table.
Now, I want to find only all the stuffs that are made of gold AND silver (not all the stuffs that contain gold and all stuffs that contain silver).
One last thing: the end user can type only a part of the substance name in the filter form field. For example he will type silv and it will show up all the stuffs made of silver.
So I made this query (not working):
select "stuffs".*
from "stuffs"
inner join "stuffs_substances" as "substances_join"
on "substances_join"."stuff_id" = "stuffs"."id"
inner join "substances"
on "substances_join"."substance_id" = "substances"."id"
where ("substances"."name" like '%silv%')
and ("substances"."name" like '%gold%')
It returns an empty array. What am I doing wrong here?
Basically, you just want aggregation:
select st.*
from "stuffs" st join
"stuffs_substances" ss join
on ss."stuff_id" = st."id" join
"substances" s
on ss."substance_id" = s."id"
where s."name" like '%silv%' or
s."name" like '%gold%'
group by st.id
having count(*) filter (where s."name" like '%silv%') > 0 and
count(*) filter (where s."name" like '%gold%') > 0;
Note that this works, assuming that stuff.id is the primary key in stuffs.
I don't understand your fascination with double quotes and long table aliases. To me, those things just make the query harder to write and to read.
if you want to do search by part of word then do action to re-run query each time user write a letter of word , and the filter part in query in case of oracle sql way
in case to search with start part only
where name like :what_user_write || '%'
or in case any part of word
where name like '%' || :what_user_write || '%'
you can also use CAB, to be sure user can search by capital or small whatever
ok, you ask about join, I test this in mysql , it work find to get stuff made from gold and silver or silver and so on, hope this help
select sf.id, ss.code, sf.stuff_name from stuffs sf, stuffs_substances ss , substances s
where sf.id = ss.id
and s.code = ss.code
and s.sub_name like 'gol%'

Why is my left join not working on two fields where one was parsed?

I have a table and a query that I want to join. The fields that I need to compare are Job/JobParse and Suffix/SuffixParse. The parsed fields came from dbo_JOB000 using RIGHT and LEFT functions to pull out the string. It may be useful to note that in the Table the Job field is of type text and the Suffix field is of type number.
dbo_job
job suf job_date Uf_Production_Line
H000001534 23 6/1/2015 LN4200
dbo_RESSCHD000
RESID JOBTAG STARTDATE
LN4200 147 6/8/2015 6:00:00 AM
LN4200 147 6/8/2015 2:00:00 PM
dbo_JOB000
JOBTAG JSID
147 .H000001534 .00023.0000000010
qry_Schedule - this query is built using dbo_RESSCHD000 and dbo_JOB000
SELECT
dbo_JOB000.JSID,
dbo_RESSCHD000.RESID,
dbo_RESSCHD000.GROUPID,
dbo_RESSCHD000.STARTDATE,
dbo_RESSCHD000.ENDDATE,
1 AS NumberofShifts,
Right(Left([JSID],11),10) AS JobParse, Left(Right([JSID],13),2) AS SuffixParse,
dbo_RESSCHD000.STATUSCD
FROM dbo_JOB000 INNER JOIN dbo_RESSCHD000 ON dbo_JOB000.JOBTAG = dbo_RESSCHD000.JOBTAG
WHERE
(((dbo_RESSCHD000.STARTDATE)>=Date()) AND ((dbo_RESSCHD000.ENDDATE)<=Date()+([Forms]![MainForm]![Text43]-1)) AND ((dbo_RESSCHD000.STATUSCD) Not Like "S"))
ORDER BY
dbo_JOB000.JSID;
qry_JobCompare - this query is built using dbo_job and qry_Schedule
SELECT
qry_Schedule.JobParse,
qry_Schedule.SuffixParse,
dbo_job.job_date,
dbo_job.Uf_Production_Line
FROM qry_Schedule
LEFT JOIN dbo_job
ON qry_Schedule.JobParse = dbo_job.job;
The goal is to return the job and suffix information from qry_Schedule and then use those to find other corresponding information from the dbo_job. For example, I want to use H000001534 and 23 to find that job in the dbo_job and find its job date, line number, etc... But I need to use the query's list of jobs.
The problem is that I am getting the error message "JOIN expression not supported".
Does anyone know how to fix this? Let me know if you need more info.
Thanks!
You can't use the equals operator to compare text fields. Try using the 'like' keyword.
SELECT
qry_Schedule.JobParse,
qry_Schedule.SuffixParse,
dbo_job.job_date,
dbo_job.Uf_Production_Line
FROM qry_Schedule
LEFT JOIN dbo_job
ON ( qry_Schedule.JobParse like dbo_job.job );
You also can't index a text field so this may be slow for large tables. Ideally the Job field would be a varchar or nvarchar type so that it can be indexed and the = operator can be used.
You can put brackets as
SELECT qry_Schedule.JobParse,
qry_Schedule.SuffixParse,
dbo_job.job_date,
dbo_job.Uf_Production_Line
FROM qry_Schedule INNER JOIN dbo_job ON (qry_Schedule .id_field=dbo_job.id_field);
If i get you right, the problem seems to be, that you have a value in one of your tables, which you have to parse first and join then.
I would use a subselect.
select * from (
select substring(bla,23,2) as jobParse, <otherfields> from qry_Schedule) as innertab left join dbo_job on innertab.job=dbo_job.job
or something like that.

SQL - Getting a column from another table to join this query

I've got the code below which displays the location_id and total number of antisocial crimes but I would like to get the location_name from a different table called location_dim be output as well. I tried to find a way to UNION it but couldn't get it to work. Any ideas?
SELECT fk5_location_id , COUNT(fk3_crime_id) as TOTAL_ANTISOCIAL_CRIMES
from CRIME_FACT
WHERE fk1_time_id = 3 AND fk3_crime_id = 1
GROUP BY fk5_location_id;
You want to use join to lookup the location name. The query would probably look like this:
SELECT ld.location_name, COUNT(cf.fk3_crime_id) as TOTAL_ANTISOCIAL_CRIMES
from CRIME_FACT cf join
LOCATION_DIM ld
on cf.fk5_location_id = ld.location_id
WHERE cf.fk1_time_id = 3 AND cf.fk3_crime_id = 1
GROUP BY ld.location_name;
You need to put in the right column names for ld.location_name and ld.location_id.
you need to find a relationship between the two tables to link a location to crime. that way you could use a "join" and select the fields from each table you are interested in.
I suggest taking a step back and reading up on the fundamentals of relational databases. There are many good books out there which is the perfect place to start.