Selecting distinct rows using join - sql

I have three table in database.
Hostel
hostel_id int,
hosteltype_id int,
hostelname varchar(100)
address varchar(800)
hosteltypes
hosteltype_id int,
Hosteltypename varchar(100)
hostelrooms
room_id int,
hostel_id int,
Room_no int,
available_beds int
reserver int
data in Hostel
1 1 hostel1 address1
2 1 hostel2 address2
3 2 hostel3 address3
4 2 hostel4 address4
in hosteltype
1 boyshostel
2 ladieshostel
in hostelroom
1 1 101 4 4
2 1 102 4 2
3 1 103 4 4
4 2 100 4 4
5 2 101 4 1
6 3 101 4 4
I can select rows using command.
select Hostel.hostel_id, Hostel.hostelname, Hostel.address, hosteltypes.Hosteltypename,
from Hostel,hosteltypes
where Hostel.hosteltype_id=hosteltypes.hosteltype_id
and hostel_id = (
select distinct hostelrooms.hostel_id
from hostelrooms
where hostelrooms.hostel_id=Hostel.hostel_id and hostelrooms.hostelrooms>hostelrooms.reserver
)
i want data similar like
1 hostel1 address1 boyshostel
2 hostel2 address2 boyshostel
how can a create sql command similar to above using join statement which returns specific hostelid,hostelname,hosteltype where the room available.

You're missing the from clause in your SQL. It should be like:
SELECT Hostel.hostel_id, Hostel.hostelname, Hostel.address, hosteltypes.Hosteltypename
FROM Hostel
JOIN hosteltypes ON ( hosteltypes.hosteltype_id = Hostel.hosteltype_id )
JOIN hostelrooms ON ( hostelrooms.hostel_id = Hostelhostel_id )

Assuming you're trying to get a distinct list of hostels with entries in the hostelrooms table, then this should work..
select distinct (Hostel.hostel_id, Hostel.hostelname, Hostel.address, )hosteltypes.Hosteltypename,
from Hostel,hosteltypes,hostelrooms
where Hostel.hosteltype_id=hosteltypes.hosteltype_id
and hostel_id = hostelrooms.hostel_id

Related

update unique id to table base on phone or email address in sql server 2008 r2

currently i have following table structure in sql server2008 r2
tbusertable
userid username uid status
1 abc null null
2 yax null null
3 xcd null null
4 max null null
5 wax null null
6 ear null null
7 yes null null
8 sqt null null
9 ora null null
tbphtable
pid userid phnos
1 1 456
2 2 456
3 3 4568
4 4 789
5 5 5555
6 6 4599
7 7 456
8 8 111
9 9 111
tbeidtable
eid userid eid
1 1 y#gmail.com
2 2 abd#gmail.com
3 3 erer#gmail.com
4 4 yer#gmail.com
5 5 g#gmail.com
6 6 g#gmail.com
i want to update uid column of tbusertable table with unique id, if they have same phnos for eid without cursor because table have large records and cursor take long time to run
desire output
userid 1,2,7 have same phnos so they have same unique id and similarly
userid 5,6 have same eid so they have different unique id
and similarly userid 8,9 have same phnos so they have same different unique id
tbusertable
userid username uid status
1 abc D7CCBC4E-EEE6-4AC8-806D-A04DCC77DF54 null
2 yax D7CCBC4E-EEE6-4AC8-806D-A04DCC77DF54 null
3 xcd null null
4 max 0608CFF7-3FC6-4952-91AE-5E42D6558827 null
5 wax 0608CFF7-3FC6-4952-91AE-5E42D6558827 null
6 ear null null
7 yes D7CCBC4E-EEE6-4AC8-806D-A04DCC77DF54 null
8 sqt 5823E1FD-2AF3-4BA7-8C48-946A16E0D3E2 null
9 ora 5823E1FD-2AF3-4BA7-8C48-946A16E0D3E2 null
If I understand correctly, you can calculate the new ids for each phone number using a subquery and then use this for the update:
update u
set uid = pp.new_uid
from tbusertable u join
tbphtable p
on u.userid = p.userid join
(select phnos, newid() as new_uid
from tbphtable
group by phnos
) pp
on p.phnos = pp.phnos;
EDIT:
Because of how SQL Server optimizes queries, you might need to put the newids in a temporary table:
select phnos, newid() as new_uid
into #pp
from tbphtable
group by phnos;
And then:
update u
set uid = pp.new_uid
from tbusertable u join
tbphtable p
on u.userid = p.userid join
#pp pp
on p.phnos = pp.phnos;
SQL Server can rewrite queries, resulting in functions being called more commonly than expected. Putting the values in a temporary table should solve that problem.

Query to display unique comments based on certain conditions

TABLE DEFINITION
ColumnName Comments
CustomerID INT
SequenceNo INT
Comments VARCHAR(MAX)
CUSTOMER TABLE
CustomerID SequenceNo Comments
1 1 ABC D
1 2 CDE
1 3 ABC
1 4 ABC D
1 5 CDE
1 6 abc
2 7 ABC DEF
2 8
2 9 ABC DEF
2 10 DEF
2 11 XYZ 123
2 12 ABC
3 13 PQ RST
OUTPUT
CustomerID SequenceNo Comments
1 3 ABC
1 4 ABC D
1 5 CDE
1 6 abc
2 8
2 9 ABC DEF
2 10 DEF
2 11 XYZ 123
2 12 ABC
3 13 PQ RST
Records should be filtered by
1. Display only Unique Comments from Customer Table for all the customers,
2. If Comments are same then display the row which has maximum SequenceNo
This assumes you are using a case sensitive collation
SELECT CustomerID,
MAX(SequenceNo) AS SequenceNo,
Comments
FROM yourTable
GROUP BY CustomerID,Comments
ORDER BY CustomerID,MAX(SequenceNo)
If you are not using a case sensitive collation, then try this:
SELECT CustomerID,
MAX(SequenceNo) AS SequenceNo,
Comments COLLATE SQL_Latin1_General_CP1_CS_AS
FROM yourTable
GROUP BY CustomerID,Comments COLLATE SQL_Latin1_General_CP1_CS_AS
ORDER BY CustomerID,MAX(SequenceNo)
You can use window functions for this:
select c.*
from (select c.*,
row_number() over (partition by CustomerId, Comments
order by SequenceNo desc) as seqnum
from comments c
) c
where seqnum = 1;

Need hierarichal data from 3 tables in SQL Server

I have following tables:
UserMaster:
UserId Int, UserName Varchar(200),AddedBy Int
UserId EmpName AddedBy
1 admin 0
2 SubAdmin1 1
3 SubAdmin2 1
4 Vikas 2
5 Mohit 4
6 Atul 5
7 Vishal 6
8 Mani 3
9 Sunny 8
SalesMaster:
SalesId Int, UserId Int (FK_UserMaster_UserId) , Price Int
SalesId UserId Price
1 1 100
2 2 200
3 3 300
4 4 500
5 5 100
6 6 200
7 7 111
8 8 222
9 9 333
Case 1: Now I want the price total of all the users who are under the one particular user and its own price also.
Means If i consider UserId=1 , Then the price will be calculated for all users where Column value in AddedBy=1
and their lower level employees.
Means the total Price of users will be calulated for the users having UserId are: 1,2,3,4,5,6,7,8,9.
Case 2: Similarly, If i want to calculate the total price under UserId=3(SubAdmin2) then the total price from the salesMaster will be calculated for the Users having UserId are: 3,8,9
The Result of first Case should be:
UserId Price
1 2066
The Result of Second Case should be:
UserId Price
3 300+222+333
Please Help
Thanks & Regards
Nitin
with cte as (
select #UserId as UserId
union all
select um.UserId
from UserMaster as um
inner join cte as c on c.UserId = um.AddedBy
)
select sum(s.Price)
from cte as c
inner join SalesMaster as s on s.UserId = c.UserId
sql fiddle demo

Sql Query to find reporting user details

i have a table with follow fields
UserID UserName ReportingUserID
1 Vinoth Null
2 Kumar 1
3 Raja 1
4 Ragu 2
5 Vignesh 2
6 Yoga 3
7 Yogesh 2
8 Eswar 4
9 Esakki 3
. ... .
. .... .
if i gave UserID as 1 then a query will display all users reporting to him will be displayed
if i gave USeRID as 1
Then result will be
UserID UserName ReportingUserID
2 Kumar 1
3 Raja 1
4 Ragu 2
5 Vignesh 2
6 Yoga 3
7 Yogesh 2
8 Eswar 4
9 Esakki 3
. ... .
. .... .
Have a look at using a recursive CTE.
A common table expression (CTE) can be thought of as a temporary
result set that is defined within the execution scope of a single
SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is
similar to a derived table in that it is not stored as an object and
lasts only for the duration of the query. Unlike a derived table, a
CTE can be self-referencing and can be referenced multiple times in
the same query.
Something like
DECLARE #Table TABLE(
UserID INT,
UserName VARCHAR(20),
ReportingUserID INT
)
INSERT INTO #Table VALUES
(1,'Vinoth',Null),
(2,'Kumar',1),
(3,'Raja',1),
(4,'Ragu',2),
(5,'Vignesh',2),
(6,'Yoga',3),
(7,'Yogesh',2),
(8,'Eswar',4),
(9,'Esakki',3)
DECLARE #UserID INT = 1
;WITH ReportingUsers AS (
SELECT *
FROM #Table
WHERE ReportingUserID = #UserID
UNION ALL
SELECT t.*
FROM #Table t INNER JOIN
ReportingUsers ru ON ru.UserID = t.ReportingUserID
)
SELECT *
FROM ReportingUsers

How to combine two tables to get results

The following is my master table:
tablename columnname size order
employee name 25 1
employee sex 25 2
employee contactNumber 50 3
employee salary 25 4
address street 25 5
address country 25 6
And following is my child table:
childid userid masterid isactive size order
1 1 1 Y 25 1
2 1 2 Y 25 2
3 1 3 N 0 0
4 1 4 Y 50 3
I would like to get the table name columnname from master table and size,order form child table against userid when isactive is Y in child table.
Some time, if the value is not there for the particular user than get all the values like tablename, columnname, size, order where isactive isY
I am really sorry to ask this but I am not good at SQL.
Regards.
Use INNER JOIN instead of LEFT JOIN
SELECT rcm.tablename, rcm.columnname, rcc.size, rcc.order
from report_customise_master rcm
INNER JOIN report_customise_child rcc
ON rcm.id = rcc.masterid
WHERE rcm.isactive = 'Y' and rcc.isactive = 'Y'
UPDATE 1
..., COALESCE(rcc.size, rcm.size) as Size,
COALESCE(rcc.`Order`, rcc.`order`) as `Order`