Converting SQL to Sybase dialect - sql-server-2008

The query is
Select id from TableA
where typ_cd="NT"
and id not in
( select id from TableA where typ_cd="BB")
I need to find those id's whose type_cd ="NT" and compare those id's with the same table which are not present for type_cd="BB" .I pretty confused why the above query is not returning the correct values.
Edit: -I'm referencing the same table and there are no null values for the column ID
Please let me know how can i achieve the same result in sql server so that i can try to write an equivalent query in sybase
TableA
id typ_cd
1 NT
1 BB
3 NT
4 NT
4 BB
I need the id=1 as the result since the id=1 is present for typ_cd=NT but not for typ_cd=BB
But at present with the above query im getting null in sybase

Replace the double quotes with single quotes.
Select id from TableA
where typ_cd='NT'
and id not in
( select id from TableA where typ_cd='BB')

Related

sql query to compare two fields of the table

I have two tables: table1 with field id and table2 with filed prv_id
Eg: table1: id
06G35ME
03G66UN
table2: prv_id
456P1BU-06G35ME
06G35ME
72006G35ME
4D022HO-06G35ME
06G35ME-06G35ME
07R63EV-06G35ME
06G36EV-06G35ME
247S4HO-06G35ME
MNG06G35ME
so i have to find all prv_id which are like id. I am using LIKE command to compare the fields but i am not getting any output. Please help
Pretty sure this will work in SQL Server and MySQL
select a.prv_id
from table1 a
left join table2 b
on a.prv_id like '%'+b.id+'%'

Some doubts about this simple INNER JOIN query?

I have the following doubt about this simple INNER JOIN query.
I have these two tables that have to be joined togheter:
The first table is named VulnerabilityFix and contains the following columns:
Id: int identity
FixName: varchar
Vendor: varchar
Title: varchar
Version: varchar
The second table is named VulnerabilityAlertDocument_VulnerabilityFix (this bind the previous table to another table, but this is not important at this time) and contains the following columns:
VulnerabilityAlertDocumentId: int
VulnerabilityFixId: int
Now, on my DB the VulnerabilityFix table contains only an empty record (this record have an id but all the other fields are empty\null), infact if I perform a select *, I obtain:
select * from VulnerabilityFix
Id FixName Vendor Title Version
1
Into the VulnerabilityAlertDocument_VulnerabilityFix I have something like this:
select * from VulnerabilityAlertDocument_VulnerabilityFix
VulnerabilityAlertDocumentId VulnerabilityFixId
78385 1
78386 1
....................................................
....................................................
....................................................
78398 1
Ok, so I want JOIN toghert these 2 table in in such a way that passing the value of the VulnerabilityAlertDocumentId field of the VulnerabilityAlertDocument_VulnerabilityFix table, I obtain all the related record in the VulnerabilityFix table.
So in this case I aspect to retrieve the previous only record that having an id (having a value equal to 1) and all the other fields are empty\null.
So my query is:
SELECT VF.* FROM VulnerabilityAlertDocument_VulnerabilityFix VAD_VF
INNER JOIN VulnerabilityFix VF ON VAD_VF.VulnerabilityAlertDocumentId = VF.Id
WHERE VAD_VF.VulnerabilityAlertDocumentId = 1
The problem is that when I execute this query I obtain an empty set of records and not the unique record that I expetc to obtain.
Why? What am I missing?
Tnx
I think your query should be more like:
SELECT VF.* FROM VulnerabilityAlertDocument_VulnerabilityFix VAD_VF
INNER JOIN VulnerabilityFix VF ON VAD_VF.VulnerabilityFixId = VF.Id
WHERE VAD_VF.VulnerabilityAlertDocumentId = 78385
That is, you are using the wrong column at your ON condition since VulnerabilityFixId seems to be the foreign key over VulnerabilityFix.Id and not VulnerabilityAlertDocumentId.
On the other hand, I can't see any VulnerabilityAlertDocument_VulnerabilityFix.VulnerabilityAlertDocumentId with value 1 in you data set (where condition)

I need a desired table format in sql

TeacherApprover Table - Table format in Database
TeacherID ApproverID ApproverLevel
15122 4 1
15122 2 2
15122 3 3
i need the result as in select Statement- it should display as below
TeacherID ApproverID ApproverID2` ApproverID3
15122 4 2 3
i tried directly binding in front end but i have some issues after binding like this, So i want to try getting the result in Dataset directly from backend.
please guide me
I think you need approver id by teacher Id.
So I think you can get data using query,
SELECT ApproverID from table_name where teacherID = 15122;
Then handle the result according to your requirement.
Create new table having column as you needed.
insert into that new table .
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

MySQL INSERT INTO table 1 SELECT table 2 with different column name

I have a table (pdt_1) in database (db_1) and another table (pdt_2) in another database (db_2).
I met pdt_1 and pdt_2 to find pdt_1 products not present and published in pdt_2.
functional code :
SELECT * FROM db_1.pdt_1 AS lm
WHERE lm.product_sku
NOT IN (SELECT DISTINCT product_cip7 FROM db_2.pdt_2)
AND lm.product_publish=‘Y'
finally, I need to insert the result of this query in pdt_2.
However, the structure of pdt_1 and pdt_2 are different.
Example:
- columns's names
- columns's numbers
I also need an auto_increment id for pdt_1 products inserted into pdt_2.
I need help.
NB : sorry for my poor english :(
If you want a new table with just the id and product_sku, try:
INSERT INTO new_table # with id and product_sku from first table
SELECT pdt_1.id,
pdt_1.product_sku
FROM db_1.pdt_1
LEFT JOIN db_2.pdt_2
ON pdt_1.product_sku = pdt_2.product_cip7
WHERE pdt_2.product_cip7 IS NULL
AND pdt_1.product_publish = 'Y'

MySQL INSERT INTO single column from a SELECT query pulling data from two other tables - throwing #1062 - Duplicate entry '' for key 2 error

I've got one col (state_not_allowed) in TABLE vendor_product where I'm trying to insert values from product_catalog_varchar.value - but only if there's a sku in vendor_product that matches a sku in product_catalog where product_catalog's id equals product_catalog_varchar's id and product_catalog_varchar's attribute id = 523.
I'm basically trying to do the MySQL equivalent of an Excel VLOOPUP. I need the result of the following query:
SELECT product_catalog_varchar.value
FROM product_catalog_varchar
JOIN product_catalog
ON product_catalog.id = product_catalog_varchar.id
JOIN vendor_product
ON vendor_product.sku = product_catalog.sku
AND product_catalog_varchar.attribute_id = 523
To be inserted in to column state_not_allowed, where the sku in vendor_product = the sku in product_catalog.
I've done some research on INSERT INTO, here and on Google in general. Looks like a lot of the instruction out there is on simplier queries so I haven't been able to find a decent model to figure out what to do. I can tell you that this query doesn't work:
INSERT INTO vendor_product(`state_not_allowed`)
SELECT product_catalog_varchar.value
FROM product_catalog_varchar
JOIN product_catalog
ON product_catalog.id = product_catalog_varchar.id
JOIN vendor_product
ON vendor_product.sku = product_catalog.sku
AND product_catalog_varchar.attribute_id = 523
It throws the following error: #1062 - Duplicate entry '' for key 2
And if I got to vendor_product and look, instead of simply inserting values in to state_not_allowed, it's creating a whole new row (with no data). Clearly, I'm misunderstanding in a fundamental sense here. Help me out? Thanks folks.
This query shows the general idea of what you want to do.
insert into table2
(field1)
select distinct field1
from table1
where field1 in
(select field1
from table1
except
select field1
from table2)
The details vary from RDBMS to RDBMS. For example, Oracle uses the keyword minus instead of except. Your MySql documentation will help you with the details.
Note that while it's tempting to simplify this by using "not in" that construct tends to be slow.