SQL Server Taking data to one row from next row - sql-server

I have the table:
| IntervalID | From |
| ---------- | ---- |
| 1 | 1 |
| 2 | 10 |
| 3 | 20 |
| 4 | 30 |
| 5 | 40 |
These table defines me intervals. First would be [1,10], second -[10,20].
I would like to have the table of these intervals. It would look like this:
| IntervalID | From | To |
| ---------- | ---- | -- |
| 1 | 1 | 10 |
| 2 | 10 | 20 |
| 3 | 20 | 30 |
| 4 | 30 | 40 |
| 5 | 40 | 50 |
How can I do this? The thing is that in column TO the value has to be taken from next row in column From.

Related

Design lookup table in SQL Server

I am trying to design EAV database for my website. Here are tables
Category
-------------------------
| Id | Title |
|-----------------------|
| 1 | Supplier |
| 2 | Material |
| 3 | RAM |
| 4 | CPU |
| 5 | Product |
-------------------------
Item
-------------------------
| Id | Category_Id |
|-----------------------|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
| 7 | 3 |
| 8 | 4 |
| 9 | 4 |
| 10 | 4 |
| 11 | 5 |
| 12 | 5 |
-------------------------
AttributeType
-------------------------
| Id | Title |
|-----------------------|
| 1 | String |
| 2 | Text |
| 3 | Number |
| 4 | Lookup |
-------------------------
Attribute
-------------------------
| Id | Title |
|-----------------------|
| 1 | Name |
| 2 | Price |
| 3 | Suplier |
| 4 | RAM |
| 5 | CPU |
| 6 | Material |
| 7 | Address |
-------------------------
AttributeStringValue
-----------------------------------------------------------------
| Id | Attribute_Id | Item_Id | Value |
|---------------------------------------------------------------|
| 1 | 1 | 1 | Samsung |
| 2 | 1 | 2 | Nokia |
| 3 | 1 | 3 | Dell |
| 4 | 1 | 4 | Steal |
| 5 | 1 | 5 | Plastic |
| 6 | 1 | 6 | Ram 8GB |
| 7 | 1 | 7 | Ram 16GB |
| 8 | 1 | 8 | Core I3 |
| 9 | 1 | 9 | Core I5 |
| 10 | 1 | 10 | Core I7 |
| 11 | 1 | 11 | Mobile Nokia 1 |
| 12 | 1 | 12 | Laptop Dell 1 |
| 13 | 7 | 1 | Korea |
| 14 | 7 | 2 | Finland |
| 15 | 7 | 3 | USA |
-----------------------------------------------------------------
AttributeNumberValue
-----------------------------------------------------------------
| Id | Attribute_Id | Item_Id | Value |
|---------------------------------------------------------------|
| 1 | 1 | 11 | 100 |
| 2 | 1 | 12 | 500 |
-----------------------------------------------------------------
I want Mobile Nokia 1 and Laptop Dell 1 have supplier names that are lookuped to the items that have category is Supplier and I can config display column.
Ex:
Supplier have values: Nokia (address: Finland), Samsung(address: Korea)
"Mobile Nokia 1" has supplier column is items in the supplier category and I can configure to display "Address" column or "Title" column of supplier
Please give me advise to design lookup tables
Sorry for my english
Many thanks

Sum, Group by and Null

I'm dipping my toes into SQL. I have the following table
+------+----+------+------+-------+
| Type | ID | QTY | Rate | Name |
+------+----+------+------+-------+
| B | 1 | 1000 | 21 | Jack |
| B | 2 | 2000 | 12 | Kevin |
| B | 1 | 3000 | 24 | Jack |
| B | 1 | 1000 | 23 | Jack |
| B | 3 | 200 | 13 | Mary |
| B | 2 | 3000 | 12 | Kevin |
| B | 4 | 4000 | 44 | Chris |
| B | 4 | 5000 | 43 | Chris |
| B | 3 | 1000 | 26 | Mary |
+------+----+------+------+-------+
I don't know how I would leverage Sum and Group by to achieve the following result.
+------+----+------+------+-------+------------+
| Type | ID | QTY | Rate | Name | Sum of QTY |
+------+----+------+------+-------+------------+
| B | 1 | 1000 | 21 | Jack | 5000 |
| B | 1 | 3000 | 24 | Jack | Null |
| B | 1 | 1000 | 23 | Jack | Null |
| B | 2 | 3000 | 12 | Kevin | 5000 |
| B | 2 | 3000 | 12 | Kevin | Null |
| B | 3 | 200 | 13 | Mary | 1200 |
| B | 3 | 1000 | 26 | Mary | Null |
| B | 4 | 4000 | 44 | Chris | 9000 |
| B | 4 | 5000 | 43 | Chris | Null |
+------+----+------+------+-------+------------+
Any help is appreciated!
You can use window function :
select t.*,
(case when row_number() over (partition by type, id order by name) = 1
then sum(qty) over (partition by type, id order by name)
end) as Sum_of_QTY
from table t;

Need to update "orderby" column

I have a table test
+----+--+------+--+--+----------+--+--------------+
| ID | | Name | | | orderby | | processgroup |
+----+--+------+--+--+----------+--+--------------+
| 1 | | ABC | | | 10 | | 1 |
| 10 | | DEF | | | 12 | | 1 |
| 15 | | LMN | | | 1 | | 1 |
| 44 | | JKL | | | 4 | | 1 |
| 42 | | XYZ | | | 3 | | 2 |
+----+--+------+--+--+----------+--+--------------+
I want to update the orderby column in the sequence, I am expecting output like
+----+--+------+--+--+----------+--+--------------+
| ID | | Name | | | orderby | | processgroup |
+----+--+------+--+--+----------+--+--------------+
| 1 | | ABC | | | 1 | | 1 |
| 10 | | DEF | | | 2 | | 1 |
| 15 | | LMN | | | 3 | | 1 |
| 44 | | JKL | | | 4 | | 1 |
| 42 | | XYZ | | | 5 | | 1 |
+----+--+------+--+--+----------+--+--------------+
Logic behind this is when we have procesgroup as 1, orderby column should update as 1,2,3,4 and when procesgroup is 2 then update orderby as 5.
This might help you
;WITH CTE AS (
SELECT ROW_NUMBER() OVER (ORDER BY processgroup, ID ) AS SNO, ID FROM TABLE1
)
UPDATE TABLE1 SET TABLE1.orderby= CTE.SNO FROM CTE WHERE TABLE1.ID = CTE.ID

SSRS 2008 R2 Row/Column Group Issues

I am using Row Group And Column Group in SSRS 2008 R2.
I have design the report contain two row groups(et.Pixel、Name) and one column group(et.Date).
Preview Report as bellow:
| Date1 | Date2 | Date2 |
Pixel | Name | Input | Ng | Name | Input | Ng | Name | Input | Ng |
| XXX1 | 1000 | 2 | | | | | | |
| | | | YYY1 | 2000 | 1 | | | |
2M | | | | YYY2 | 1000 | 2 | | | |
| | | | YYY3 | 3000 | 5 | | | |
| | | | | | | ZZZ1 | 800 | 2 |
| | | | | | | ZZZ2 | 500 | 3 |
|Total | 1000 | 2 |Total | 6000 | 8 |Total | 1300 | 5 |
My question is, How do I get the Preview Report don't show white space column in report.
For example:
| Date1 | Date2 | Date2 |
Pixel | Name | Input | Ng | Name | Input | Ng | Name | Input | Ng |
| XXX1 | 1000 | 2 | YYY1 | 2000 | 1 | ZZZ1 | 800 | 2 |
2M | | | | YYY2 | 1000 | 2 | ZZZ2 | 500 | 3 |
| | | | YYY3 | 3000 | 5 | | | |
|Total | 1000 | 2 |Total | 6000 | 8 |Total | 1300 | 5 |
It's the grouping by name which is causing the issue that you are having. Since the name is different they won't be on the same line.
On the plus side, you can probably work around this. If the data is like you display it, I would group on the numeric number in the name instead of the whole name.
=MID(Fields!Name.Value, 4, LEN(Fields!Name.Value) - 3)
Of course you couldn't have XXX and YYY data on the same date with this expression otherwise you would have multiple rows.

Table data into Pivot

An Accounting Table has the sample data shown below:
(There could be more AcctgHeads than those shown here)
+-------+-----------+--------+-----+
| Loan | AcctgHead | Amount | D_C |
+-------+-----------+--------+-----+
| 1 | Principal | 10000 | D |
| 1 | Principal | 500 | C |
| 1 | Cash | 10000 | C |
| 1 | Cash | 500 | D |
| 2 | Principal | 5000 | D |
| 2 | Cash | 5000 | C |
| 2 | Cash | 300 | D |
| 2 | Principal | 300 | C |
| 1 | IntDue | 50 | D |
| 1 | IntIncome | 50 | C |
+-------+-----------+--------+-----+
The desired ouput is:
+------+-------------+-------------+--------+--------+----------+----------+-------------+-------------+
| Loan | Principal_D | Principal_C | Cash_D | Cash_C | IntDue_D | IntDue_C | IntIncome_D | IntIncome_C |
+------+-------------+-------------+--------+--------+----------+----------+-------------+-------------+
| 1 | 10000 | 500 | 500 | 10000 | 50 | 0 | 0 | 50 |
| 2 | 5000 | 300 | 300 | 5000 | 0 | 0 | 0 | 0 |
+------+-------------+-------------+--------+--------+----------+----------+-------------+-------------+
What would be the query to accomplish this?
Thanks in advance for the help.
Try this: (assuming your acctghead has fixed number of values as shown)
select loan,
isnull(Principal_D,0) Principal_D,
isnull(Principal_C,0) Principal_C,
isnull(Cash_D,0) Cash_D,
isnull(Cash_C,0) Cash_C,
isnull(IntDue_D,0) IntDue_D,
isnull(IntDue_C,0) IntDue_C,
isnull(IntIncome_D,0) IntIncome_D,
isnull(IntIncome_C,0) IntIncome_C
from
(select loan, amount, AcctgHead + '_' + D_C As AcctgHeadDC from t) t
pivot
(
max(amount) for AcctgHeadDC in
(Principal_D,Principal_C,Cash_D,Cash_C,
IntDue_D,IntDue_C,IntIncome_D,IntIncome_C)
) p
SQL DEMO

Resources