|
select DrugTherapy.dtid,DrugTherapy.drugtherapy,<br />
(select count (dtid) from ReferralDrugTherapy where dtid = DrugTherapy.dtid) <br />
from DrugTherapy
I Love T-SQL
|
|
|
|
|
Im slightly confused now, did you mean to add that bit to replace the top like so?
select DrugTherapy.dtid,DrugTherapy.drugtherapy,
(select count (dtid) from ReferralDrugTherapy where dtid = DrugTherapy.dtid)
from DrugTherapy INNER JOIN
ReferralDrugTherapy ON DrugTherapy.DTID = ReferralDrugTherapy.DTID INNER JOIN
CardiacReferrals ON ReferralDrugTherapy.ReferralID = CardiacReferrals.ReferralID
WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to) OR
(CardiacReferrals.DateSubmitted IS NULL)
GROUP BY DrugTherapy.DrugTherapy
Or something else? That query alone works, but how would i enter my date ranges still?
Many thanks
|
|
|
|
|
How about if you try to make that first join an outer join, like this:
SELECT DrugTherapy.DrugTherapy, COUNT(ReferralDrugTherapy.DTID) AS Expr1<br />
FROM DrugTherapy LEFT OUTER JOIN<br />
ReferralDrugTherapy ON DrugTherapy.DTID = ReferralDrugTherapy.DTID INNER JOIN<br />
CardiacReferrals ON ReferralDrugTherapy.ReferralID = CardiacReferrals.ReferralID<br />
WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to) OR<br />
(CardiacReferrals.DateSubmitted IS NULL)<br />
GROUP BY DrugTherapy.DrugTherapy
An inner join will join two tables together and return only the records where they match in the "ON" portion. A left outer join will join two tables and return the records where they match in the "ON" portion but will also return any records that didn't match in the table on the left of the "LEFT OUTER JOIN" statement. Be aware that this may bring back null values for fields that you reference in the table on the right side of the join.
If you want to do some more research on this, I'd suggest searching for the keywords "OUTER JOIN SQL" on google.
Hope this helps.
|
|
|
|
|
That source didnt do the trick either
Ive been searching on this but not to much luck, theres plenty on joining the 2 tables but 3 isnt particularly good
|
|
|
|
|
I think I should have put a LEFT OUTER JOIN for both of the joins in your statement. Try that and see if it gets you anywhere.
|
|
|
|
|
I think I got it (just change date values in according to your format string, instead yyyy-MM-dd set your format)
SELECT CASE WHEN dbo.ReferralDrugTherapy.DTID IS NULL THEN<br />
(SELECT TOP 1 t1.DTID<br />
FROM DrugTherapy AS t1<br />
WHERE t1.DrugTherapy IN (dbo.DrugTherapy.DrugTherapy)) ELSE dbo.ReferralDrugTherapy.DTID END AS dtid, dbo.DrugTherapy.DrugTherapy, <br />
COUNT(dbo.ReferralDrugTherapy.DTID) AS numbers<br />
FROM dbo.ReferralDrugTherapy FULL OUTER JOIN<br />
dbo.DrugTherapy ON dbo.ReferralDrugTherapy.DTID = dbo.DrugTherapy.DTID <br />
<br />
left outer join CardiacReferrals on dbo.ReferralDrugTherapy.referralid = CardiacReferrals.referralid<br />
and CardiacReferrals.datesubmitted between '2008-01-01' and '2008-02-21' <br />
GROUP BY dbo.ReferralDrugTherapy.DTID, dbo.DrugTherapy.DrugTherapy, dbo.DrugTherapy.DTID
I Love T-SQL
|
|
|
|
|
Not quite, it doesnt seem to matter what date range you put it, it always pulls back all the results for each drug type. I tried changing it slightly so the dates in a where to
SELECT CASE WHEN dbo.ReferralDrugTherapy.DTID IS NULL THEN<br />
(SELECT TOP 1 t1.DTID<br />
FROM DrugTherapy AS t1<br />
WHERE t1.DrugTherapy IN (dbo.DrugTherapy.DrugTherapy)) ELSE dbo.ReferralDrugTherapy.DTID END AS dtid, DrugTherapy.DrugTherapy, <br />
COUNT(ReferralDrugTherapy.DTID) AS numbers<br />
FROM ReferralDrugTherapy FULL OUTER JOIN<br />
DrugTherapy ON ReferralDrugTherapy.DTID = DrugTherapy.DTID LEFT OUTER JOIN<br />
CardiacReferrals ON ReferralDrugTherapy.ReferralID = CardiacReferrals.ReferralID<br />
WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to)<br />
GROUP BY ReferralDrugTherapy.DTID, DrugTherapy.DrugTherapy, DrugTherapy.DTID
but all that did was give me the same problem where if it doesnt find anything it brings back nothing instead of 0 values. Argh!
|
|
|
|
|
write date values which you are giving?
I Love T-SQL
|
|
|
|
|
Well say if i put in 01/01/2008 - 01/05/2008 it pulls back records as there are ones within that range. (But also will not display those that dont have anything as 0, just wont display) But say i put in 01/05/2008 - 02/05/2008 then it pulls back nothing
|
|
|
|
|
between dates 01/05/2008 - 02/05/2008 you need this result :
DTID DrugTherapy CountNumbers<br />
2 Drug2 0<br />
3 Drug3 0<br />
1 Drug1 5<br />
4 Drug4 1
I Love T-SQL
|
|
|
|
|
Yes, so if there is none made, it to display a 0 so i can display reports on a page.
Thanks
|
|
|
|
|
change date format to dd/MM/yyyy from regoinal Settings on control panel, then
on date parameters ad values like this '01/01/2008' - '02/01/2008'
by that changes,query works perfeclty for me
here it is
SELECT CASE WHEN dbo.ReferralDrugTherapy.DTID IS NULL THEN
(SELECT TOP 1 t1.DTID
FROM DrugTherapy AS t1
WHERE t1.DrugTherapy IN (dbo.DrugTherapy.DrugTherapy)) ELSE dbo.ReferralDrugTherapy.DTID END AS dtid, dbo.DrugTherapy.DrugTherapy,
COUNT(dbo.ReferralDrugTherapy.DTID) AS numbers
FROM dbo.ReferralDrugTherapy FULL OUTER JOIN
dbo.DrugTherapy ON dbo.ReferralDrugTherapy.DTID = dbo.DrugTherapy.DTID
left outer join CardiacReferrals on dbo.ReferralDrugTherapy.referralid = CardiacReferrals.referralid
and CardiacReferrals.datesubmitted between '01/01/2008' and '01/05/2008'
GROUP BY dbo.ReferralDrugTherapy.DTID, dbo.DrugTherapy.DrugTherapy, dbo.DrugTherapy.DTID
I Love T-SQL
|
|
|
|
|
My date settings are already set to that.
The query that you have given me also just gives the total made, no matter what dates you enter you always get the total, it doesnt calculate any differences.
Thanks for your help so far!
|
|
|
|
|
I will lookup later again and I will notify you
I Love T-SQL
|
|
|
|
|
Many thanks, if i crack it in the mean time without blowing my head off ill let you know!
|
|
|
|
|
ok, if I crack it too I will tell you. On my mind i have solution but I must seriozly deal with it, coz until now I didn't have time to deal with it seriozly.
See u later.
I Love T-SQL
|
|
|
|
|
I guess this time I got the right solution
declare @fromdate as varchar(15)<br />
declare @todate as varchar(15)<br />
set @fromdate = '01/02/2008'<br />
set @todate = '05/02/2008'<br />
select ReferralDrugTherapy.dtid ,count(ReferralDrugTherapy.dtid) as countAll,DrugTherapy.DrugTherapy <br />
from ReferralDrugTherapy,CardiacReferrals,DrugTherapy<br />
where ReferralDrugTherapy.referralid =CardiacReferrals.referralid and datesubmitted <br />
between convert(varchar,@fromdate,103) and convert(varchar,@todate,103) and DrugTherapy.dtid = ReferralDrugTherapy.dtid<br />
group by ReferralDrugTherapy.dtid ,DrugTherapy.DrugTherapy<br />
union all<br />
select dtid,0,DrugTherapy.DrugTherapy from DrugTherapy where dtid not in (<br />
select ReferralDrugTherapy.dtid from ReferralDrugTherapy,CardiacReferrals,DrugTherapy<br />
where ReferralDrugTherapy.referralid =CardiacReferrals.referralid and datesubmitted <br />
between convert(varchar,@fromdate,103) and convert(varchar,@todate,103) and DrugTherapy.dtid = ReferralDrugTherapy.dtid<br />
group by ReferralDrugTherapy.dtid ,DrugTherapy.DrugTherapy)<br />
order by ReferralDrugTherapy.dtid asc
I Love T-SQL
|
|
|
|
|
Hi and thanks.
Afraid not though....
|
|
|
|
|
Sorry for my unusefull answers.
I Love T-SQL
|
|
|
|
|
No need to apologies, your help has been much appreciated, your not the only person it seems to have stumped!
|
|
|
|
|
AdamskiR wrote: your help has been much appreciated
It's my pleasure trying to help others...
Let me know if you find solution? If you have time to post explanation again then do it and after couple of hours I will try again to find solution.
If you post explanation write data how are stored on table, and write result which you want to get.
I Love T-SQL
|
|
|
|
|
We got there in the end!
SELECT d.DrugTherapy, ISNULL(c.count, 0) AS count<br />
FROM DrugTherapy AS d LEFT OUTER JOIN<br />
(SELECT COUNT(ReferralDrugTherapy.DTRID) AS count, ReferralDrugTherapy.DTID<br />
FROM CardiacReferrals INNER JOIN<br />
ReferralDrugTherapy ON CardiacReferrals.ReferralID = ReferralDrugTherapy.ReferralID<br />
WHERE (CardiacReferrals.DateSubmitted BETWEEN @from AND @to)<br />
GROUP BY ReferralDrugTherapy.DTID) AS c ON c.DTID = d.DTID
Many many thanks to everyone
|
|
|
|
|
Cool,Great and Good News...
I Love T-SQL
Don't torture yourself,let the life to do it for you.
|
|
|
|
|
Hi All,
I want to count records any time from Mid day(12:00:00.000') of a given date up to the next day MID Day get all the count in to the first date.
For instance for a date '2008-04-23' count is made between '2008-04-23 12:00:00.000 and 2008-04-24 12:00:00.000'.
If my date Query is Between '2008-04-20' and '2008-04-23' then
1. '2008-04-20' --------- count on '2008-04-20 12:00:00.000 and 2008-04-21 12:00:00.000'.
2. '2008-04-21' ---------- count on '2008-04-21 12:00:00.000 and 2008-04-22 12:00:00.000'.
etc..
Can you give me a script for this one?
Thank you
regards
|
|
|
|
|
hope this will help you...
select count(*) from myTable <br />
where [columndate] >= substring(convert(varchar,columnName,20),1,10)+' 12:00:00.000' and<br />
[columndate] <= substring(convert(varchar,columnName,20),1,10)+' 12:00:00.000'
I Love T-SQL
modified on Thursday, April 24, 2008 4:26 PM
|
|
|
|