Select clause from two different table - sql

SELECT DEPARTMENT.D#,DEPARTMENT.DNAME,DEPARTMENT.MANAGER#
FROM DEPARTMENT,PROJECT
WHERE DEPARTMENT.D# <> PROJECT.D#;
when this code run in SQL plus, a lot repeated output come out, my output should be
one time only.

Use as
SELECT DEPARTMENT.D#,DEPARTMENT.DNAME,DEPARTMENT.MANAGER#
FROM DEPARTMENT,PROJECT
WHERE DEPARTMENT.D# != PROJECT.D#;

Not sure if this is something that you want, but if you want only those departments that do not have any projects use
SELECT DEPARTMENT.D#,DEPARTMENT.DNAME,DEPARTMENT.MANAGER#
FROM DEPARTMENT
WHERE DEPARTMENT.D# not in (select PROJECT.D# from project);
Hope it Helps
Vishad

Related

Sub-query result set not read by main query

I have written a query like this :
select *
from DATASYNCH_HA_TO_TRG_AUDIT_HIST
where PSX_BATCH_ID IN (select PSX_BATCH_ID
from DATASYNCH_HA_TO_TRG_AUDIT_T
);
Here,when I execute the sub-query alone, it results some values and when I put those values in the place of sub-query the main query also returns some values.But,when I use this whole query ,it does not result any values.How is it possible?
Hope PSX_BATCH_ID column datatype is integer. if it is varchar filed, then trim the value.
SELECT *
FROM DATASYNCH_HA_TO_TRG_AUDIT_HIST
WHERE TRIM(PSX_BATCH_ID) IN
(SELECT TRIM(PSX_BATCH_ID) FROM DATASYNCH_HA_TO_TRG_AUDIT_T);
Instead of using IN query, use JOIN
SELECT *
FROM DATASYNCH_HA_TO_TRG_AUDIT_HIST A
INNER JOIN DATASYNCH_HA_TO_TRG_AUDIT_T B
ON (A.PSX_BATCH_ID = B.PSX_BATCH_ID)
Really Sorry guys,I found out it was actually a database issue.I ran the query recently in a procedure,it is working fine.

Create table - SQL Oracle

I need to create table, which should I call Others. I want only employeers who names which having names start with any other letter, but not K
I wrote sthg like this:
CREATE TABLE others AS select * from employees WHERE last_name no like 'K%';
I found sthg like this idea but it doesn't work
I'm receiving errror about syntax. Can you help me?
The second question: there is any other way to write it?
Try This
CREATE TABLE others AS (SELECT *
FROM employees
WHERE last_name NOT LIKE 'K%');
As #jarlh said in his comment, a view would serve the same purpose, but the data would only be stored once instead of twice, thus saving disk space. You could define the view as
CREATE OR REPLACE VIEW OTHERS AS
SELECT *
FROM EMPLOYEES
WHERE LAST_NAME NOT LIKE 'K%';
Best of luck.
I would recommend using just string functions. Here are two ways:
WHERE SUBSTR(last_name, 1, 1) <> 'K'
or:
WHERE last_name < 'K' or last_name >= 'L'
Although you can use LIKE or REGEXP_LIKE() for this, I like this simpler approaches.

Include records based on another table

I have table with "incidents" a second table with "notes/calls" and a third table with "assets".
I am trying to include ONLY records from the first table which are NOT referenced in the second or third...
As I write this another thought comes to mind... in any case suggestions are appreciated.
This is where I currently am...
SELECT tblCustIncidents.EntryDateTime, tblCustIncidents.IncidentID, tblCustIncidents.LocID
FROM tblCustIncidents
WHERE (tblCustIncidents.IncidentID Not In (SELECT tblCustCalls.IncidentID
FROM tblCustCalls) OR tblCustIncidents.IncidentID Not In (SELECT tblIncidentAssets.IncidentID
FROM tblIncidentAssets)) AND (tblCustIncidents.EntryDateTime)>Date()-17;
In SQL it would be something like
select * from incidents where incident_id_column not in
(select incident_id_column from notes/calls union select incident_id_column from assets)
Not sure how access does it
hope that helps though

Like sql statement not working

select sec_sec_nm,count(1)OVER() from nms_num_scheme
where nms_sec_nm like 'CMIG4%';
This query runs in one Oracle DB.
But the same query does not return any data in other DB though data are available.
This should work:
SELECT sec_sec_nm from nms_num_scheme where nms_sec_nm like 'CMIG4%';
I really don't know what you're trying to do with:
select sec_sec_nm,count(1)OVER()
But I don't think that will work at all.
select sec_sec_nm,count(1) cnt from nms_num_scheme
where nms_sec_nm like 'CMIG4%'
group by sec_sec_nm

Skip first letters of all values returned from a sql server database

as topic says, I don't want to return the first two letters in the return values
just an example:
select companyname from companies
returns companyX
Can I write a query that returns panyX instead?
Thanks in advance
select right(companyname, len(companyname)-3) as companyname will do the thing (this should work for microsoft T-SQ, see more string functions here )
Since you don't say what RDBMS you're using, here is an ANSI-compliant answer:
SELECT SUBSTRING(mycolumn,3,CHARACTER_LENGTH(mycolumn))
SELECT SUBSTRING(companyname, 3, len(companyname) - 2)
FROM companies
Here is a bunch of string manipulation stuff for sql
Tutorial 4: SQL functions
SELECT SUBSTR(COMPANYNAME,3) FROM COMPANIES;
u can solve ur issue using following queries,
--> select substring([image],4,len([image]))from dbo.emp
--> select replace([image],'ima','') from dbo.emp
Thanks
Venkat