webForumDet fria alternativet

Får två tillbaka, men vill ha en.

Databaser & SQL

1 svar · 284 visningar · startad av Fredde Mannen

Medlem sedan nov. 20014 054 inlägg
Frågan#1
select 
	annonser = (select count(distinct custompropertyid) from properties where customer=a.Customer
	and imported between @StartDate and @StopDate)
from properties a
	inner join 
customers b on
	a.Customer=b.customerid 
	where a.customer=@CustomerID
-- end
UNION 
-- SQL-sats beräkna antal annonser för ej importerad kund.
select
	annonser = (
		(select count(*) from properties where customer=a.Customer
			and imported is null and deleted is null)
		+ 
		(select count(deleted) from properties where customer=a.Customer
			and deleted between @StartDate and @StopDate and imported is null)
		)
from properties a
	inner join 
customers b on
	a.Customer=b.customerid
	where a.Customer=@CustomerID

Har denna sql-sats som jag skall sätta in i en udf (user define scalar function) i sql. Nackdelen är att den returerar två rader, hur kan jag få så att den bara returerar en rad., nu returnerar den två rader, en med värdet noll och en med värdet jag är ute efter..

Medlem sedan nov. 20014 054 inlägg
#2

Duktiga jag missade ju att summera ihop dem.. DOH!

select sum(c.annonser) from (
	select 
	(
	select count(distinct custompropertyid) from properties where customer=a.Customer
	and imported between @StartDate and @StopDate
	) As annonser 
from properties a
	inner join 
customers b on
	a.Customer=b.customerid 
	where a.customer=@CustomerID
-- end
UNION 
-- SQL-sats beräkna antal annonser för ej importerad kund som skall
-- vara underlag för fakturering.
select
	(
		(select count(*) from properties where customer=a.Customer
			and deleted is null)
		+ 
		(select count(propertyid) from properties where customer=a.Customer
			and deleted between @StartDate and @StopDate)
	) As annonser
from properties a
	inner join 
customers b on
	a.Customer=b.customerid
	where a.Customer=@CustomerID
) as c

Funkade mycket bättre! :e

356 ms totalt · 4 externa anrop · v20260731065814-full.6fe65c25
120 ms — deklarationer (db)
0 ms — hämta statistik (cache)
137 ms — hämta tråd, inlägg och bilagor (db)
217 ms — ändringar (db)