SQL get the next rows value into current row - sql

I am trying to get the next row's 'alignment' value and store it into the current row in a field called nextAlign. I have tried using grouping and also the unique identifier for this table is not consistent and doesn't always have an increment of 1. Here is some data:
type field starttop startleft length decimals alignment
-------------------------------------------------------------------------------------------
Text CUSTOMER DETAILS CONFIRMATION 13.00 38.00 30.00 0.00 Centre
Text Please spare a few minutes to 15.00 2.00 30.00 0.00 Left
Text confirm your details as held 15.00 2.00 30.00 0.00 Wrap
Text on our system. You may fax the 15.00 2.00 30.00 0.00 Wrap
Text form to the number above. 15.00 2.00 30.00 0.00 Wrap
Text Any information you may supply 17.00 2.00 30.00 0.00 Left
Text will not be made available to 17.00 2.00 30.00 0.00 Wrap
Text third parties according to the 17.00 2.00 30.00 0.00 Wrap
Text Privacy Act. 17.00 2.00 30.00 0.00 Wrap
Text Legal name: 20.50 2.00 30.00 0.00 Left
All I want is a column called 'nextAlign' that has the following data:
nextAlign
-Left
-Wrap
-Wrap
-Wrap
-Left
-Wrap

You didn't specify your DBMS, so this is ANSI SQL:
select type,
field,
align,
lead(align) over (order by starttop) as next_align,
starttop,
startleft
from the_table

use ROW_NUMBER() OVER(ORDER BY )
and outer join it to the same select this way: select_1_rownumber = select_2_rownumber+1

with temptable as
( select rownum
,type
,field
,starttop
,startleft
,length
,decimals
,alignment
from YOURTABLE )
select t.type
,t.field
,t.starttop
,t.startleft
,t.length
,t.decimals
,t.alignment
( select tt.alignment from temptable tt where tt.rownum = t.rownum+1 ) nextalign
from temptable t
Might work for you.

Related

Is there a way to calculate a dynamic column in SQL based on multiple other columns

I am trying to determine if something is possible to do in SQL where I am creating a view for users and I need to create a column that would state whether or not a line number is "Open" or "Closed". The trouble is that the process to determine that value is based on multiple factors/values of other columns from the source table. Take a look at some data that the view currently generates.
Line # Req_Qty Rej_Qty Adj_Qty Alt_Qty Shipped Cxl Rec Conf
Line 1 71.00 0.00 100.00 0.00 100.00 0.00 100.00 100.00
Line 2 23.00 0.00 0.00 0.00 23.00 0.00 0.00 0.00
Line 3 11.00 0.00 10.00 0.00 10.00 0.00 0.00 0.00
Line 4 12.00 12.00 0.00 0.00 0.00 0.00 0.00 0.00
Line 5 0.00 0.00 0.00 20.00 20.00 0.00 0.00 0.00
In the table above the way the determination is made is as follows:
If there is any value > 0 in Adj_Qty, then the Adj_Qty(Adjusted Qty) effectively becomes the Req_Qty(Requested Qty). If both the Req_Qty and the Adj_Qty are 0 and the Alt_Qty >0, then that now becomes the real Req_ Quantity. So basically, my first comparison is Adj->Req->Alt. Whatever that quantity is would then be reduced by the Rej_Qty(Rejected) or the Cxl_Qty(Cancelled) which results in a balance.
Finally, if the quantity in either the Rec or Conf columns matches that balance, then the line # is closed. So, for the table, Lines 1 and 4 would be closed, 2, 3, and 5 would be open. Is there any way to create a field/column in SQL that would be able to assign "Open" or "Closed" based on that type of logic?
You can use the CASE clause to compute the balance and the status in the view. For example:
create view v as
select *,
case when (
case when adj_qty > 0 then adj_qty
when req_qty > 0 then req_qty
when alt_qty > 0 then alt_qty
else 0.0
end
- rej_qty
- cxl_qty
) in (rec, conf) then 'closed'
else 'open' end as status
from t
I probably didn't understand all the details of your specific logic but this query should be pretty close to what you need. Tweak as needed.

SQL Server : grouping 2 columns in same row

I have got this query
select
Id,
case when isThirdParty = 0 then sum(total) end as FirstPartyFees,
case when isThirdParty = 1 then sum(total) end as ThirdPartyFees
from
MyTable
group by
id, isThirdParty
And I got this result
Id FirstPartyFees ThirdPartyFees
------------------------------------ --------------------------------------- ---------------------------------------
DA29BDC0-BE3F-4193-BFDC-493B354CE368 15.00 0.00
2EF0B590-FE4F-42E8-8426-5864A739C16B 5.00 0.00
246DC3D8-732F-4AE3-99F3-BDEBF98F7719 15.00 0.00
FC81F220-ED54-48FE-AE1B-C394492E82A4 5.00 0.00
336D9CF1-6970-48BA-90E5-C7889914DDCB 114.00 0.00
6F2EEF6F-5FA1-42E5-A988-DB88037DAB92 5.00 0.00
80763B37-68E1-4716-B32A-FE82C1700B52 15.00 0.00
DA29BDC0-BE3F-4193-BFDC-493B354CE368 0.00 1.00
2EF0B590-FE4F-42E8-8426-5864A739C16B 0.00 1.00
246DC3D8-732F-4AE3-99F3-BDEBF98F7719 0.00 1.00
FC81F220-ED54-48FE-AE1B-C394492E82A4 0.00 1.00
336D9CF1-6970-48BA-90E5-C7889914DDCB 0.00 0.00
6F2EEF6F-5FA1-42E5-A988-DB88037DAB92 0.00 1.00
80763B37-68E1-4716-B32A-FE82C1700B52 0.00 1.00
As you see, I get duplicates for the first party and the third party. How can I group first party and third party total for the same Id in 1 row?
You are looking for conditional aggregation.
You should move the case expression inside the sum() and remove isThirdParty from the group by clause.
SELECT Id,
SUM(CASE WHEN isThirdParty = 0 THEN total END) AS FirstPartyFees,
SUM(CASE WHEN isThirdParty = 1 THEN total END) AS ThirdPartyFees
FROm MyTable
GROUP BY id
Because isThirdParty is an indicator, you can also just use math:
select Id, SUM(total * (1 - isThirdParty)) as FirstPartyFees,
SUM(total * isThirdParty) as ThirdPartyFees
frOm MyTable
group by id;

how to calculate balances in an accounting software using postgres window function

I'ved got a problem same as this but I am using Postgres.
Calculate balance with mysql
have a table which contains the following data:
ID In Out
1 100.00 0.00
2 10.00 0.00
3 0.00 70.00
4 5.00 0.00
5 0.00 60.00
6 20.00 0.00
Now I need a query which gives me the following result:
ID In Out Balance
1 100.00 0.00 100.00
2 10.00 0.00 110.00
3 0.00 70.00 40.00
4 5.00 0.00 45.00
5 0.00 60.00 -15.00
6 20.00 0.00 5.00
How best to handle "balance" calculation. I was told there is window function in postgres, how would this be done using postgres window functions ?
Thanks.
select t.*, sum("In"-"Out") over(order by id) as balance
from tbl t
order by id
Fiddle: http://sqlfiddle.com/#!15/97dc5/2/0
Consider changing your column names "In" / "Out" so that you don't need to put them in quotes. (They are reserved words)
If you wanted only one customer (customer_id = 2):
select t.*, sum("In"-"Out") over(order by id) as balance
from tbl t
where customer_id = 2
order by id
If your query were to span multiple customers and you wanted a running balance that RESTARTED with each customer, you could use:
select t.*, sum("In"-"Out") over( partition by customer_id
order by customer_id, id ) as balance_by_cust
from tbl t
order by customer_id, id

RDLC Report Merge 2 Rows In 1 Row NAV 2013

I have a question about RDLC Report in NAV 2013
I have 2 rows like this
Store10 2.00 5.00 0.00 0.00
Store10 0.00 0.00 3.00 9.00
And I want 1 row by merging these, like;
Store10 2.00 5.00 3.00 9.00
How can it possible ?
Thanks for your answers.
First of all,
If rows first column is unique for your data. You should Group On this field in Visual Studio .
I couldn't add an image for reputation..
After that, Your line will shown merged. So, values maybe missing.
For calculating values effectively, You must use SUM(Field) syntax in "Table Cell Expression"
Like That
=SUM(Fields!RecNo.Value)

Using multiple tables to find cost by transactiondate and location

I am trying to create a query that joins 3 tables with multiple prices and locations. Ex of the data I'm pulling.
Table A - product, product name
Table B - transactionid, date, product, returnreason, quantity, costprice, costamount,
location, journalid
Table C - price1, price2, price3, price4, price5, price6, product, activedate
Table C is updating prices for our products and can be the same from one date to another, or completely change depending on the market. Location in Table B can be up to four different locations.
Ex. Table C (each product is setup basically the same in the tables.)
product price1 price2 price3 price4 price 5 price6 activedate
A 0.00 0.00 0.00 0.00 0.00 0.00 1/1/11
A 0.00 0.00 0.00 0.00 0.00 0.00 2/1/11
A 1.00 0.00 0.63 0.00 1.20 0.20 1/1/12
A 1.20 0.53 0.01 1.00 0.42 0.00 4/1/13
Table B
product transactionid tdate returnreason quantity costprice costamount location journalid
A 00001 5/1/11 def 100 2.00 2.50 100 000010
B 00205 6/13/11 col 250 10.00 15.00 300 000320
A 00207 4/11/13 col 50 5.00 1.50 100 000720
I need to get the information out by the most current price for the item by date. So if I need a yearly report, I can see that for product A - price1 for 1/1/13-4/30/13 was 1.00 and for 5/1/13-present the price is now 1.20.
I have tried many things including sub queries and the latest (which is closest) to limit it by creating a where statement to be activedate<=tdate which will bring me every active date below and equal to the tdate. For this please assume product A is at location 100. How can I limit to the product active during the time frame it would be active?
Most recent...
I added line number in to make sure I got each line of the journal in...just visual.
select distinct(b.transactionid), b.tdate, b.linenum b.product, b.location,
b.journalid, c.price1, c.price2, c.price3, c.price4, c.price5, c.activedate
from Table b inner join
Table c on c.product=b.product
where c.activedate <= b.tdate
When I run this I get all dates that were less than the transaction date, but I need specific. For example the first transaction in my example, it happened on 5/1/11, when I run the query it will give me the results from table c for 1/1/11 and 2/1/11. I just need the date of the price for when the transaction took place.