I want to add this specific line of code to my ENDDATE parameter to verify a date range. I want to add this code
Set #ENDDATE = DateAdd(dd,1,#ENDDATE)
But im not sure where to put it, whenever in the report parameters query or not
Thanks
Just put it in your query:
SELECT SomeFields
FROM V_SubmissionDate
WHERE (V_SubmissionDate.DATE_SUBMITTED >= #BEGINDATE)
AND (V_SubmissionDate.DATE_SUBMITTED <= DateAdd(dd,1,#ENDDATE))
AND (APPLICATION.APP_DECISION_CODE <> 5)
Related
I want to put this request (SELECT qte FROM table_stock where id_pro = cpt1) into an integer, like that i will be able to check if the product still exist (if quantity >0) so I us
SELECT qte
into quantite
FROM table_stock
where id_pro = cpt1;
More details in the PL/pgSQL reference
Try to avoid the loop with a single query like insert into table_result select... where quantity > 0;
I have a table with dated conversions rates. The startdate are both in the past and the future. What I want to do is to find the latest startdate that is before today.
I have tried to use MAX(DatedConversionRate.startdate) to fetch the latest date possible, and I tried to use a WHERE statement that find the latest day before today DatedConversionRate_Start_date< CURRENT_TIMESTAMP().
This is what I have tested, but it can't recognize the DatedConversionRate_Max_Start_date field:
SELECT
Opportunity.id,
Opportunity.currencyisocode,
MAX(DatedConversionRate.startdate) AS `DatedConversionRate_Max_Start_date`,
DatedConversionRate.conversionrate AS `DatedConversionRate_Conversion_rate`,
DatedConversionRate.isocode AS `DatedConversionRate_Iso_code`
FROM
`dataset.Opportunity` Opportunity
LEFT JOIN
`dataset.DatedConversionRate` DatedConversionRate
ON DatedConversionRate.isocode = Opportunity.currencyisocode
WHERE
`DatedConversionRate_Max_Start_date` = `DatedConversionRate_Start_date`
AND `DatedConversionRate_Start_date`< CURRENT_TIMESTAMP()
How can I fix this?
You declared the names of the columns in the SELECT statement, it will return an error if you use these names in your following WHERE statement.
Did you try this:
SELECT
Opportunity.id,
Opportunity.currencyisocode,
MAX(DatedConversionRate.startdate) AS `DatedConversionRate_Max_Start_date`,
DatedConversionRate.conversionrate AS `DatedConversionRate_Conversion_rate`,
DatedConversionRate.isocode AS `DatedConversionRate_Iso_code`
FROM
`dataset.Opportunity` Opportunity
LEFT JOIN
`dataset.DatedConversionRate` DatedConversionRate
ON DatedConversionRate.isocode = Opportunity.currencyisocode
WHERE
MAX(DatedConversionRate.startdate) = `DatedConversionRate_Start_date`
AND MAX(DatedConversionRate.startdate) < CURRENT_TIMESTAMP()
I am trying to return sums with their specific conditions.
SELECT
COUNT(*),
SUM("transactionTotal" WHERE "entryType"=sold) as soldtotal,
SUM(case when "entryType"=new then "transactionTotal" else 0 end) as newtotal
FROM "MoneyTransactions"
WHERE cast("createdAt" as date) BETWEEN '2020-10-08' AND '2020-10-09'
I am trying to sum up the rows with "entryType"=sold and "entryType"=new and return those values separately.
obviously both my logic are wrong.
can someone lend a hand.
You were on the right track to use conditional aggregation, but your syntax is slightly off. Try this version:
SELECT
COUNT(*) AS total_cnt,
SUM(transactionTotal) FILTER (WHERE entryType = 'sold') AS soldtotal,
SUM(transactionTotal) FILTER (WHERE entryType = 'new') AS newtotal
FROM MoneyTransactions
WHERE
createdAt::date BETWEEN '2020-10-08' AND '2020-10-09';
Note: Assuming your createdAt column already be date, then there is no point in casting it. If it is text, then yes you would need to convert it, but you might have to use TO_DATE depending on its format.
I need to update my date with a subquery like this, i aint got a day number .
UPDATE grh
SET date = to_date('subquery.year-subquery.week_number','YYYYWW')
FROM (SELECT year,week_number
FROM grh) AS subquery
how could i use the subquery.year variable inside the to_date fonction ?
TRY 1 : So I've tried this :
UPDATE grh SET date = to_date(week_number, 'WW');
And i have this kind of error :
function to_date(integer,unknown) doesnt exists.
but if you look at this doc : https://www.techonthenet.com/postgresql/functions/to_date.php
They say that WW exists, for specifiing a week number. My 'date' column is a date format .
TRY 2 : this is working :
UPDATE grh SET date = to_date('42018', 'WWYYYY');
As soon as I try to use a variable like that, it doesnt work :
UPDATE grh SET date = to_date(string_agg(week_number,2018), 'WWYYYY');
In Postgres( and most other SQL flavors ), the concatenation operator is ||
UPDATE grh SET date = to_date(week_number||'2018', 'WWYYYY');
The linked documentation on to_date function :
to_date( string1, format_mask )
and the error is
function to_date(integer,unknown) doesnt exists.
so i think that you have a mismatch on the type of your first parameter
Edit1:
for the statement
UPDATE grh SET date = to_date(string_agg(week_number,2018), 'WWYYYY');
if you chekc the documentation of the function string_agg you will see the expected type of input params are string and at least the second one is a integer
SSRS parameters are a pain. I want to be able to re-use reports for many different needs by allowing the users access to many different parameters and making them optional.
So, if I start out with code such as:
Select * from mytable myt
where myt.date between '1/1/2010' and '12/31/2010'
and year(myt.date) = '2010'
and myt.partnumber = 'XYZ-123'
I want those parameters to be optional so my first attempts were to make the parameters default to null such as:
and (myt.partnumber = (#PartNumber) or (#PartNumber) is null)
That has problems because if the database fields in question are nullable then you will drop records because null does not equal null.
I then used code such as this:
DECLARE #BeginDate AS DATETIME
DECLARE #EndDate AS DATETIME
DECLARE #PartNumber AS VARCHAR(25)
SET #Year = '..All'
SET #BeginDate = '1/1/2005'
SET #EndDate = '12/31/2010'
SET #PartNumber = '..All'
SET #Year = '..All'
Select * from mytable myt
where (myt.date between (#BeginDate) and (#EndDate))
and (year(myt.date) = (#Year) or (#Year) = '..All' )
and (myt.partnumber = (#PartNumber) or (#PartNumber) = '..All')
That doesn't work because Year(myt.date) is an integer and #Year is not.
So, here are my questions.
How can I make my dates optional? Is the best way to simply default them to dates outside of a practical range so I return all values?
What is the best way to handle the null or '..All' options to make my queries as readable as possible and allow my users to have optional parameters for most data types? I'd rather not use null for
Go ahead and allow nulls, which indicates the filter should not be applied. Then, you can use the following:
SELECT *
FROM mytable myt
WHERE COALESCE(myt.date, '1/1/1900') between COALESCE(#BeginDate, myt.date, '1/1/1900') and COALESCE(#EndDate, myt.date, '1/1/1900')
AND COALESCE(YEAR(myt.date), -1) = COALESCE(#Year, YEAR(myt.date), -1)
AND COALESCE(myt.partnumber, -1) = COALESCE(#PartNumber, myt.partnumber, -1)
In summary, if any variable value is NULL, then compare the column value to itself, which effectively ignores the condition. More specifically, when testing myt.date, if #BeginDate is NULL then set the lower range value equal to the myt.date value. Do the same substitution with the #EndDate value. Even, if both #BeginDate and #EndDate are NULL, the condition will be true.
A similar approach is used for YEAR(myt.date) and myt.partnumber. If the variable value is NULL, then compare the column value to itself, which is always true.
UPDATE:
Added a default value to each COALESCE to handle the situation where the column value is NULL.
I like your third code block. It seems like your WHERE clause could be corrected to work with a non-int value. The AND clause for the year line would look like this--not my best T-SQL, but it should get you pointed in the right direction:
and 1 = CASE #Year WHEN '..All' THEN 1 ELSE CASE WHEN year ( myt.date ) = CONVERT ( int, #Year ) THEN 1 ELSE 0 END END
This will allow you to have a string value of '..All' or an int value. Either will match correctly. You can do the same with partnumber.
try it like this, the key is to fix your null parameters values to surrogate nulls, also since sql server supports short circuit evaluation, putting the null check should generally perform better.
Select * from mytable myt
where (myt.date between (#BeginDate) and (#EndDate))
and (#Year IS NULL OR COALESCE(myt.date,'1900') = #Year)
and (#PartNumber IS NULL OR ISNULL(myt.partnumber, '<NULL>') = (#PartNumber)