I am trying to add some report logic to save setting up another dataset. First thing I need is to count the number of rows per date, then number of rows per date where another column states = "Yes".
I have a raw data stored procedure which returns the data as a table, on another tab.
Number of records that match date parameter:
=SUM(IIF(Fields.DATE_FIELD.Value = Parameters!DATE_PARAM.Value, 1, 0))
Number of records that match date parameter where another column = YES:
=SUM(IIF(Fields.DATE_FIELD.Value = Parameters!DATE_PARAM.Value AND Fields.OTHER_FIELD.Value = "Yes", 1, 0))
The IIF checks the conditions and if they are TRUE then return 1 for a record else 0 then it sums these values up.
UPDATE:
I think I misunderstood what you were needing.
You want to have a count of all records by date not just for a particular date.
For that you would want a table that groups by date, then you could just use a COUNTROWS or SUM(1) for the number of records for each date.
Then you could use an IIF to check for the Yes in the other field:
=SUM(IIF(Fields.OTHER_FIELD.Value = "Yes", 1, 0))
Since your table is already grouping by date, you don't need the date in the above expression.
Related
I have a table as follows:
Date / Name / OldValue / NewValue
I want a way to create a Start Date & End Date columns using any of the following : DAX , T-SQL or M Query.
Meaning; the StartDate is the [Date] and the End the date is the Date where the same person will change value.
Thanks in advance.
Using Power Query M, you can follow these steps:
In my demo data the columns came in sorted correctly, but you can use this code to ensure they are:
#"Sort Table" = Table.Sort(#"Changed Type",{{"Name", Order.Ascending}, {"Date", Order.Ascending}})
Add an Index column > Add Column > Index Column > From 0 (It HAS to be from 0)
Add Column > Custom Column > name End Date >
if [Name] <> #"Added Index" [Name] {[Index] + 1} then null else Date.AddDays(#"Added Index" [Date] {[Index] + 1},-1)
Right-click End Date > Replace Errors > Replace with null
Adjust column order as needed
Rename New Value column to "Value", rename Date column to "Start Date". Remove unneeded columns
Source and Output:
EDIT: I should add that this works okay on a small data set, but on a larger one you may run into performance issues. In that case you should duplicate the query, add an index starting at 0 for one query, and on the other query start an index at 1. Then you can merge the two together to extract the necessary dates and values.
I would like to select from a partitioned table where the date is the highest date strictly below a given date d.
I can do the following:
d:2019.10.02;
{select from x where date = max date} select from t where date < d
where t is my partitioned table.
The issue with the above query is that it is very slow as it has to first load all the dates strictly older than d, and then taking the max date out of it.
To select all the dates that are earlier than your specified date you can use the select statement below:
select from t where date=max date where date<d
Where t is your partitioned table and d is your specified date.
If you just want to select from the max date in a date partitioned hdb
Lets assume that the max populated date partition less than 2019.08.20 is 2019.08.07
q)d:2019.08.20
q)select from t where date=max date where date<d
This is because the partition type is available as a variable once you load into a DB, (i.e,. date, month, int etc). This will be the .Q.pf variable.
select from table where date=(last .Q.pv where .Q.pv < d)
kdb+ stores a variable in memory which contains all the dates within your db.
select from telemetry where date=desc[date]1
Above where clause will sort this by largest ->smallest
Selecting index 1 will filter the max date out of your query (without first querying the entire dataset).
I am developing a real-time auction site for a school project. We can't make any changes to the design of the database.
The 'Item' table has a column for the expiration date (the day the auction expires) and the expiration time (the exact time at which the auction expires). It also has a column that indicates whether the auction is opened or closed. This [AuctionClosed?] column needs to be updated when the expiration date and time are reached, which has to happen in real-time.
We're using an SQL Server database and the website runs on PHP7. The only possible solution I've found is to run a job every second, but this is too much overhead.
This is the function I want to use to check the column:
CREATE FUNCTION dbo.fn_isAuctionClosed (#Item BIGINT)
RETURNS BIT
AS
BEGIN
DECLARE #expirationDay DATE = (SELECT expirationDate FROM Item WHERE itemId = #Item)
DECLARE #expirationTime TIME = (SELECT expirationTime FROM Item WHERE itemId = #Item)
IF
DATE(GETDATE()) = #expirationDay AND TIME(GETDATE()) >= #expirationTime
OR
DATE(GETDATE()) > #expirationDay
RETURN 1
RETURN 0
END
And this is the procedure that updates the column:
CREATE PROCEDURE updateAuctionClosed #Item BIGINT
AS
UPDATE Item
SET [AuctionClosed?] = fn_isAuctionClosed(#Item)
WHERE itemId = #Item
To be more specific, what you really want here is a calculated column. Like I said in the comments, as the column will rely on the current date and time, the column won't be deterministic. This means it can't be PERSISTED but would be calculated every time the column is referenced (A PERSISTED column actually has it's value stored and is calculated when the row is effected in some way and restored). Even so, it can be calculated as follows:
ALTER TABLE Item DROP COLUMN [AuctionClosed?]; --You can't alter a column to a computed column, so we have to DROP it first
ALTER TABLE Item ADD [AuctionClosed?] AS CASE WHEN CONVERT(datetime,expirationDate) + CONVERT(datetime, expirationTime) > GETDATE() THEN 1 ELSE 0 END;
On a side note, I recommend against special characters in an object's name. Stick to alphanumerical characters only, and (if you must) underscores (_), as these don't force the object to be delimit identified.
I need to select from a table that has two fields , a start date and an end date , selecting the row that the current date is. Look at the image ...how do i select the row that encompasses the date range 91/201 - 9/15/2018 ? Somehow this was flagged as a duplicate. I am NOT attempting to find overlap. I am attempting to find the row that represents a given date. Say , one table has a field that has the value 9/5/2018 ....how do I select the row that has a start date of 9/1/2018 and end date of 9/15/2018
I have a ssrs report that has a date/time parameter that allows the user to select the date when running report. how to I get it to exclude the time part of the date field.
currently the field in the db is a date/time field so when I run query
select count(*) from table where date <= #dateparameter
it is not including records where the time part of field is greater than 00.00.00
how can I ignore the time part so all records are returned for that date
The simplest (and probably best performance) solution would be to add a day to the date passed by the user amd change the <= to <:
select count(*) from table where date < DATEADD(DAY, 1, #dateparameter)