using Full join in sql server - sql-server

i have 2 table data want join all together,each table are less 1 row date
example : table A have 8 row date and table b also have 8 row date but both table have 1 row date is different .i want my result show out as 10 row
table A
RN USERID ClockIn CHECKTIME badgenumber
1 6 8:24AM 2017-03-02 107
1 6 7:57AM 2017-03-03 107
1 6 8:23AM 2017-03-06 107
1 6 8:26AM 2017-03-07 107
1 6 8:57AM 2017-03-08 107
1 6 8:33AM 2017-03-09 107
1 6 8:36AM 2017-03-10 107
1 6 8:15AM 2017-03-13 107
table B
RN USERID ClockOut CHECKTIME badgenumber
1 6 9:31PM 2017-03-01 107
1 6 10:28PM 2017-03-02 107
1 6 8:22PM 2017-03-03 107
1 6 9:18PM 2017-03-06 107
1 6 9:48PM 2017-03-07 107
1 6 9:11PM 2017-03-08 107
1 6 11:31PM 2017-03-09 107
1 6 6:30PM 2017-03-10 107
my result show as
SELECT #clockin.ClockIn, #clockOut.ClockOut,#clockin.USERID,#clockin.CHECKTIME
FROM #clockin
FULL JOIN #clockOut
ON #clockin.CHECKTIME=#clockOut.CHECKTIME
where #clockin.userid = 6 and #clockOut.userid = 6
ORDER BY #clockin.userid;
<!DOCTYPE html>
<html>
<body>
<h2>result</h2>
<img src="https://i.stack.imgur.com/IcdSS.png" alt="result" >
</body>
</html>

Because of your where clause, you are filtering out rows where x.userid is null (where there is no match). This essentially turns your full join into an inner join. You can use coalesce() to return the first non-null value from your two columns and compare that to 6 like so:
SELECT #clockin.ClockIn, #clockOut.ClockOut,#clockin.USERID,#clockin.CHECKTIME
FROM #clockin
FULL JOIN #clockOut
ON #clockin.CHECKTIME=#clockOut.CHECKTIME
where coalesce(#clockin.userid,#clockOut.userid)=6
ORDER BY #clockin.userid;

SELECT ClockIn, ClockOut,
ISNULL(ci.USERID, co.USERID) AS USERID,
CONVERT(VARCHAR(10), ISNULL(ci.CHECKTIME, co.CHECKTIME), 101) AS CHECKTIME
FROM #ClockIn AS ci
FULL JOIN #ClockOut AS co ON (co.CHECKTIME = ci.CHECKTIME);
This should give your desired output for the sample data. However you may have to consider adding RN, USERID etc. in the JOIN filter depending on what you want.
The ISNULL() replaces the NULL id of ClockIn with the id from ClockOut.

Related

SQL server select statement to select the ids of a duplicated entries of another column

Consider the table 'Table1' as below
main_id main_item_id
-------- ---------
1 101
1 102
2 105
2 105
3 105
3 106
4 101
4 101
4 102
I need to fetch main_id 2 and 4 as it has duplicate main_item_id among 1 million other records
Thanks in advance.
This will select all unique main_id's which have 2 or more identical main_item_id's:
SELECT DISTINCT T.main_id
FROM YourTable T
GROUP BY T.main_id
, T.Main_item_id
HAVING COUNT(1) > 1
Use group by clause to check the duplication
SELECT main_id, main_item_id
FROM table
GROUP BY main_id, main_item_id
HAVING count(*) > 1

get record according to date

I try this query according to date i have only column datecurrent in this record according to current date so there is almost 5 days record with different dates now i want to fetch record according to date i use this but this shows all records whereas i want to get record in specific dates
select c.Orderid,c.DateCurrent,c.Quantity,c.ItemCost,i.ItemCost*c.quantity
as Bill from CustomerOrder c inner join item i on i.ItemId=c.ItemId
inner join userinformation ui on ui.userid=c.userid
where c.datecurrent as fromdate > '2017-04-13' union all
select null,null,null,null,sum (i.ItemCost*c.Quantity) bill
from CustomerOrder c inner join item i on i.itemid=c.ItemId
inner join userinformation ui on ui.userid=c.userid where c.datecurrent as todate > '2017-04-15'
there is records of 16 date and above query shows 16 date records where i want only those records which is between 13 and 15 and same with other dates .. i try this query in winforms with datepicker
this is data
Orderid DateCurrent Quantity ItemCost Bill
101 2017-04-16 14:35:45.823 12 10 120
1093 2017-04-16 17:32:36.250 2 10 20
2093 2017-04-16 17:32:36.250 2 10 20
2094 2017-04-13 17:32:36.250 4 10 400
2095 2017-04-15 17:32:36.250 5 10 50
2096 2017-04-15 17:32:36.250 10 10 1000
2097 2017-04-14 17:32:36.250 12 10 120
NULL NULL NULL NULL 1210
this is i want to get data if i enter only dates between 13 and 15
Orderid DateCurrent Quantity ItemCost Bill
2094 2017-04-13 17:32:36.250 4 10 400
2095 2017-04-15 17:32:36.250 5 10 50
2096 2017-04-15 17:32:36.250 10 10 1000
2097 2017-04-14 17:32:36.250 12 10 120
NULL NULL NULL NULL 1210
I suspect that this does what you want:
select c.DateCurrent, sum(i.ItemCost * c.quantity) as Bill
from CustomerOrder c inner join
item i
on i.ItemId = c.ItemId
where c.datecurrent >= '2017-04-13'
group by grouping sets ( (c.DateCurrent), () );
This returns one row per date plus the total of all dates, along with the sum of the cost times quantity.

Filter some records based on date range

I have below tables structures,
Trans Table:
Trans_Id(PK) User_Id(FK) Arroved_Date
________________________________________________
1 101 05-06-2016
2 101 12-06-2016
3 101 20-06-2016
4 102 06-06-2016
5 103 10-06-2016
6 103 25-06-2016
Table2:
Id(Pk) User_Id(Fk) Start_Date End_Date Is_Revoked
_________________________________________________________________________
1 101 01-06-2016 15-06-2016 1
2 101 10-06-2016 15-06-2016 0
3 103 05-06-2016 20-06-2016 0
I want to filter out the transaction, if the Approved_Date is not between the users Start_Date and End_Date of table2.
If is_revoked = 1 then it should not consider.
Expected Result:
Trans_Id
________
1
3
4
6
Try this
SELECT A.Trans_ID
FROM TransTable A JOIN Table2 B
ON (A.User_Id = B.User_Id)
WHERE B.Is_Revoked = 1 OR A.Approved_Date NOT BETWEEN B.Start_Date AND B.End_Date
This resolves you,
SELECT Trans_Id FROM TRANS AS A JOIN TABLE2 AS B ON A.[USER_ID]=B.[USER_ID]
WHERE B.IS_REVOKED=0 AND ARROVED_DATE NOT BETWEEN B.START_DATE AND B.END_DATE
there are basically two condition you want to meet.
if is_revoked is equal to 1 then ignore the row from the result..
if Approved_Date is between start_date and end_date then dont consider then also ignore this from the result.
So this can be easily done using join.
SELECT A.Trans_ID
FROM TransTable A, Table2 B
WHERE B.Is_Revoked != 1 AND A.Approved_Date NOT BETWEEN B.Start_Date AND
B.End_Date
As far as I understand from the expected result that you mentioned. you really dont' care if the User_id matches or not.

Max of a table into a column of a view

I'm trying to figure it out how to put a column of max of another table into a view table!
so here is my tables:
TblProprtyDetails
id Property_id amount situation
1 1 152 true
2 1 545 false
3 2 5 false
4 2 87 true
TblExperties
id PropertyDetails_id ExpertiesDate ExpertiesPrice
1 1 2015-10-01 54
2 1 2015-11-15 546
3 2 2016-01-05 6895
4 2 2016-08-01 654
now I want to put Max of ExpertiesDate and sum of amount into a view in this structure:
id Property_id amount situation LastExpertiesDate
select
id ,Property_id,amount,situation,max(ExpertiesDate) as lastexpertisedate
from
TblProprtyDetails t1
join
TblExperties t2
on t1.id=t2.id
group by
id ,Property_id,amount,situation
You also can use cross apply
select
id ,Property_id,amount,situation,b.*
from
TblProprtyDetails t1
cross apply
(
select max(ExpertiesDate) as lastexpertisedate from table t2 where
t1.id=t2.id) b

Return Values from Table 1 based on multiple columns from table 2

I want to be able to return values from Table 1 based on multiple columns from table 2.
Table 1
ID eventDate Price
111 2013-09-01 103
111 2013-10-04 103.5
111 2013-11-01 115
111 2013-11-02 114.5
111 2013-11-05 114
111 2013-11-09 112
112 2013-10-20 103
111 2013-10-23 103.5
111 2013-10-24 103
111 2013-10-25 103
111 2013-10-26 103
111 2013-10-27 103
etc...
Table 2 example:
ID startDate endDAte
111 2013-11-01 2013-11-05
112 2013-10-23 2013-11-07
113 2013-11-02 2013-11-03
114 2013-10-15 2013-11-01
115 2013-11-02 2013-11-05
I want to return results from Table 1 based on Table 2
For each row in Table 2 I want the following results from Table 1...
For ID 111, I want to know every Date and Price from Table 1 that falls between Table 2's "startDate" and "endDate". I want this information for each row of Table 2. If I only cared about one single ID I would write a simple query like...
Select ID, eventDate, Price
From Table 1
where (ID = 111) and (eventDate between startDate and endDate)
This would get me the results I need for ID 111, but I need the same results for ID 112, 113, etc. and for each respective startDate and endDate****
Please help. Thank you.
SELECT t2.ID, t1.EventDate, t1.Price
FROM dbo.Table2 AS t2
INNER JOIN dbo.Table1 AS t1
ON t1.ID = t2.ID
AND t1.EventDate >= t2.StartDate
AND t1.EventDate < DATEADD(DAY, 1, t2.EndDate);
To pre-empt questions/complaints about why I'm not using BETWEEN here, please read this and this.

Resources