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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Total Missing in Part of Table

Author  Topic 

hasanqz
Starting Member

5 Posts

Posted - 2015-04-30 : 14:46:15
I want to add a new column in a table. In this column I want to include a Total Value. This Total Value exists already for dates after Sept 23, 2013. For dates before Sept 23, 2013 the total can be calculated using the following math:

Total = Total(t-1) - (Resource_Name1-Resource_Name1).

The Resource_Name column contains the Total field (after Sept 23) as well as the Resource_Name1 field. There is a third column called Direction which contains the values injection or withdrawal. The Resource_Name1 differes in value depending on whether it is injection or withdrawal. How do I create a new column with Totals for the full data set?

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-04-30 : 14:54:11
Adding a column to a table:

alter table mytable
add Total as [datatype]|(expression)

However it's not clear to me what you really need.

Please post the current table definition (as a CREATE TABLE statement)

Also, in your epression


Total = Total(t-1) - (Resource_Name1-Resource_Name1).


What is "t" and what do you mean by Total(t-1)

Also I don't know what this means: "third column called Direction which contains the values injection or withdrawal"

Perhaps some examples would help.






However
Go to Top of Page

hasanqz
Starting Member

5 Posts

Posted - 2015-04-30 : 15:10:17
Assume that Total is a field stored in the RESOURCE_NAME column. Assume that the RESOURCE_NAME column also has a field called ALPHA. Since the Total field does not exist prior to Sept 23, 2013. I need to calculate it using the ALPHA field and the Total from the prior day (Which I defined as t-1). The DIRECTION column distinguishes the values for the ALPHA field (as injection or withdrawal). I have the basic SQL structure below but I need to alter the second select statement to include the math mentioned above.


SELECT [OPR_DATE]
,[RESOURCE_NAME]
,[VALUE]
,[DIRECTION]
FROM TABLE_NAME
where OPR_DATE >= '2013-09-23'

UNION

SELECT [OPR_DATE]
, [RESOURCE_NAME]
, [VALUE]
, [DIRECTION]
FROM TABLE_NAME
where OPR_DATE <'2013-09-23'


order by OPR_DATE
Go to Top of Page

hasanqz
Starting Member

5 Posts

Posted - 2015-04-30 : 15:19:42
here is my problem For reference:

http://www.pge.com/pipeline/operations/cgt_storage_search.page

If you go to the following link and search for Sept 23, 2013 and surrounding dates, you will see that the bottom row (PG&E Total Gas in Storage) does not have values starting Sept 22. But the values can be calculated using the Sept 23 PG&E Total Gas in Storage and subtracting (PG&E Core and Market Center (Injection) - PG&E Core and Market Center (Withdrawal) for Sept 22.
Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2015-04-30 : 16:07:39
quote:
Originally posted by hasanqz

here is my problem For reference:

http://www.pge.com/pipeline/operations/cgt_storage_search.page

If you go to the following link and search for Sept 23, 2013 and surrounding dates, you will see that the bottom row (PG&E Total Gas in Storage) does not have values starting Sept 22. But the values can be calculated using the Sept 23 PG&E Total Gas in Storage and subtracting (PG&E Core and Market Center (Injection) - PG&E Core and Market Center (Withdrawal) for Sept 22.


Tried to lookup Sept 24. to see if I could calc the total of Sept 23, but I failed to get the right result. 100079-(371-159)=99867 (99787 on the web).
Then I tried Sept 25. but my calculator just will not get the same as on you web. 100366-(397-166)=100135 (100079 on the web).
Am I failing to punch in the right numbers, or is something wrong on your web?
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-04-30 : 16:19:01
Could you please post your CREATE TABLE statement as requested before?
Go to Top of Page

hasanqz
Starting Member

5 Posts

Posted - 2015-04-30 : 16:57:21
@bitsmed,
you are doing it correctly. This calculated method isn't the most accurate way to come up with the Total but its the best estimate possible given availability of the data

@gbritton
I did not create the table so I'm not sure where to get this statement from. I am simply writing a query to generate the missing data based on existing value.

Here is the table structure for just Sept 22 and Sept 23...As you can see Sept 22 does not have PG&E Total Gas in Storage but I want to calculate it by taking Sept 23's PG&E Total Gas in Storage MINUS [Sept 22's PG&E Core and Market Center (where direction = Injection) MINUS Sept 22's PG&E Core and Market Center (where direction = Withdrawal)]. I want to do this for all remaining dates prior to Sept 23.

OPR_DATE RESOURCE_NAME VALUE DIRECTION
9/22/2013 PG&E Core and Market Center 409 Injection
9/22/2013 PG&E Core and Market Center 159 Withdrawal
9/23/2013 PG&E Core and Market Center 381 Injection
9/23/2013 PG&E Core and Market Center 159 Withdrawal
9/23/2013 PG&E Total Gas in Storage 99787 BalancingGas
Go to Top of Page

hasanqz
Starting Member

5 Posts

Posted - 2015-04-30 : 17:02:10
OPR_DATE----RESOURCE_NAME-----------------VALUE----DIRECTION
9/22/2013---PG&E Core and Market Center---409------Injection
9/22/2013---PG&E Core and Market Center---159------Withdrawal
9/23/2013---PG&E Core and Market Center---381------Injection
9/23/2013---PG&E Core and Market Center---159------Withdrawal
9/23/2013---PG&E Total Gas in Storage----99787-----Balancing Gas


Thought this would be an easy way to distinguish the columns..
Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2015-04-30 : 17:46:52
This might work for you:
select a.opr_date
,a.resource_name
,sum(b.value*case
when b.resource_name='PG&E Core and Market Center'
and b.direction='Injection'
then -1
else 1
end
) as value
,'BalancingGas' as direction
from (select a.opr_date
,min(b.opr_date) as total_opr_date
from table_name as a
inner join table_name as b
on b.opr_date<a.oprdate
and b.resource_name=a.resource_name
and b.direction='BalancingGas'
where a.resource_name='PG&E Core and Market Center'
and a.direction='Injection'
and not exists (select 1
from table_name as t
where t.opr_date=a.opr_date
and t.resource_name='PG&E Total Gas in Storage'
and t.direction='BalancingGas'
)
group by a.opr_date
) as a
inner join table_name as b
on b.opr_date>a.opr_date
and b.opr_date<=a.total_opr_date
and ((b.resource_name='PG&E Total Gas in Storage'
and b.direction='BalancingGas'
)
or (b.resource_name='PG&E Core and Market Center'
and b.direction in ('Injection','Withdrawel')
))
group by a.opr_date


Edit: Fixed bracket mixup
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-04-30 : 19:26:43
quote:
Originally posted by hasanqz

@bitsmed,
you are doing it correctly. This calculated method isn't the most accurate way to come up with the Total but its the best estimate possible given availability of the data

@gbritton
I did not create the table so I'm not sure where to get this statement from. I am simply writing a query to generate the missing data based on existing value.

Here is the table structure for just Sept 22 and Sept 23...As you can see Sept 22 does not have PG&E Total Gas in Storage but I want to calculate it by taking Sept 23's PG&E Total Gas in Storage MINUS [Sept 22's PG&E Core and Market Center (where direction = Injection) MINUS Sept 22's PG&E Core and Market Center (where direction = Withdrawal)]. I want to do this for all remaining dates prior to Sept 23.

OPR_DATE RESOURCE_NAME VALUE DIRECTION
9/22/2013 PG&E Core and Market Center 409 Injection
9/22/2013 PG&E Core and Market Center 159 Withdrawal
9/23/2013 PG&E Core and Market Center 381 Injection
9/23/2013 PG&E Core and Market Center 159 Withdrawal
9/23/2013 PG&E Total Gas in Storage 99787 BalancingGas




IN SSMS, rightclick on the table in the object explorer, then choose Script table as Create to Clipboard and paste the results here.
Go to Top of Page
   

- Advertisement -