Just nu i M3-nätverket
Jump to content

Flera selectsatser på rad?????


Ansan

Recommended Posts

Vilse i sql-djungeln försöker jag att söka ut antal förekomster under olika år, det är ju inget problem så länge jag gör en select i taget, men det måste ju gå att göra detta med select i selecten om någon förstår vad jag menar????

 

Ex.

SELECT COUNT(*) as '-95'

FROM MOVIE

WHERE [YEAR]<1995

 

SELECT COUNT(*) as '95-97'

FROM MOVIE

WHERE [YEAR]BETWEEN 1995 AND 1997;

 

SELECT COUNT(*) as '98-00'

FROM MOVIE

WHERE [YEAR]BETWEEN 1998 AND 2000;

 

Ovanstående skulle jag vilja att det presenteras ungefär så här:

 

-95 95-97 98-00

3 7 14

 

Hoppas någon kan hjälpa mig med detta.

 

Link to comment
Share on other sites

SELECT '-95' AS YEAR, COUNT(*) AS ANTAL
FROM MOVIE
WHERE [YEAR]<1995
UNION
SELECT  '95-97'  AS YEAR, COUNT(*) AS ANTAL
FROM MOVIE
WHERE [YEAR]BETWEEN 1995 AND 1997
UNION
SELECT  '98-00'  YEAR, COUNT(*) AS ANTAL
FROM MOVIE
WHERE [YEAR]BETWEEN 1998 AND 2000;

 

Ger dig

 

YEAR ANTAL

-95 3

95-97 7

98-00 14

 

Link to comment
Share on other sites

Lösningen blev så här till slut, om det kan vara något som kan hjälpa andra.

 

SELECT COUNT(*) AS TotaltAntalFilmer,

(SELECT COUNT(*) FROM MOVIE

WHERE [YEAR]<1995) AS '-95',

(SELECT COUNT(*) FROM MOVIE

WHERE [YEAR]BETWEEN 1995 AND 1997) AS '95-97',

(SELECT COUNT(*) FROM MOVIE

WHERE [YEAR]BETWEEN 1998 AND 2000) AS '98-00',

(SELECT COUNT(*) FROM MOVIE

WHERE [YEAR]BETWEEN 2001 AND 2003) AS '01-03',

(SELECT COUNT(*) FROM MOVIE

WHERE [YEAR]>=2004) AS '2004-'

FROM MOVIE;

 

Resultatet blev då

 

TotaltAntalFilmer -95 95-97 98-00 01-03 2004-

24 3 7 14 0 0

 

Tack för tipset; )

 

[inlägget ändrat 2009-02-13 20:29:24 av Ansan]

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...