Is it possible to convert below current table into the target view using a stored procedure or view? ID is unique here and each ID may contain different type of accounts and each type can have more than 1 number. can anyone help ?
Current table (in SQL Server 2008):
ID Type Account Balance
---------------------------------
1 checking 11111 100
1 checking 22222 200
1 savings 33333 300
1 savings 44444 400
2 investment 55555 500
2 checking 66666 600
Target (below record is for ID value 1)
Checking1_Account Checking2_Account Checking1_Balance Checking2_Balance Savings1_Account Savings2_Account Savings1_Balance Savings2_Balance
11111 22222 100 200 33333 44444 300 400
Target (below record is for ID value 2)
Investment1_Account Investment1_Balance Checking1_Account Checking1_Balance
55555 500 66666 600
Related
I'm trying to update the same table after an insert's trigger.
I know that you can't update the same table on a trigger but I can't seem to find an alternative.
I have for example the following table :
id | team | time
1 A 1 568 725 200
2 A 1 568 725 200
3 B 1 568 824 950
If I update id 1 time from 1 568 725 200 to 1 568 455 800, id 2 time's should also be updated to 1 568 455 800
because they are in the same team (A).
Is there a solution to achieve this update inside the trigger's transaction?
I'm working on a bad designed database and I can't change much.
I use MySql and I have some problems with it ^^
Let's say I have two tables which will be "FirstTable" and "SecondTable"
First table (ID isn't the primary key)
ID IdCust Ref
1 300 123
1 300 124
2 302 345
And the second (ID isn't the primary key)
ID Ref Code Price
1 123 A 10
1 123 Y 15
2 124 A 14
3 345 C 18
[EDIT] The column "Stock" in the final result is equals to "ID" in the second table
The column "Stock", "Code" and "Price" can have x values, so I don't know it, in advance...
I make some research in stackoverflow, but I only find post where people use "count(case when ..."
Like this one : MySQL pivot row into dynamic number of columns
For my problem, I cannot use it, because i can't know in advance the value of reference
I'm trying to produce the following output:
The result I want
[EDIT] Result in TXT
ID IdCust Ref StockA Code1 Price1 StockB Code2 Price2
1 300 123 1 A 10 1 Y 15
1 300 124 2 A 14
2 300 345 3 C 18
Fiddler Here: http://sqlfiddle.com/#!9/9579da/3
Given this table of products
CountryId Product Quantity
1 Apple 100
1 Banana 5000
2 Carrot 50000
3 Apple 500
4 Apple 250
6 Apple 6000
And this table of available money
CountryId Quantity Rate
1 4135 0.005
1 870.5 1
1 1000 1
2 7249.71 0.007
2 1788 0.01
2 10 1
2 352 10
3 1900 0.09
4 29877 0.005
5 7108 0.005
Where Rate represents an exchange rate for the country. Not shown in the money table are seller Ids.
So for the first product, There are 100 apples in country 1. The Pricing for those Apples would be 100 * (0.005) because there was 4135 available money units for the 0.005 rate, of which we only needed 100 for our apples.
Another example: There are 5000 bananas for country 1. The pricing of those Bananas would be 4135 * (0.005) + 865 * (1). There wasn't enough 4135 money units at rate 0.005 so it had to take 865 units from the next available rate which was 1.
I am trying to use this logic and join the money table onto the products table and have a Price column. I have no idea how to start because it is not a simple Join
So we got four tables T1,T2,T3,T4
Each table has four columns, id,1,x,2 (id is primary key auto incremented in all tables) and all of them are populated with numbers (40-50 rows each).
How can I compare the first field of each table and column (and same for all the remaining fields) and display the maximum field for each column and its id?
For example, compare column 1/field 1 of T1 with column 1/field 1 of T2 and column 1/field 1 of T3 display the maximum and its id and then do the same for column 2/field 1 OF T1/T2/T3/T4/ etc etc and each of their respective fields for all 40 rows ?
I have read about UNION and JOINS but I don't know how to do this.
Any help would be appreciated.
TABLE 1
ID | 1 | X | 2 |
1 10 20 30
2 5 45 6
3 3 11 12
4 0 14 23
TABLE 2
ID | 1 | X | 2 |
1 100 200 300
2 50 405 60
3 30 101 102
4 0 104 203
ETC ETC
we need to compare the 10 of table 1 column 1 row 1 id=1 with 100 of table 2 row/column/id=1 then the same for x and 2 column.
I recently imported an excel arch into an sql database. The values imported look like:
name age adress date
carl 12 something 2015-11-10
lisa 51 something2 2+15-10-09
steven 32 something3 2014-12-29
I then added an auto increment column named id, which resulted in:
id name age adress date
1 carl 12 something 2015-11-10
2 lisa 51 something2 2+15-10-09
3 steven 32 something3 2014-12-29
My problem is that I need to reverse the ids. Because if I now where to insert a new row, it'd result in the date column not matching up with the id column. It'd look like this:
id name age adress date
1 carl 12 something 2015-11-10
2 lisa 51 something2 2015-10-09
3 steven 32 something3 2014-12-29
4 neil 25 something4 2016-01-12
I've searched for methods of creating a new id column, reversed. Or reversing my existing column. Problem is I haven't succeeded very well.
Create a new empty table also with autoincremt and insert the rows in reverse order
INSERT into new_table
select '',name,age address,`date`
FROM org_table
ORDER BY id DESC;
Old topic but if anyone else is looking for an answer, here's what I found to do this:
SELECT MAX(id) as max_id FROM table;
Get this max_id value (example: 28542)
Ensure you that your "id" column is not UNSIGNED, otherwise, uncheck "Unsigned" param in your table structure mysql. And then:
UPDATE table SET id=(id - max_id) * (-100000);
example: UPDATE table SET id=(id - 28542) * (-100000);
And
UPDATE table SET id=(id/100000);
Results, before:
id name age adress date
1 carl 12 something 2015-11-10
2 lisa 51 something2 2015-10-09
3 steven 32 something3 2014-12-29
After:
id name age adress date
28541 carl 12 something 2015-11-10
28540 lisa 51 something2 2015-10-09
28539 steven 32 something3 2014-12-29
So the new record will be
28542 neil 25 something4 2016-01-12