SQL Query for correlated tables - sql-server-2008

I have 4 tables named General , Apartment , RoomsAndAmenities and BasisInfo.
In General table columns are GeneralId , Name , ApartmentId
In Apartment table columns are ApartmentId
In RoomsAndAmenities table columns are RoomsAndAmenitiesId , ApartmentId
In BasicInfo table columns are BasicInfoId , bedrooms , RoomsAndAmenitiesId.
I wants to get the Name column from General where bedrooms=1
plz help out me.
Thank You

Alter procedure FilterApartmentByBedroom
#bedroom varchar(255)= null
As
Begin
select distinct
general.name
from general
inner join rooms_and_amenities on rooms_and_amenities.apartmentID = general.apartmentID
inner join basic_information on basic_information.roomsAndAmenitiesID = rooms_and_amenities.roomsAndAmenitiesID
where bedrooms=#bedroom
End

Related

Sql equivalent of this dax

I have a table in power bi which uses the below dax:
need inputs on SQL Query.
powerbi table name: demo,
employee is another table and demo table uses it and KEY IS ALREADY CREATED IN EMPLOYEE TABLE BY CONCATINATING employee'[EMP_CODE]&'employee'[dob]
key=SUMMARIZE( FILTER('employee','employee'[emp_STS]="working"),'employee'[EMP_CODE],'employee'[dob],"Key",'employee'[EMP_CODE]&'employee'[dob])
WORKEDHOURS = CALCULATE(MAX('Employee'[workhrs]), 'Empployee'[NAME]<>BLANK())
employeecode=SUMMARIZE( FILTER('employee','employee'[emp_STS]="working"),'employee'[EMP_CODE],'employee'[dob],"Key",'employee'[EMP_CODE]&'employee'[dob])
employeedob=SUMMARIZE( FILTER('employee','employee'[emp_STS]="working"),'employee'[EMP_CODE],'employee'[dob],"Key",'employee'[EMP_CODE]&'employee'[dob])
corresponding SQL Query I have tried :
select KEY,
CASE
WHEN NAME IS NOT NULL THEN MAX(HOURS)
END AS WORKEDHOURS,
EMP_CODE,
EMP_DOB
from employee where EMP_STS='WORKING' GROUP BY KEY,QC_NAME,DOB,EMP_CODE
Please, check this part and come with your feedback.
WITH
e1 AS (
SELECT
CONCAT([EMP_CODE],[dob]) AS [Key]
,EMP_CODE
,EMP_DOB
FROM employee
WHERE [emp_STS]='working'
GROUP BY [EMP_CODE],[dob]
)
,e2 AS(
SELECT
CONCAT([EMP_CODE],[dob]) AS [Key]
,MAX([workhrs]) AS [WorkedHours]
FROM employee
WHERE [NAME] IS NOT NULL -- ,[emp_STS]='working' ?
GROUP BY [EMP_CODE] ,[dob]
)
SELECT
e1.[Key]
,e1.[EMP_CODE]
,e1.[EMP_DOB]
,e2.[WorkedHours]
FROM e1
LEFT JOIN e2
ON e1.[Key]=e2.[Key]

MySQL select and match two tables and update column based on matched data

It seems difficult for me to thats why I need your help. So basically, I got two tables named xp_pn_resale and xp_guru_properties. What I need to do is update or set the column postal_code from table xp_pn_resale based from the data from another table. So here are my tables
My xp_pn_resale table, I wrote query like this in order to show you
SELECT postal_code,
block,
concat(block,' ', street_name) as address
FROM xp_pn_resale
where street_name like '%ANG MO KIO%';
And I get the result like this
As you can see, there are null values there and there are some postal_code that has values because I manually update them based on what I searched. I just want to automatically fill the postal_code from the query I got from other table.
Here is my xp_guru_properties table and I wrote query like this in order to show you
SELECT property_name as GURU_PROEPRTY_NAME,
property_type as GURU_PROPERTY_TYPE ,
JSON_UNQUOTE(JSON_EXTRACT(xp_guru_properties.json, '$.postcode') )as GURU_POSTCODE
FROM xp_guru_properties
where property_type like '%HDB%' AND property_name like '%ang mo kio%';
And the result is like this
xp_guru_properties got a column property_type which is a bit similar in the concatinated columns of block and street_name from other table I named it as GURU_PROPERTY_NAME.
As you can see, there is the virtual column named GURU_POSCODE. The values of that column is what I want to fill in the postal_code column from xp_pn_resale table. I was doing it manually to update the postal_code by doing
UPDATE xp_pn_resale
SET postal_code = 560110
WHERE street_name LIKE '%ANG MO KIO%'
AND block = 110
which is very tedious to me. Does anyone know how could I automatically update it based on the queries I showed ? Help will be appriciated.
EDIT: I wrote a JOIN query like this but this is for the record Lingkong Tiga which i manually filled all the postal_code
select distinct
JSON_UNQUOTE(json_extract(g.json, '$.postcode')) postcode,
JSON_UNQUOTE(json_extract(g.json, '$.name')) name,
JSON_UNQUOTE(json_extract(g.json, '$.streetnumber') )streetnumber,
p.block, p.street_name, p.postal_code
from xp_pn_resale p
inner join xp_guru_properties g
on g.property_name = concat(p.block, ' ', p.street_name)
where g.property_type like '%HDB%' AND g.property_name like '%Lengkong Tiga%'
I got result like this
Join the two tables and update.
UPDATE xp_pn_resale AS r
JOIN xp_guru_properties AS p ON concat(r.block,' ', r.street_name) = p.property_name
SET r.postal_code = JSON_UNQUOTE(JSON_EXTRACT(xp_guru_properties.json, '$.postcode') )
WHERE r.street_name like '%ANG MO KIO%'
AND p.property_type like '%HDB%' AND p.property_name like '%ang mo kio%'
AND r.postal_code IS NULL

SQL update table and INSERT only new lines with selected fields from another table

I am trying to update mysql databases of 2 sites in order to have both the same products, I need to find a query that will add ONLY new product lines to the product table, I am trying with:
INSERT INTO pre_upde_prop
(id
,pro_name
,pro_alias
,agent_id
,ref
,category_id
,price
,pro_small_desc
,pro_full_desc
,pro_type
,isFeatured
,published
,isSold
,note
,city
,state
,country
,province
,postcode
,hits
,created
,modified
,bed_room
,square_feet
,lot_size
,number_of_floors
,parking
,address
)
SELECT id
, name
, alias
, agent_id
, ref
, cid
, price
, description
, text
, type
, featured
, published
, available
, description
, lid
, sid
, cyid
, lid
, postcode
, hits
, listdate
, refresh_time
, bedrooms
, area
, covered_area
, garage
, garage
, years
FROM pre2_properties_products;
but when i run this query it adds duplicates of all lines. I need to find a query that, for example, starts the insert at line id number 6000, for example, inserting only new line after 6000.
Both table have same product ids.
You need some way to restrict the records that are going into via the INSERT. Currently you are SELECTing from a table without any criteria. What you need is to restrict the results of that SELECT statement to eliminate any records that already exist.
Where it would be easy enough to add WHERE ID > 6000 to the end of the query that you've shown, this is of little use for anything more than an ad-hoc update. More stable methods are a) a frustrated join against the table, or b) a WHERE NOT IN/WHERE NOT EXISTS clause:
Method 0:
INSERT INTO pre_upde_prop (...)
SELECT ...
FROM pre2_properties_products
WHERE id > 6000;
Method 1:
INSERT INTO pre_upde_prop (...)
SELECT ...
FROM pre2_properties_products AS a
LEFT JOIN pre_udpe_prop AS b ON b.ID = a.ID
WHERE b.ID IS NULL
Method 2:
INSERT INTO pre_upde_prop (...)
SELECT ...
FROM pre2_properties_products AS a
WHERE id NOT EXISTS (
SELECT id FROM pre_udpe_prop
);

1052 - Column 'typeid' in field list is ambiguous

select id,pubdate, typeid,aid,jobname,jobdepart,jobplace,jobnumber,jobcontact from
archives right join jobrt on id=aid where typeid=19
1, table archives have fileds: id,pubdate,typeid...
2, table jobrt have fields:aid,jobname,jobdepart,jobplace,jobnumber,jobcontact, typeid..
3, id=aid
now, i want to select out the id column the jobname,jobplace comlumns when typeid=19,..
thank you
since two tables: archives and jobrt contains columnName typeID, you need to specify the tableName where the value came from, eg
SELECT id
, pubdate
, jobrt.typeid
, aid
, jobname
, jobdepart
, jobplace
, jobnumber
, jobcontact
FROM archives
RIGHT JOIN jobrt
ON archives.id = jobrt.aid
WHERE jobrt.typeid = 19
You should identify what table in the select, something like the following:
select archives.id,archives.pubdate, archives.typeid,aid,jobname,jobdepart,jobplace,jobnumber,jobcontact from
archives right join jobrt on id=aid where typeid=19

Getting multiple values from a query

I write a query to get values from 3 tables but this is returning multiple values so can any one tell where i went wrong
select c.CompanyName,cd.FedTaxID,cd.EmailAddress,cd.PhoneNumber
from tblcustomerdetail cd,tblcustomer c
where c.FedTaxID in (
select FedTaxID
from tblcustomer
where CustomerID in (
select LOginID
from tbluserlogindetail
where UserName like "pa%" and RoleTypeID='20'
)
)
and cd.FedTaxID in (
select FedTaxID
from tblcustomer
where CustomerID in (
select LOginID
from tbluserlogindetail
where UserName like "pa%" and RoleTypeID='20'
)
);
My relation is here
My 3 tables are `tbluserlogindetails, tblcustomerdetails and tblCustomer'
1) Initially i will get `Login ID` from `tblUserLoginDetail ` based on the `user name`.
2) Next based on `LoginID` i will get `FedTaxID` from tblcustomerDetail`
3) Next based on 'FedTaxID' i will get the the required details from `tblcustomer'
SELECT
tblcustomer.CompanyName,
tblcustomerdetail.FedTaxID,
tblcustomerdetail.EmailAddress,
tblcustomerdetail.PhoneNumber
FROM tbluserlogindetail, tblcustomer, tblcustomerdetail
WHERE
tbluserlogindetail.LOginID = tblcustomer.CustomerID
AND tblcustomer.FedTaxID = tblcustomerdetail.FedTaxID
AND tbluserlogindetail.UserName LIKE 'pa%'
AND tbluserlogindetail.RoleTypeID = '20'
Try something like this.
Subqueries have a slow perfomance.
MySQL - SELECT WHERE field IN (subquery) - Extremely slow why?