Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mortency
Starting Member
4 Posts |
Posted - 2004-10-04 : 09:56:52
|
| I have problems with a query that i cant se to solve. Maybe someone here can help a norwegian guy????The database have about 10000 posts with cars.i have made this query:SELECT DISTINCT BID,Regnr,ANavn,GjelderFra,SiOppForsikringFra,Aktiv,CASE WHEN SiOppForsikringFra is null THEN DateDiff(d, GjelderFra, getdate()) ELSE DateDiff(d, GjelderFra, SioppForsikringFra) END AS AntDaysFROM marsh_HKL.tblbiler, (biler is cars)marsh_HKL.tblAvdeling (avdeling is department)WHERE FKBRAvdeling=AIDORDER BY ANAVN ASCThe data isRegnr RegNo of the carANavn is department nameGjelderFra is the date the car will be active fromSiOppForsikringFra is the date the cars shouldent be active more fromAktiv is if the car is active YES/NOYou may see the tabels her --> http://www.cyren.no/diagram3.gifI want the output like this:Department(ANavn)JANUAR 2003VF54423(RegNr) 21.01.2003(GjelderFra) 23.01.2003(SiOppforsikringFra) 3(AntDays)VF54422(RegNr) 21.01.2003(GjelderFra) 23.03.2003(SiOppforsikringFra) 9(AntDays) SUM JANUAR 12 days 2 carsFEBRUARY 2003VF54422(RegNr) 21.01.2003(GjelderFra) 23.03.2003(SiOppforsikringFra) 30(AntDays) SUM JANUAR 30 days 1 carMARS 2003VF54422(RegNr) 21.01.2003(GjelderFra) 23.03.2003(SiOppforsikringFra) 23(AntDays) SUM JANUAR 23 days 1 carIs this possible? Or must the query be somthing like this? But together?SELECTMonth(gjelderFra) as Month1,Year(gjelderFra) as Year1,Count(Regnr) as totalFROM marsh_HKL.tblbiler (biler is cars)WHERE FKBRAvdeling=184Group by Month(GjelderFra), Year(gjelderFra)SELECTMonth(SioppForsikringFra) as Month1,Year(SioppForsikringFra) as year1,Count(Regnr) as totalFROM marsh_HKL.tblbiler (biler is cars)WHERE FKBRAvdeling=184Group by Month(SioppForsikringFra), Year(SioppForsikringFra)//Newbie Morten |
|
|
tuenty
Constraint Violating Yak Guru
278 Posts |
Posted - 2004-10-04 : 11:57:00
|
It would be easier to help if you write table structure as create statements and add some data with insert. (use the button with the '#' sign to insert code and keep its format)Check if these selects helps you (I did not test them)--DetailSelect ANavn, datepart(month, isnull(SiOppForsikringFra,getDate())) as detMonth, Regnr, GjelderFra, SiOppforsikringFra CASE WHEN SiOppForsikringFra is null THEN DateDiff(d, GjelderFra, getdate()) ELSE DateDiff(d, GjelderFra, SioppForsikringFra) END AS AntDaysFROM marsh_HKL.tblbiler, marsh_HKL.tblAvdeling WHERE FKBRAvdeling=AIDORDER BY SiOppForsikringFra, ANAVN ASC--SummarySelect datepart(month, isnull(SiOppForsikringFra,getDate())) as sumMonth, AIDsum, (CASE WHEN SiOppForsikringFra is null THEN DateDiff(d, GjelderFra, getdate()) ELSE DateDiff(d, GjelderFra, SioppForsikringFra) END) AS SumAntDaysFROM marsh_HKL.tblbiler, marsh_HKL.tblAvdeling WHERE FKBRAvdeling=AIDGroup BY datepart(month, isnull(SiOppForsikringFra,getDate())),AID *.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*A candle loses nothing by lighting another candle |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2004-10-04 : 11:59:57
|
| 1st Change CASE WHEN SiOppForsikringFra is null THEN DateDiff(d, GjelderFra, getdate()) ELSE DateDiff(d, GjelderFra, SioppForsikringFra) END AS AntDaysTo AntDays = CASE WHEN SiOppForsikringFra is null THEN DateDiff(d, GjelderFra, getdate()) ELSE DateDiff(d, GjelderFra, SioppForsikringFra) END Second I would handle the report gouping on the front end.JimUsers <> Logic |
 |
|
|
tuenty
Constraint Violating Yak Guru
278 Posts |
Posted - 2004-10-08 : 10:17:34
|
| [code]Select ANavn, datepart(month, isnull(SiOppForsikringFra,getDate())) as detMonth, Regnr, GjelderFra, SiOppforsikringFra CASE WHEN SiOppForsikringFra is null THEN DateDiff(d, GjelderFra, getdate()) ELSE DateDiff(d, GjelderFra, SioppForsikringFra) END AS AntDaysFROM marsh_HKL.tblbiler inner join marsh_HKL.tblAvdeling on marsh_HKL.tblbiler.xyz = marsh_HKL.tblAvdeling.xyz --LINK THE TWO TABLESINNER JOIN (Select datepart(month, isnull(SiOppForsikringFra,getDate())) as sumMonth, AIDsum, (CASE WHEN SiOppForsikringFra is null THEN DateDiff(d, GjelderFra, getdate()) ELSE DateDiff(d, GjelderFra, SioppForsikringFra) END) AS SumAntDays FROM marsh_HKL.tblbiler, marsh_HKL.tblAvdeling Group BY datepart(month, isnull(SiOppForsikringFra,getDate())),AID) summary ON marsh_HKL.tblbiler.xyz = SUMMARY.xyz --LINK SUMMARY WITH THE OTHER TABLESORDER BY SiOppForsikringFra, ANAVN ASC[/code]*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*A candle loses nothing by lighting another candle |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-10-08 : 10:22:42
|
quote: Originally posted by JimL 1st Change CASE WHEN SiOppForsikringFra is null THEN DateDiff(d, GjelderFra, getdate()) ELSE DateDiff(d, GjelderFra, SioppForsikringFra) END AS AntDaysTo AntDays = CASE WHEN SiOppForsikringFra is null THEN DateDiff(d, GjelderFra, getdate()) ELSE DateDiff(d, GjelderFra, SioppForsikringFra) END Second I would handle the report gouping on the front end.JimUsers <> Logic
?? Why do that, Jim ? Both are legal and are equivalent.- Jeff |
 |
|
|
|
|
|
|
|