TSQL How to make where clause match all 'IN' multiple values - sql

I am using 4 dates in my where clause using IN. For example
...where date in ('2017-01-01', '2017-01-02', '2017-01-03','2017-01-04')
My query will return a result if even one date matches but I want my where clause to match ALL the dates. I'm sure there has to be an easy solution for this.

You want to match your data for a specific column value. Group by that column and take only those groups having all 4 dates
select col
from your_table
where date in ('2017-01-01', '2017-01-02', '2017-01-03','2017-01-04')
group by col
having count(distinct date) = 4

Related

SQL Server : get date range and number of days

I have 4 columns (two columns filter) and one column date and one column flag.
For a combination of column 1 and column 2, and the open flag, I want to find the open date, the close date and number of days where it was open.
Here is my example and what I want to find with SQL is with the blue/grey color
You can use DATEDIFF() function as shown below if you have already two dates available as OpenDate and CloseDate in your case.
Select DATEDIFF(DAY, OpenDate, CloseDate) as NumberOfOpenDays from YourTable
This looks a gaps-and-islands problem -- but the islands are already defined by opendt (and perhaps col1 and col2).
So, you can just use row_number():
select row_number() over (partition by col1, col2, opendt order by effectivedt)

Sql count and and/or condition in single statement

I am trying to build where clause condition on table having columns “Id”, itemNumber” which can be either 1 or 2 for any row and “date”.
My goal is to write where clause such that i only get “Id’s” where “itemNumber” is 2, and then if count is greater than some value it should filter whole rows to date between today and today+1, otherwise today and today+2.
I tried,
Select Id
from table
where itemNumber=2 And ((count(itemNumber)>2 and date between ‘today’ and ‘today+1’) OR (count(itemNumber)<=2 and date between ‘today’ and ‘today+2’))
I got error saying you need to have sql “having”. Am i doing it wrong?
Try it like this:
SELECT id
FROM t
WHERE itemNumber = 2
GROUP BY id
HAVING (COUNT(itemNumber) > 2 AND date BETWEEN 'today' and 'today+1'))
OR (COUNT(itemNumber) <= 2 AND date BETWEEN 'today' and 'today+2'))
Think of HAVING as a WHERE clause after you have grouped your data, which you have to do if you want to count something by group (or id).

Having trouble getting the correct dates from a table

I currently have this query with the date range set to only the 24th of February. Unfortunately my results have dates from all over the place.
Any direction as to why this is happening?
P.S. "TIMESTAMP" is the name of my column (I can't change this).
SELECT
DATE(TIMESTAMP) as dateofEvent,
eventType,
parameters.name,
parameters.value,
membershipStatus,
gender,
COUNT(*)
FROM (
SELECT
*
FROM
TABLE_DATE_RANGE(myFirstTable, TIMESTAMP('2016-02-24 00:00:00'), TIMESTAMP('2016-02-24 23:59:59'))) AS EV
JOIN
mySecondTable AS UD
ON
UD.userId = EV.userId
WHERE
eventType = 'SettingUpdated'
and (UPPER(parameters.value) = 'TRUE' or UPPER(parameters.value) = 'FALSE')
GROUP BY
dateofEvent,
eventType,
gender,
parameters.value,
parameters.name,
membershipStatus
TABLE_DATE_RANGE filters table names, it does nothing with field values.
So if you want to get only fields in specific time range, you have to make sure the field values correspond to table names. In specific case, you need to make sure to add only rows with TIMESTAMP field between '2016-02-24 00:00:00' and '2016-02-24 23:59:59' to the table myFirstTable.20160224. You likely have this violated and the table myFirstTable.20160224 contains TIMESTAMP fields all over the place.
When you use below query
SELECT *
FROM TABLE_DATE_RANGE(myFirstTable,
TIMESTAMP('2016-02-24 00:00:00'),
TIMESTAMP('2016-02-24 23:59:59'))) AS EV
FROM statement is being "translated" and you query is executed as if it were like below (see more in Table wildcard functions)
SELECT *
FROM myFirstTable20160224
So, now the fact that you are getting dates all over the place tells me that you have your timestamp field not limited to the 2016-02-24 but rather being all over the place despite table name
If you need to filter output by date you can add something like
WHERE DATE(timestamp) = '2016-02-24'

SQL: select datetime values prior to that date based on it's value

I want to select rows for a field MRD which is declared as date where it is prior for that date only.
So
(case when sum (transPoints) > 4 and MRD is that same date then 4
So if a row has a date of today, I want the case when to be triggered when the transaction points are bigger than 4 against all columns with the same date.
As you can imagine the date field will be different against many rows.
Based on what I can understand from your question, it seems that the GROUP BY clause may be what you're looking for. If your date column is in the correct format then you may have to use something like:
SELECT CAST(DateColumn as DATE)
FROM YourTable
GROUP BY CAST(DateColumn as DATE)

SQL Difference between using two dates

My database has a table called 'clientordermas'. Two columns of that table are as follows.
execid and filledqty.
Three records of those two fields are as follows.
E02011/03/12-05:57_24384 : 1000
E02011/03/12-05:57_24384 : 800
E02011/03/09-05:57_24384 : 600
What i need to do is get the filledqty diffrence btween latest date and 1 before latest date which is 400(1000-400).
I have extracted the date from the execid as follows:
(SUBSTR (execid, 3, 10)
I tried so hard but but I was unable to write the sql query to get 400. Can someone please help me to do this???
P.S I need to select maximum filled quantity from the same date. That is 1000 not, 800.
You can use window functions to access "nearby" rows, so if you first clean up the data in a subquery and then use window functions to access the next row, you should get the right results. But unless you have an index on substr(execid, 3, 10), this is going to be be slow.
WITH datevalues AS
(
SELECT max(filledqty) maxfilledqty, substr(execid, 3, 10) execiddate
FROM clientordermas
GROUP BY substr(execid, 3, 10)
)
SELECT
execiddate,
maxfilledqty -
last_value(maxfilledqty) over(ORDER BY execiddate DESC ROWS BETWEEN 0 PRECEDING AND 1 FOLLOWING)
FROM datevalues
ORDER BY execiddate DESC;
WITH maxqtys AS (
SELECT substr(a.execid,3,10) AS date, MAX(a.filledqty) AS maxqty
FROM clientordermas a
GROUP BY substr(a.execid,3,10)
)
SELECT a.maxqty-b.maxqty
FROM maxqtys a, maxqtys b
WHERE a.date <> b.date AND ROWNUM=1
ORDER BY a.date DESC, b.date DESC
This first creates a subquery (maxqty) which contains the max filledqty for each unique date, then cross join this subquery to itself, excluding the same rows. This results in a table containing pairs of dates (excluding the same dates).
Sort these pairs by date descending, and the top row till contain the last and 2nd-to-last date, with the appropriate quantities.