If I have an sql statement:
select call_id, title, description, due_date
from calls
I want to include a column if the condition due_date < getdate() is true.
I was thinking this column would be of type bit, and be set to 1 if condition is true and 0 if condition is false.
Know how I can do this?
select
call_id,
title,
description,
due_date,
case when due_date < getdate() then 1 else 0 end
from calls
SELECT call_id, title, description, due_date,
CASE WHEN due_date < getdate() THEN CAST(1 AS BIT) ELSE 0 END overdue
FROM calls
Related
I have a select clause with a case statement and I need to create another case statement comparing the column created by the previous case statement. Something like this:
select client
,discount
,(case when sales_avg>10000 then 30
when sales_avg>5000 then 20
else 0 end) discount_rule
,(case when discount < discount_rule then 1 else 0 end) status
from sales;
I get a message that discount_rule is unknown. How can I accomplish that?
You can use a Common Table Expression (CTE) and reference a CTE within a CTE as:
with CTE_discount_rule as
(
select client,
discount,
(case when sales_avg>10000 then 30
when sales_avg>5000 then 20
else 0 end) as discount_rule
from sales
),
CTE_Final_Status as
(
select client,
discount,
discount_rule,
(case when discount < discount_rule then 1 else 0 end) as status
from CTE_discount_rule
)
select * from CTE_Final_Status;
The simplest way is to use a subquery that returns the column discount_rule:
select t.client, t.discount, t.discount_rule,
case
when discount < discount_rule then 1
else 0
end status
from (
select client, discount,
case
when sales_avg > 10000 then 30
when sales_avg > 5000 then 20
else 0
end discount_rule
from sales
) t
I have a simple table in SQL server 2008.
SELECT TOP lastupdated, remarks FROM table
I want to select the following criteria.
if remarks is 'Others' and DATEDIFF(MINUTE, LastUpdated, GETDATE() ) > 15
then select these rows
Additionally, I want to select following in the same query
if remarks is not 'Others'
then select these rows without waiting for 15 minutes
You can do the following:
select LastUpdated, Remarks
from table
where (remarks = 'Others' and datediff(min, LastUpdated, GETDATE()) > 15)
or remarks != 'Others'
You can adjust the TOP x, according to your needs, since you didn't put it in your question so I didn't know how many rows you needed to select.
A case expression should help you here:
SELECT TOP lastupdated, remarks
FROM mytable
WHERE DATEDIFF(MINUTE, LastUpdated, GETDATE() ) > CASE remarks WHEN 'Others' THEN 15 ELSE -1 END
You should be able to just nest your WHERE clause like this:
SELECT lastupdated , remarks
FROM table
WHERE remarks != 'Others'
OR
(
remarks = 'Others'
AND
DATEDIFF(MINUTE, LastUpdated, GETDATE() ) > 15
)
Just keep first to condition in braces and then add OR condition like below:
SELECT TOP lastupdated , remarks FROM table
WHERE (remarks = 'Others' AND DATEDIFF(MINUTE, LastUpdated, GETDATE()) > 15)
OR remarks <> 'Others'
Hope it helps.
I searched for many solutions on SO and elsewhere but couldn't quite understand how to write a query for my problem.
Anyway my query looks like below
SELECT * FROM
(
SELECT Id, Date, Name, Amount,
CASE
WHEN DATEDIFF(DAY,Date,GETDATE()) <=0
THEN 'Current'
WHEN DATEDIFF(DAY,Date,GETDATE()) <30
THEN 'Due30'
WHEN DATEDIFF(DAY,Date,GETDATE()) <60
THEN 'Due60'
ELSE 'Due90'
END AS [Age]
FROM Statement
WHERE (Amount <> 0)
) AS S
PIVOT
(
SUM(Amount)
FOR[Age] IN ([Current],[Due30],[Due60],[Due90])
) P
and the result looks like this
Id Date Name Current Due30 Due60 Due90
----------- ---------- --------------------------------------------
1 2016-04-03 Alan NULL NULL NULL 110.00
2 2016-05-02 TC NULL NULL 30.00 NULL
where should i insert IsNull condition to be able to remove the null in the result and add a zero there.
I tried inserting IsNull in the pivot query but we all know that is not meant to work
You have to add it repetitively in the final SELECT, when you replace the SELECT * (which should only exist in ad-hoc queries or EXISTS tests) with the column list:
SELECT
Id,
Date,
Name,
COALESCE([Current],0) as [Current],
COALESCE(Due30,0) as Due30,
COALESCE(Due60,0) as Due60,
COALESCE(Due90,0) as Due90
FROM
(
SELECT Id, Date, Name, Amount,
CASE
WHEN DATEDIFF(DAY,Date,GETDATE()) <=0
THEN 'Current'
WHEN DATEDIFF(DAY,Date,GETDATE()) <30
THEN 'Due30'
WHEN DATEDIFF(DAY,Date,GETDATE()) <60
THEN 'Due60'
ELSE 'Due90'
END AS [Age]
FROM Statement
WHERE (Amount <> 0)
) AS S
PIVOT
(
SUM(Amount)
FOR[Age] IN ([Current],[Due30],[Due60],[Due90])
) P
I've also used COALESCE since it's generally the preferred option (ANSI standard, extends to more than two arguments, applies normal type precedence rules) instead of ISNULL.
SELECT Id
, [Date]
, Name
, [Current] = SUM(CASE WHEN val <= 0 THEN Amount ELSE 0 END)
, Due30 = SUM(CASE WHEN val < 30 THEN Amount ELSE 0 END)
, Due60 = SUM(CASE WHEN val < 60 THEN Amount ELSE 0 END)
, Due90 = SUM(CASE WHEN val >= 60 THEN Amount ELSE 0 END)
FROM dbo.[Statement] t
CROSS APPLY (
SELECT val = DATEDIFF(DAY, [Date], GETDATE())
) s
WHERE Amount <> 0
GROUP BY Id, [Date], Name
I have to write a SQL query and I am confused either I can solve it by self join or using an inner query.
I have a table containing columns
UserID, DateSubscription, Status
Status can be 'E' or 'R'.
I have to select users that has subscription Date between Date1 and Date2 and that UserID should not load who have Status = 'E' and is not in Date Range between Date1 And Date2.
UserID can exist in multiple rows with different status
Below is the query i tried
SELECT
IDNO,
IND_ID,
IND_SRC,
EXPIRY_DATE,
RECSTA
FROM
VIND,
VSCR
WHERE
VIND.IND_ID = VSCR.IND_ID
AND
VIND.IND_SRC = VSCR.IND_SRC
AND
EXPIRY_DATE >= '2015-04-25'
AND
EXPIRY_DATE <= '2015-06-25'
AND
(
RECSTA <> 'E'
AND
EXPIRY_DATE > '2015-06-25'
)
I'd use NOT EXISTSto check the second requirement
SELECT
UserID
FROM
Subscriptions
WHERE
DateSubscription BETWEEN #date1 and #date2
AND NOT EXISTS
(SELECT
NULL
FROM
Subscriptions OtherSubscriptions
WHERE
-- Get UserIDs other rows
Subscriptions.UserID = OtherSubscriptions.UserID
-- Only rows that have status 'E'
AND OtherSubscriptions.Status = 'E'
-- subscription is before #date1 or after #date2
AND (OtherSubscriptions.DateSubscription < #date1
OR OtherSubscriptions.DateSubscription > #date2)
One way to do this would be to group by on userid and filter the data using HAVING and CASE.
SELECT UserID, DateSubscription, Status
FROM UserStatusTable
GROUP BY UserID
HAVING SUM(CASE WHEN DateSubscription BETWEEN Date1 AND Date2 THEN 1 ELSE 0 END) > 1
AND SUM(CASE WHEN DateSubscription NOT BETWEEN Date1 AND Date2 AND Status = 'E' THEN 1 ELSE 0 END) = 0
Try This.
select UserID, DateSubscription
from table
where DateSubscription between #startDate and #endDate and
(Status <> 'E' OR DateSubscription not between #startDate and #endDate)
If I understand correctly you want all records where the expiry date is between A and B, and also all records outside this date where the status is NOT E
select *
from yourtable
where EXPIRY_DATE between #startDateParam and #endDateParam
or [Status] <> 'E'
say I have the following query:
select ID, ActualDate, DueDate
from table1
What I need to do is to add another field called Flag
which will be marked as "Y" if ActualDate is greater than DueDate
select ID, ActualDate, DueDate,
CASE
WHEN ActualDate > DueDate THEN 'Y'
ELSE 'N'
END as Flag
from table1
The above won't work as I get invalid column name ActualDate. Invalid column name DueDate.
What I need to do is a select within a select like this:
select ID, ActualDate, DueDate,
CASE
WHEN ActualDate > DueDate THEN 'Y'
ELSE 'N'
END as Flag
from
(select ID, ActualDate, DueDate
from table1) tbl1
)
If your table has the fields in it, then the following should work without the need for a subquery:
select ID,
ActualDate,
DueDate,
CASE
WHEN ActualDate > DueDate
THEN 'Y'
ELSE 'N'
END as Flag
FROM table1
You can use a subquery but it is unnecessary:
select ID,
ActualDate,
DueDate,
CASE
WHEN ActualDate > DueDate
THEN 'Y'
ELSE 'N'
END as Flag
FROM
(
select ID, ActualDate, DueDate
from table1
) tbl1