I'm trying to create dynamic SQL in MySQL - mysql

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

Related

Access Event Driven Macro

Table1:
ID Name XCount
1 Bob 12
2 Jack 13
Table2:
ID XCount YCount
1 14 1
2 22 15
When i insert a new record (on this example as a third record) to the Table1 then;
if that third record's XCount value is equal to some row's XCount Field on the Table2;
Than that rows YCount field will be updated as ycount=ycount+1 on the Table2.
Input: Table 1
ID Name XCount
3 George 14
Output: Table2:
Id Xcount YCount
1 14 2
2 22 15
I tried a couple of times with "After Insert Macro" but can't handle it. Can anyone help me to overcome this situation?

Want to rewrite the query as follows to display data

I have a result table like this
UniqueID
Value
1
100
1
98
1
99
2
100
2
98
3
99
and I want to display the result as follows using SQL
UnqiueID
Value1
Value2
Value3
1
100
98
99
2
100
98
3
99
Please suggest queries using single table and multiple tables

Get the maximum field value of different row's between different tables and its id's

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.

How to change record in table with consecutive value?

I have table like below :
Application_Number Id_Number1 Name
1 123 John
2 456 Alan
3 789 Charlie
4 111 Patrick
5 222 Robert
Then i would like to update record in one of rows become like this :
Application_Number Id_Number1 Name
1 123 Alias 1
2 456 Alias 2
3 789 Alias 3
4 111 Alias 4
5 222 Alias 5
if i have more than one million record do i need update syntax or any another way? I'm using SQL2008
Thanks
Select Application_Number
,Id_Number1
,'Alias' + CAST( ROW_NUMBER()
OVER (ORDER BY Application_Number) AS Varchar) AS Name
FROM Table Name

GROUP BY using a SUM

This is a tough one to explain. I have this data set
Data set 1
counter | id
1 280
1 280
0 280
1 781
1 781
1 781
0 25
1 25
1 25
I want to GROUP BY the id, but with the SUM of counter so I end up with this data set
Data set 2
counted | id
2 280
3 781
2 25
I have tried a number of approaches but I always end up with the total SUM of rows for each ID like this
Data set 3
counted | id
3 280
3 781
3 25
EDIT
It might be worth noting that data set 1 comes from a sub query and is not a table in itself.
EDIT
I have used a query like this, it gives me the result of data set 3
select sum(counter) as counted, id
from t
group by id;
Did you do this?
select sum(counter) as counted, id
from t
group by id;