tanj1035
asked on
SQL, the table does not return value
Hi Expert,
If @Dealershipname = A which has 4 testdrive , the query will return the table with values.
If @Dealershipname = B which has 0 testdrive, the query will not return the values, only the row header.
Do you have any suggestions, thanks.
SELECT dt.[dealerid]
,dt.[name]
,dt.[Alternatedealmtd]
,dt.[ManualDealmtd]
,dt.[wtd]
,dt.[mtd]
,dt.[lastmonth]
,dt.lastmonthtotal
,dt.[trending]
,isnull(COUNT(t.dealid),0) as testdrive
FROM [MMD_Feed].[dbo].[DART] AS dt
INNER JOIN Mmd_feed..dealview AS dv
on dt.dealerid=dv.dealerid
INNER JOIN Mmd_feed..deal d
on dv.dealid=d.dealid
LEFT OUTER JOIN [MakeMyDeal_com].[dbo].[Te stDrive] t
on t.dealid=d.dealid
where name in (@DealershipName)
and t.dealid in (select t.dealid from [MakeMyDeal_com].[dbo].[Te stDrive]
where cast(d.submittedtimestamp as date)>=DateAdd("d",-(Day(g etdate())- 1), getdate()) and cast(d.submittedtimestamp as date)<getdate())
group by dt.[dealerid]
,[name]
,[Alternatedealmtd]
,[ManualDealmtd]
,[wtd]
,[mtd]
,[lastmonth]
,lastmonthtotal
,[trending]
If @Dealershipname = A which has 4 testdrive , the query will return the table with values.
If @Dealershipname = B which has 0 testdrive, the query will not return the values, only the row header.
Do you have any suggestions, thanks.
SELECT dt.[dealerid]
,dt.[name]
,dt.[Alternatedealmtd]
,dt.[ManualDealmtd]
,dt.[wtd]
,dt.[mtd]
,dt.[lastmonth]
,dt.lastmonthtotal
,dt.[trending]
,isnull(COUNT(t.dealid),0)
FROM [MMD_Feed].[dbo].[DART] AS dt
INNER JOIN Mmd_feed..dealview AS dv
on dt.dealerid=dv.dealerid
INNER JOIN Mmd_feed..deal d
on dv.dealid=d.dealid
LEFT OUTER JOIN [MakeMyDeal_com].[dbo].[Te
on t.dealid=d.dealid
where name in (@DealershipName)
and t.dealid in (select t.dealid from [MakeMyDeal_com].[dbo].[Te
where cast(d.submittedtimestamp as date)>=DateAdd("d",-(Day(g
group by dt.[dealerid]
,[name]
,[Alternatedealmtd]
,[ManualDealmtd]
,[wtd]
,[mtd]
,[lastmonth]
,lastmonthtotal
,[trending]
Unclear on what your goal is.
Is this your goal or your current problem? If the former then what are you getting now, if latter then what are you trying to achieve?
If @Dealershipname = A which has 4 testdrive , the query will return the table with values.
If @Dealershipname = B which has 0 testdrive, the query will not return the values, only the row header.
Is this your goal or your current problem? If the former then what are you getting now, if latter then what are you trying to achieve?
this is because you are using AND condition here. Change AND to OR and see the dirrerence.
where name in (@DealershipName)
and t.dealid in (select t.dealid from [MakeMyDeal_com].[dbo].[Te stDrive]
where name in (@DealershipName)
and t.dealid in (select t.dealid from [MakeMyDeal_com].[dbo].[Te
ASKER
hi Brain, sorry, this is the problem.
I want to get the table return value even dealershipename B has 0 test drive.
I want to get the table return value even dealershipename B has 0 test drive.
ASKER
Vitor and Deepak, thanks for your replies. Sorry, the queries do not work. It still cannot return value for the dealershipname B
Please provide some sample data so we can test before providing you an assertive answer.
ASKER
Sure, how you want me to provide the sample data? Thanks.
Few sample rows for each table. If the tables contains sensitive data don't forget to mask them.
Tables:
[MMD_Feed].[dbo].[DART]
Mmd_feed..dealview
Mmd_feed..deal
[MakeMyDeal_com].[dbo].[Te stDrive]
Tables:
[MMD_Feed].[dbo].[DART]
Mmd_feed..dealview
Mmd_feed..deal
[MakeMyDeal_com].[dbo].[Te
ASKER
Vitor, please find attached excel, thx.
Sample.xlsx
Sample.xlsx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, Vitor. Yours worked out!!
I was originally trying to get the "isnull(COUNT(t.dealid),0) as testdrive" from "t.dealid in (select t.dealid from [MakeMyDeal_com].[dbo].[Te stDrive] where cast(d.submittedtimestamp as date)>=DateAdd("d",-(Day(g etdate())- 1), getdate()) and cast(d.submittedtimestamp as date)<getdate())
It means the count has to be in [testdrive] from the date range from another table [deal].
Yours worked successfully without "t.dealid in (select t.dealid from [MakeMyDeal_com].[dbo].[Te stDrive]" . So I do not need to especially to indicate dealid in [testdrive] in the query?
I was originally trying to get the "isnull(COUNT(t.dealid),0)
It means the count has to be in [testdrive] from the date range from another table [deal].
Yours worked successfully without "t.dealid in (select t.dealid from [MakeMyDeal_com].[dbo].[Te
No, you don't because you already did in the JOIN clause:
LEFT OUTER JOIN [MakeMyDeal_com].[dbo].[Te stDrive] t on t.dealid=d.dealid
LEFT OUTER JOIN [MakeMyDeal_com].[dbo].[Te
ASKER
good point!!
(...) where (name in (@DealershipName) or name is null)
and (...)