Please Correct my query to Select data.
If there is an entry in LeaveApply Table having given date betwwn a from date and to date in the table, i should get the pK of table else zero.
please help
Select A,
B,
ISNULL((select LeaveApply_Id from Tbl_Stud_Leave_Apply where Candidate_Id=120
and ('01/13/2014 12:00:00 AM' between Stud_LeaveFromDate and Stud_LeaveToDate)),0) As Leave from TableA where Condition.
You have to apply the isnull to the column, not to the whole query.
Try the below
select ISNULL(LeaveApply_Id,0) AS ID from Tbl_Stud_Leave_Apply
where Candidate_Id= #Candidate and (#date between Stud_LeaveFromDate and Stud_LeaveToDate)
In the above #Candidate and #date are dynamically passed to query.
Note that if you do the lookup across a lot of results, you should consider a set-based approach to the lookup, e.g.
SELECT
c.*,
ISNULL(l.LeaveApply_Id, 0) AS IsLeave
FROM
Candidates c
LEFT OUTER JOIN Tbl_Stud_Leave_Apply l
on l.Candidate_Id=c.ID AND #Date between Stud_LeaveFromDate and Stud_LeaveToDate;
Related
I want to sum different rows of my table into a different column. I have a Id column which have multiple times. When I try to sum the time table it says cannot sum time data type.
Here is what my query looks like atm:
select t.Id,
a.EndTime,
cast(A.Endtime as time)[time]
from [plugin.tickets].Ticket as T
join
[plugin.tickets].TicketActivity as TA ON TA.TicketId = T.Id
join
dbo.Activity as A on A.Id = TA.ActivityId
Here is the output of this query:
As you can see there are id numbers which have multiple times. How can I sum these values?
EDIT:
I have changed my query as following:
select t.Id
,a.EndTime
,convert (varchar(5), EndTime,108) as Tijd
from [plugin.tickets].Ticket as T
join
[plugin.tickets].TicketActivity as TA ON TA.TicketId = T.Id
join
dbo.Activity as A on A.Id = TA.ActivityId
I just want to sum these where the ID number is the same.
Thanks,
Shabby
You should look into the two functions DATEPART and DATEADD. I'm assuming your useing T-SQL here. So you would take the time field convert that to hour which would give you 17 for example and then use date add on the End Time field.
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017
Below is my query which is going to Generate two rows for me after execution of the query.
SELECT *FROM ELP_COUNTRY,ELP_COUNTRY_Tax where
ELP_COUNTRY.COUNTRY_ID=Elp_Country_Tax.Country_Id
and ELP_COUNTRY.DESCRIPTION='Brasil' and GETENGLISHDESCFROMLISTITEM(ELP_COUNTRY_TAX.TAX_TYPE_ID)='Premiums'
and ELP_COUNTRY_TAX.DEALER_ID is null
This query is returning two rows and again from that rows there is a date named effective date. so again from those rows I want to get the Highest date.
Can anyone help me on this. I am new to Oracle
You could self-join the table with the effective date and evaluate against that by adding something like this: (note the joins may not be correct since I'm just guessing what table the effective date is in and how the tables are related)
and elp_country.effective_date = (select max(e2.effective_date) from elp_country e2 where e2.country_id = elp_country.country_id and elp_country.description = 'Brasil')
Or if it's just a grouping thing you're trying to do then your select would be something like this:
SELECT max(effective_date), column2, column3
FROM ELP_COUNTRY,
ELP_COUNTRY_Tax
where ELP_COUNTRY.COUNTRY_ID=Elp_Country_Tax.Country_Id
and ELP_COUNTRY.DESCRIPTION='Brasil' and
GETENGLISHDESCFROMLISTITEM(ELP_COUNTRY_TAX.TAX_TYPE_ID)='Premiums'
and ELP_COUNTRY_TAX.DEALER_ID is null
group by column2, column3;
You could use the rank function, and wrap that with a "select *" and a where clause predicate filtering the rank of 1, like so:
SELECT * FROM (
SELECT
c.*,
ct.*,
rank() over (order by ct.effective_date) as eff_date_rnk
FROM
ELP_COUNTRY c,
ELP_COUNTRY_Tax ct
WHERE
ELP_COUNTRY.COUNTRY_ID = Elp_Country_Tax.Country_Id
and ELP_COUNTRY.DESCRIPTION='Brasil'
and GETENGLISHDESCFROMLISTITEM(ELP_COUNTRY_TAX.TAX_TYPE_ID)='Premiums'
and ELP_COUNTRY_TAX.DEALER_ID is null
) t
WHERE t.eff_date_rnk = 1;
Tried to come up with SQL query in MS Access, but null values and an aggregate function have me stumped. Any help appreciated.
Query to show records from TABLE1 where the EditDate (which may be null) is greater than the maximum LastImportDate from TABLE2.
TABLE1
Field Name - Data Type
ReportID - Number
EditDate - Date/Time
TABLE2
Field Name - Data Type
LastImportDate - Date/Time
Thank you.
SELECT *
FROM table1
WHERE editDate > (
SELECT max(lastImportDate)
FROM table2
)
not sure how exactly that translates to an access query, but that is the idea.
Additionally, if you could break out the max date as a separate variable, that will get you a bit better performance - something like:
DECLARE #maxDate DATETIME
SET #maxDate = (
SELECT max(lastImportDate)
FROM table2
)
SELECT *
FROM table1
WHERE editDate > #maxDate
Lastly, if you wanted to dates that have a null editDate, you can have the query interpret nulls as some arbitrary date like isNull(editDate, '1900-01-01') - this will make null editDates get interpreted as 1900 Jan 1
Need a help.
I have two timestamp columns, so basically I want to get the max and min value with a thirD column showing as timedifference. I am skipping any 12.am time so used the syntax below. ANy help how to achieve the third column, timedifference.. It is in DB2.
SELECT EMPID,MIN(STARTDATETIME),MAX(ENDDATETIME)
FROM TABLE
WHERE DATE(STARTDATETIME)= '2012-05-15' AND HOUR(STARTDATETIME)<>0 AND HOUR(ENDDATETIME)<>0
GROUP BY EMPID
You can use the results from that in an inner select, and use those values to define the TimeDifference column. My knowledge of DB2 is very limited, so I'm making some assumptions, but this should give you an idea. I'll update the answer if something is drastically incorrect.
Select EmpId,
MinStartDate,
MaxEndDate,
MaxEndDate - MinStartDate As TimeDifference
From
(
Select EMPID,
MIN(STARTDATETIME) As MinStartDate,
MAX(ENDDATETIME) As MaxEndDate
From Table
Where DATE(STARTDATETIME) = '2012-05-15'
And HOUR(STARTDATETIME) <> 0
And HOUR(ENDDATETIME) <> 0
Group By EMPID
) A
I have two tables, TblVal and TblAdj.
In TblVal I have a bunch of values that I need adjusted according to TblAdj for a given TblVal.PersonID and TblVal.Date and then returned in some ViewAdjustedValues. I must apply only those adjustments where TblAdj.Date >= TblVal.Date.
The trouble is that since all the adjustments are either a subtraction or a division, they need to be made in order. Here is the table structure:
TblVal: PersonID, Date, Value
TblAdj: PersonID, Date, SubtractAmount, DivideAmount
I want to return ViewAdjustedValues: PersonID, Date, AdjValue
Can I do this without iterating through TblAdj using a WHILE loop and an IF block to either subtract or divide as necessary? Is there some nested SELECT table magic I can perform that would be faster?
I think you can do it without a loop, but whether you want to or not is another question. A query that I think works is below (SQL Fiddle here). The key ideas are as follows:
Each SubtractAmount has the ultimate effect of subtracting SubtractAmount divided by the product of all later DivideAmounts for the same PersonID. The Date associated with the PersonID isn't relevant to this adjustment (fortunately). The CTE AdjustedAdjustments contains these adjusted SubtractAmount values.
The initial Value for a PersonID gets divided by the product of all DivideAmount values on or after that persons Date.
EXP(SUM(LOG(x))) works as an aggregate product if all values of x are positive. You should constrain your DivideAmount values to assure this, or adjust the code accordingly.
If there are no DivideAmounts, the associated product is NULL and changed to 1. Similarly, NULL sums of adjusted SubtractAmount values are changed to zero. A left join is used to preserve an values that are not subject to any adjustments.
SQL Server 2012 supports an OVER clause for aggregates, which was helpful here to aggregate "all later DivideAmounts."
WITH AdjustedAdjustments AS (
select
PersonID,
Date,
SubtractAmount/
EXP(
SUM(LOG(COALESCE(DivideAmount,1)))
OVER (
PARTITION BY PersonID
ORDER BY Date
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
)
) AS AdjustedSubtract,
DivideAmount
FROM TblAdj
)
SELECT
p.PersonID,
p.Value/COALESCE(EXP(SUM(LOG(COALESCE(DivideAmount,1)))),1)
-COALESCE(SUM(a.AdjustedSubtract),0) AS AmountAdjusted
FROM TblVal AS p
LEFT OUTER JOIN AdjustedAdjustments AS a
ON a.PersonID = p.PersonID
AND a.Date >= p.Date
GROUP BY p.PersonID, p.Value, p.Date;
Try something like following:
with CTE_TblVal (PersonID,Date,Value)
as
(
select A.PersonID, A.Date, A.Value
from TblVal A
inner join TblAdj B
on A.PersonID = B.PersonID
where B.Date >= A.Date
)
update CTE_TblVal
set Date = TblAdj.Date,
Value = TblAdj.Value
from CTE_TblVal
inner join TblAdj
on CTE_Tblval.PersonID = TblAdj.PersonID
output inserted.* into ViewAdjustedValues
select * from ViewAdjustedValues