sql server - SQL Where statement to check against other records in same month -


i'm attempting pull records database having problems constructing clause of query.

i have database full of accounts tied service location. on time, accounts change service location never does. want accomplish query returns records service location had 1 account attached it.

here sample of code, isn't close need.

select c.account_num, month(fb.[end]) 'month',  year(fb.[end]) 'year',  avg(fb.edays) 'billing days'   dimservicelocation sl inner join factbill fb on fb.servicelocationkey = sl.servicelocationkey inner join dimcustomer c on c.customerkey = fb.customerkey   , fb.edays in (28, 29, 30, 31, 32, 33, 34, 35) , c.class_name = 'customer'  , (fb.[end] > dateadd(m, - 24, getdate())) 

the clause defines number of "edays" (billing days) , class_name (customer). there issues doing way (ex: if month has 33 billing days, return customers attached location 29 days, not have been account holders entire month).

i have no idea how accomplish goal. feel though solution may checking service locations specific bill periods (defined field called period_num) , querying service locations had 1 account_num during period_num.

any appreciated.

---------- updated ----------

i mistakenly left out significant portion of original question. apologies helped. second part of problem follows:

in original select statement, need return average usage billed during individual period across similar service locations. these lines of code added:

select c.account_num,  sl.[floorplan name], convert(decimal(5,0), avg(eusage)) 'average electric usage', count(distinct sl.location_id) 'service locations',  month(fb.[end]) 'month',  year(fb.[end]) 'year',  avg(fb.edays) 'billing days' 

the final result ideally return information:

  • the floorplan name
  • the average usage during period
  • the number of service locations used determine average
  • the month & year period ended.
  • the average number of billing days month (essentially, how many bill days in month because accounts used should have same number of bill days).

again, ultimate goal consider service locations single account attached during individual billing period.

thanks again!

---------- sample results ----------

sample results

i think query need, although sample data , expected output make things more certain:

select     c.account_num     , month(fb.[end]) 'month',      , year(fb.[end]) 'year'     , avg(fb.edays) 'billing days' dimservicelocation sl     inner join factbill fb on fb.servicelocationkey = sl.servicelocationkey     inner join dimcustomer c on c.customerkey = fb.customerkey      , fb.edays in (28, 29, 30, 31, 32, 33, 34, 35)     , c.class_name = 'customer'      , (fb.[end] > dateadd(m, - 24, getdate())) group c.account_num     , month(fb.[end]     , year(fb.[end]) having count(distinct c.account_num) = 1 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -