Use SQL query saved question in another SQL query based question

I have a saved question which gives me default dates when user does not select a date. Now i want to this is question in other sql questions to use startDate and endDate from saved question.
This is my question which gives startDate and endDate.
DECLARE @startDate varchar(30);
DECLARE @endDate varchar(30);
SET @startDate = DATEADD(MONTH,-1,getdate());
SET @startDate = concat(@startDate,’|’ [[,{{start_date}}]] );
SET @endDate = concat(getdate(),’|’ [[,{{end_date}}]] );
IF (LEN(@startDate) > 20)
SET @startDate = SUBSTRING(@startDate , 21,30)
ELSE
SET @startDate = SUBSTRING(@startDate , 0,20)
IF (LEN(@endDate) > 20)
SET @endDate = SUBSTRING(@endDate , 21,30)
ELSE
SET @endDate = SUBSTRING(@endDate , 0,20)
select cast(@startDate as date) as startDate, cast(@endDate as date) as endDate
I want to use the values of startDate and endDate in other sql questions.
Is it possible ?

Hi @babarali
I think you can simplify this to:

SELECT 
  CAST(COALESCE([[{{start_date}}, ]] DATEADD(MONTH,-1,GETDATE())) AS DATE) AS startDate,
  CAST(COALESCE([[{{end_date}}, ]] GETDATE()) AS DATE) AS endDate

And then you would use the new sub-query functionality in 0.35:
https://www.metabase.com/docs/latest/users-guide/13-sql-parameters.html#using-an-existing-question-as-a-sub-query

But there’s a limitation currently, where you cannot use a sub-query or Saved Question, when it has variables:
https://github.com/metabase/metabase/issues/6449 - upvote by clicking :+1: on the first post

So the only solution would be to include the variables in all your questions.

1 Like