Table recursive relationship-query - sql-server

The above table has recursive relationship ie. the crfor column has reference to resourceid column.
So the number 1 in the crfor column means resourceid = 1 .How should I change my query to make crforname column (dynamic column) display the proper name Arjun instead of Ashok?
I used the query below to get the result above. You can ignore the other table call PCR in the query
select t1.*,t2.pcrname,t1.ResourceName AS crforName from
Resource t1 left join Pcr t2 on t1.Pcrid=t2.pcrid
where t1.contractid=1

Try adding a self join on the Resource table joining the ResourceId to the CrFor:
select t1.*,t2.pcrname,t3.ResourceName AS crforName from
Resource t1 left join Pcr t2 on t1.Pcrid=t2.pcrid left join Resource t3 on t1.CrFor = t3.ResourceId
where t1.contractid=1

Related

How to use same two column names in angularjs

I have two tables which as same column name called id (stores record id). I'm using an inner join on these 2 tables and taking the id column of 2 tables. I'm using this select query in the angularjs http.get rest API. When the rest API throws the resultant table which contain 2 column of same name-id, I'm storing this in scope variable.
The problem is how can i use the id of one particular table.
consider query is,
select t1.id,t2.id
from table1 AS t1
INNER JOIN table2 AS t2 ON t2.product=t1.product_id
I am storing the result of rest API in scope variable,
$scope.details=response.data.platform.record;
I used these below syntax inside alert box in controller but not working,
$scope.details.id, $scope.details.t1.id.
Using column alias name, will solve your problem:
SELECT t1.id AS T1Id,
t2.id AS T2Id
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t2.product = t1.product_id
and in your controller, you can use as:
$scope.details.T1Id
$scope.details.T2Id

Compare rows two tables

I want to compare rows value from one table with rows value from another table. Structure of the first table is :
The other table is more complex than first table and have the same structure but number of rows is million rows, by the way first have 60.000 rows.
I need to compare these two tables by biling_profiles so I have an insight into the price but that billing profile turn in the column. It's easily done using the left outer join because approximately 6 or 7 billing_profiles . Next thing I need is that if the other table has no value (for example, the first table has row with (destination 201425, cost 2,624) and the other does not , then I have trimmed one character from right to left and then search (destination - 20142). If no again result i repeat again trim from right to left (destination - 2014), when i found same destination then show in table)
So, how to solve that ?
try
SELECT DISTINCT A.Id
FROM
table1 A
INNER JOIN table 2B
ON A.Id = B.Id AND A.biling_profiles = B.biling_profiles
or
select t1.biling_profiles,t2.biling_profiles
from table1 t1
inner join table2 t2 on
t1.id= t2.id
where t1.biling_profiles= #biling_profiles

Join Query Combining Two Tables

I need to display 3 columns from table A and one column from table B. I used the following join query to combine them but not getting the value in the expected columns.
example1 -this is working.(If I display one column from table A and one column from table B it works)
SELECT
tableA.col1(fkid),
tableB.col1
FROM tableA
INNER JOIN tableB
ON tableA.fkid = tableB.pkid;
I need to display 3 columns from table A and one column from table B the below query is not working.
SELECT
tableA.col1,
tableA.col2,
tableA.col3,
tableB.col1
FROM tableA
FULL JOIN tableB
ON tableA.fkid = tableB.pkid;
Original query:
select device.name,device.description, device.fkphonetemplate,phonetemplate.name from device inner join phonetemplate ON device.fkphonetemplate=phonetemplate.pkid;
Result:
description fkphonetemplate name
Nikhil (nkalantr) 10ce46f6-615d-4605-9f42-454225df5647 ARRAY(0xc7153b0)
Expected result should be:
description fkphonetemplate name
Nikhil (nkalantr) 10ce46f6-615d-4605-9f42-454225df5647 Standard 7960 SCCP
I don't get name from device table in the result and the name from phonetemplate table shows something as Array0X... but I need to get the name of phonetemplate like Standard 7960 as shown in expected result.
Can you refine my query or suggest what's wrong with the 2nd query?
Any reason for you to using full join instead of inner join ?
If no,try using inner join.
SELECT
a.col1,
a.col2,
a.col3,
b.col1
FROM tableA a
INNER JOIN tableB b
ON a.fkid = b.pkid;
Can you provide your database sample data , so we can clearly know what your need.

display data from two different table which don't have any relation?

table1: in that three columns billno, billamt, wno
table2: in that three column chequeentry, chequeamt, wno
i want display the result which is not repeated rows where wno is equal to same
if from table1 i got four rows and from table2 i got two rows. I want display the rows but not repeated. if i use join its done the multiply and show the result and my table2's rows are repeated. But dont want this. it display the null value it i accepted.
i pass the one query i.e join the table
select b.billno, b.billamt, c.chequeentry, c.chequeamt
from t2 as b
inner join t3 as c on b.wno=c.wno where b.wno=1
i got output but some value are repeated.
Use the Full outer Join, it return both matching, and not matching rows from both tables.
Syntax:
SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name;

SQL Same Column in one row

I have a lookup table that has a Name and an ID in it. Example:
ID NAME
-----------------------------------------------------------
5499EFC9-925C-4856-A8DC-ACDBB9D0035E CANCELLED
D1E31B18-1A98-4E1A-90DA-E6A3684AD5B0 31PR
The first record indicates and order status. The next indicates a service type.
In a query from an orders table I do the following:
INNER JOIN order.id = lut.Statusid
This returns the 'cancelled' name from my lookup table. I also need the service type in the same row. This is connected in the order table by the orders.serviceid How would I go about doing this?
It Cancelled doesnt connect to 31PR.
Orders connects to both. Orders has 2 fields in it called Servicetypeid and orderstatusid. That is how those 2 connect to the order. I need to return both names in the same order row.
I think many will tell you that having two different pieces of data in the same column violates first normal form. There is a reason why having one lookup table to rule them all is a bad idea. However, you can do something like the following:
Select ..
From order
Join lut
On lut.Id = order.StatusId
Left Join lut As l2
On l2.id = order.ServiceTypeId
If order.ServiceTypeId (or whatever the column is named) is not nullable, then you can use a Join (inner join) instead.
A lot of info left out, but here it goes:
SELECT orders.id, lut1.Name AS OrderStatus, lut2.Name AS ServiceType
FROM orders
INNER JOIN lut lut1 ON order.id = lut.Statusid
INNER JOIN lut lut2 ON order.serviceid = lut.Statusid

Resources