---
title: "Får två tillbaka, men vill ha en."
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/161448-får-två-tillbaka-men-vill-ha-en"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "Fredde Mannen"
published: "2007-06-13T07:00:30.000Z"
updated: "2007-06-13T08:14:36.000Z"
replies: 1
views: 290
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/databaser-sql/161448-får-två-tillbaka-men-vill-ha-en"
---

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

## #1 — Fredde Mannen, 2007-06-13T07:00Z

```
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..

Permalänk: https://www.webforum.nu/p/161448

## #2 — Fredde Mannen, 2007-06-13T08:14Z

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

Permalänk: https://www.webforum.nu/p/1989872

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/161448-får-två-tillbaka-men-vill-ha-en
