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 |
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-08 : 12:02:59
|
| I have a rather complex query that I am going to try to simplify for just what I am having issues with. I hope that I dont make it more confusing, but if so, please let me know.The query is used to get meter reading data for a yacht slip in order to process the next months meter readings.TablesLocation lLocationMeterHistory lmhSelect l.*,lmh.LastReading,lmh.LastReadingDateFrom Location lLEFT JOIN LocationMeterHistory lmhON l.ID = lmn.LocationIDThis all works and returns the records that I want, except if there is no record in LocationMeterHistory, I want the LastReading to return 0 and I cannot figure out how to make that happen. I can deal with the date value being null. But when I try to calculate the next reading, it is bombing because I have no value if it was not read before, therefore I need a 0. Make any sense at all?I tried to addcase mlmh.ReadValue When 0 then 0 Else mlmh.ReadValueend as LastReadValuewhich would work if there were a record in mlmh that had a 0, but there is no record in the history table at all yet. Thanks in advance,JAdauto |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-08 : 12:13:58
|
| Select l.*,isnull(lmh.LastReading,0),isnull(lmh.LastReadingDate,0)From Location lLEFT JOIN LocationMeterHistory lmhON l.ID = lmn.LocationID |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-08 : 14:53:18
|
| I totally hate when the answer was that easy and I was making it so difficult. "Sometimes it is hard to see the forest through all the trees" Thank you so much! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-09 : 03:51:07
|
| Note that isnull(lmh.LastReadingDate,0) would result in 1900-01-01 if the column is nullMadhivananFailing to plan is Planning to fail |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-12 : 16:54:17
|
| This query continues to get more complicated. The LocationMeterHistory table will hold all of the meter readings for (ever and ever I guess) for a meter. So, each month when they calculate the readings, I only want to return the last 1 to see what the last reading was. So my query looks something like:Select Top 1(isnull(lmh.LastReadingDate,0)),isnull(lmh.LastReading,0),l.*From Location lLEFT JOIN LocationMeterHistory lmhON l.ID = lmn.LocationIDORDER BY lmh.LastReadingDate DESCAnd this fixes my problem of only wanting the most recent item, but now it is ignoring the Location records that dont yet have a locationMeterHistory record. Make sense? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-13 : 00:01:01
|
Try like belowSelect *FROM(Select isnull(lmh.LastReadingDate,0),isnull(lmh.LastReading,0),l.*,ROW_NUMBER() OVER(PARTITION BY l.ID ORDER BY lmh.LastReadingDate DESC) AS SeqFrom Location lLEFT JOIN LocationMeterHistory lmhON l.ID = lmn.LocationID)WHERE Seq=1 |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-24 : 10:34:27
|
| Now my problem seems to be that I am only returning the top 1 record for everything if there are no LastReading Dates. Jeez.. this query is gonna send me to an early grave. I guess I thought Top 1 would return only my top date for that location (so I would get a list of all slips with only the most recent read date for each one), not the top 1 for ALL locations. I tried to do a groupBy and that didnt work. Any other suggestions?So my query looks something like:Select Top 1(isnull(lmh.LastReadingDate,0)),isnull(lmh.LastReading,0),l.*From Location lLEFT JOIN LocationMeterHistory lmhON l.ID = lmn.LocationIDORDER BY lmh.LastReadingDate DESC |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-24 : 10:47:25
|
| Did you try solution i posted? |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-24 : 10:50:29
|
quote: Originally posted by visakh16 Did you try solution i posted?
Yes, I get invalid column name 'Seq' |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-24 : 10:59:27
|
| Oh wait..I just looked again and I had been putting the RowNumber() in my select list.. not my From... let me see if i can figure out how to get all of that in my query. This is what is really looks like (I was just trying to trim it down to get only the parts I needed help with). I am getting incorrect syntax near INNER. SELECT isNull(mlmh.ReadDate,0) as LastReadDate, ce.Description as SlipStatus, mlevd.AssignedDate, mlevd.ReleaseDate , e.ID as EntityID, (IsNull(E.LastName,'') + IsNull(', ' + E.FirstName,'')) as LastNameFirstName, (e.Number) + '-' + (e.NumberSuffix) as NumberSuffix, (e.Number) as Number, ml.MeterNumber, Isnull(ml.MeterRate,0) as MeterRate, ml.Description as SlipNumber, Isnull(mlmh.ReadValue,0) as LastReadValue, '0' as ChargeAmount, ml.ID as MarinaLocationIDFROM (Select isnull(submlmh.ReadDate,0),isnull(submlmh.LastReading,0),subml.*,ROW_NUMBER() OVER(PARTITION BY subml.ID ORDER BY submlmh.ReadDate DESC) AS SeqFrom MarinaLocation submlLEFT JOIN MarinaLocationMeterHistory submlmhON subml.ID = submlmh.LocationID)INNER JOIN MarinaLocEntityVehicleDetail mlevd ON mlevd.MarinaLocationID = sbml.IDINNER JOIN MarinaLocation ml ON ml.ID = mlevd.MarinaLocationID INNER JOIN EntityVehicle ev ON ev.ID = mlevd.EntityVehicleID INNER JOIN Entity e ON ev.EntityID = e.ID LEFT JOIN MarinaLocationMeterHistory mlmh ON mlmh.MarinaLocationID = ml.ID LEFT JOIN CategoryElement as ce ON mlevd.MarinaStatusCID = ce.IDWHERE (mlevd.AssignedDate <= 'October 15 2008 12:00:00 AM' or mlevd.AssignedDate is Null)AND (mlevd.ReleaseDate >= 'October 15 2008 12:00:00 AM' OR mlevd.ReleaseDate Is Null) AND Seq = 1ORDER BY mlmh.ReadDate DESC, ml.MeterNumber |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-24 : 11:06:17
|
quote: Originally posted by JAdauto Oh wait..I just looked again and I had been putting the RowNumber() in my select list.. not my From... let me see if i can figure out how to get all of that in my query. This is what is really looks like (I was just trying to trim it down to get only the parts I needed help with). I am getting incorrect syntax near INNER. SELECT isNull(mlmh.ReadDate,0) as LastReadDate, ce.Description as SlipStatus, mlevd.AssignedDate, mlevd.ReleaseDate , e.ID as EntityID, (IsNull(E.LastName,'') + IsNull(', ' + E.FirstName,'')) as LastNameFirstName, (e.Number) + '-' + (e.NumberSuffix) as NumberSuffix, (e.Number) as Number, ml.MeterNumber, Isnull(ml.MeterRate,0) as MeterRate, ml.Description as SlipNumber, Isnull(mlmh.ReadValue,0) as LastReadValue, '0' as ChargeAmount, ml.ID as MarinaLocationIDFROM (Select isnull(submlmh.ReadDate,0),isnull(submlmh.LastReading,0),subml.*,ROW_NUMBER() OVER(PARTITION BY subml.ID ORDER BY submlmh.ReadDate DESC) AS SeqFrom MarinaLocation submlLEFT JOIN MarinaLocationMeterHistory submlmhON subml.ID = submlmh.LocationID)tblINNER JOIN MarinaLocEntityVehicleDetail mlevd ON mlevd.MarinaLocationID = tbl.IDINNER JOIN MarinaLocation ml ON ml.ID = mlevd.MarinaLocationID INNER JOIN EntityVehicle ev ON ev.ID = mlevd.EntityVehicleID INNER JOIN Entity e ON ev.EntityID = e.ID LEFT JOIN MarinaLocationMeterHistory mlmh ON mlmh.MarinaLocationID = ml.ID LEFT JOIN CategoryElement as ce ON mlevd.MarinaStatusCID = ce.IDWHERE (mlevd.AssignedDate <= 'October 15 2008 12:00:00 AM' or mlevd.AssignedDate is Null)AND (mlevd.ReleaseDate >= 'October 15 2008 12:00:00 AM' OR mlevd.ReleaseDate Is Null) AND Seq = 1ORDER BY mlmh.ReadDate DESC, ml.MeterNumber
Modify like above |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-24 : 11:57:01
|
| so close.. I got it to work, but I am still getting the records that have already been billed instead of only just the most recent. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-24 : 12:43:43
|
quote: Originally posted by JAdauto so close.. I got it to work, but I am still getting the records that have already been billed instead of only just the most recent.
what are the other joins for? |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-24 : 12:59:37
|
| I need those to retrieve other data that I need in the results. They dont really have any bearing the wear criterea. This is why I was trying to simplify it originally so that you wouldn't have to deal with all those other joins, but when I got the syntax error, I didn't know where to find it.I tried taking out the stuff with the Row number and adding this line to the WhereAND mlmh.ReadDate = (Select Max(mlmh.ReadDate) from MarinaLocationMeterHistory mlmh)but it only returned me the results for the one guy that I had posted 2 reading for (both of them instead of the greatest one) and not the other 5 that dont have readings yet.I really want to be able to post you some sample data and build some structures for you, but this is such a mess. I am afraid it would be too much. Would that help or hurt the process? |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-24 : 13:02:27
|
| Well I can see why that WHERE statement would not work. Those two readings that it returns have the same date. So it equals it... I realized it as soon as I clicked Post.Sorry, ignore all of that... just grasping. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-24 : 13:13:51
|
can you try including other tables to inside query?something like below:-SELECT *FROM(Select isnull(submlmh.ReadDate,0) AS LastReadDate,isnull(submlmh.ReadValue,0) AS LastReadValue,ce.Description as SlipStatus, mlevd.AssignedDate, mlevd.ReleaseDate , e.ID as EntityID, (IsNull(E.LastName,'') + IsNull(', ' + E.FirstName,'')) as LastNameFirstName, (e.Number) + '-' + (e.NumberSuffix) as NumberSuffix,(e.Number) as Number, subml.MeterNumber, Isnull(subml.MeterRate,0) as MeterRate, subml.Description as SlipNumber, '0' as ChargeAmount, subml.ID as MarinaLocationID ,ROW_NUMBER() OVER(PARTITION BY subml.ID ORDER BY submlmh.ReadDate DESC) AS SeqFrom MarinaLocation submlINNER JOIN MarinaLocEntityVehicleDetail mlevd ON mlevd.MarinaLocationID = subml.IDINNER JOIN EntityVehicle ev ON ev.ID = mlevd.EntityVehicleID INNER JOIN Entity e ON ev.EntityID = e.ID LEFT JOIN MarinaLocationMeterHistory submlmhON subml.ID = submlmh.LocationIDLEFT JOIN CategoryElement as ce ON mlevd.MarinaStatusCID = ce.IDWHERE (mlevd.AssignedDate <= 'October 15 2008 12:00:00 AM' or mlevd.AssignedDate is Null)AND (mlevd.ReleaseDate >= 'October 15 2008 12:00:00 AM' OR mlevd.ReleaseDate Is Null) )tWHERE Seq = 1ORDER BY LastReadDate DESC, MeterNumber |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-24 : 13:16:05
|
| Question... is RowNumber only a SQL20005 function? While i use SQL 2005, some of our clients still only have 2000. |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-24 : 13:17:59
|
| omg! That worked! Please tell me this function is available to 2000? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-24 : 13:22:20
|
quote: Originally posted by JAdauto Question... is RowNumber only a SQL20005 function? While i use SQL 2005, some of our clients still only have 2000.
Yup it is. If its sql 2000 you cant use row_number(). I gave sql 2005 specific soln as you had posted this in sql 2005 forum. |
 |
|
|
JAdauto
Posting Yak Master
160 Posts |
Posted - 2008-10-24 : 13:28:01
|
| oh man! I want to cry! Yes I know. I forget a lot of times that some of our clients are still on 2000. I usually dont have an issue as most things work in both. I am sorry for this. URGH! It works so beautiful! I am just sick |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-24 : 13:37:31
|
try this in 2000 too and hope it worksSelect isnull(tmp.ReadDate,0) AS LastReadDate,isnull(tmp.ReadValue,0) AS LastReadValue,ce.Description as SlipStatus, mlevd.AssignedDate, mlevd.ReleaseDate , e.ID as EntityID, (IsNull(E.LastName,'') + IsNull(', ' + E.FirstName,'')) as LastNameFirstName, (e.Number) + '-' + (e.NumberSuffix) as NumberSuffix,(e.Number) as Number, subml.MeterNumber, Isnull(subml.MeterRate,0) as MeterRate, subml.Description as SlipNumber, '0' as ChargeAmount, subml.ID as MarinaLocationIDFrom MarinaLocation submlINNER JOIN MarinaLocEntityVehicleDetail mlevd ON mlevd.MarinaLocationID = subml.IDINNER JOIN EntityVehicle ev ON ev.ID = mlevd.EntityVehicleID INNER JOIN Entity e ON ev.EntityID = e.ID LEFT JOIN (SELECT submlmh.* FROM MarinaLocationMeterHistory submlmhINNER JOIN (SELECT LocationID,MAX(ReadDate) AS latest FROM MarinaLocationMeterHistory GROUP BY LocationID) submlmh1ON submlmh1.LocationID=submlmh.LocationIDAND submlmh1.latest=submlmh.ReadDate)tmpON subml.ID = tmp.LocationIDLEFT JOIN CategoryElement as ce ON mlevd.MarinaStatusCID = ce.IDWHERE (mlevd.AssignedDate <= 'October 15 2008 12:00:00 AM' or mlevd.AssignedDate is Null)AND (mlevd.ReleaseDate >= 'October 15 2008 12:00:00 AM' OR mlevd.ReleaseDate Is Null) ORDER BY tmp.ReadDate DESC, subml.MeterNumber |
 |
|
|
Next Page
|
|
|
|
|