Click here to Skip to main content
15,891,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

How can i manage to do this,
it would require to show my sql entries to be per date in a column, so it means that every entry date would become a column and be a report.


SQL
SELECT remittance_no, remit_date, messenger, item, item_value, rate.product, rate.product_value
FROM remittance
INNER JOIN rate
ON rate.product=remittance.item


SAMPLE OUTPUT:

remittance_no remit_date messenger item item_value product product_value
REMMKT00003 12/4/2015 MESSENGER 3 PRODX 150 PRODX 1.85
REMMKT00003 12/4/2015 MESSENGER 3 PRODX 150 PRODX 1.75
REMMKT00003 12/4/2015 MESSENGER 3 PRODX 150 PRODX 1.65
REMMKT00003 12/4/2015 MESSENGER 3 PRODX 150 PRODX 1.5
REMMKT00004 12/5/2015 MESSENGER 3 PRODX 7 PRODX 1.85
REMMKT00004 12/5/2015 MESSENGER 3 PRODX 7 PRODX 1.75
REMMKT00004 12/5/2015 MESSENGER 3 PRODX 7 PRODX 1.65
REMMKT00004 12/5/2015 MESSENGER 3 PRODX 7 PRODX 1.5
REMMKT00004 12/6/2015 MESSENGER 3 PRODX 7 PRODX 1.85
REMMKT00004 12/6/2015 MESSENGER 3 PRODX 7 PRODX 1.75
REMMKT00004 12/6/2015 MESSENGER 3 PRODX 7 PRODX 1.65
REMMKT00004 12/6/2015 MESSENGER 3 PRODX 7 PRODX 1.5
REMMKT00004 12/7/2015 MESSENGER 3 PRODX 7 PRODX 1.85
REMMKT00004 12/7/2015 MESSENGER 3 PRODX 7 PRODX 1.75
REMMKT00004 12/7/2015 MESSENGER 3 PRODX 7 PRODX 1.65
REMMKT00004 12/8/2015 MESSENGER 3 PRODX 7 PRODX 1.85
REMMKT00004 12/8/2015 MESSENGER 3 PRODX 7 PRODX 1.75
REMMKT00004 12/8/2015 MESSENGER 3 PRODX 7 PRODX 1.65


each same date should have a single column output, ex: product | day1 | day2 | day3 | day4 | day5

Thank you so much,

raz
Posted
Updated 11-Dec-15 15:34pm
v6
Comments
CHill60 11-Dec-15 11:44am    
Do you want to share the code that you ran to get these results?
You've given us nothing to go on - language, RDBMS, schema.
Are the dates fixed, a limited number of dates, by month by year?
craft_trone 11-Dec-15 20:15pm    
Sorry for those vague details.

I dont have the code to get the result which i wanted to get. All i can do is, retrieved the data from the sql database and show it in my datagridview but still, the dates where on a single column and i cant chop it like per day every column


but i can tell that i want a table or a result like this:


product | day1 | day2 | day3 | day4 | day5 | day6 | total |
Tomas Takac 12-Dec-15 5:31am    
Sounds like you want PIVOT[^].
CHill60 12-Dec-15 6:39am    
Sounds like the solution to me!
craft_trone 14-Dec-15 20:17pm    
does it work with mysql workbench? i created my database in workbench

1 solution

As Tomas Takac says, you need to know about PIVOT. As well as the link he has provided there are several articles here on CodeProject.

Here is an example of how your sample data could be pivoted

SQL
select remittance_no, messenger, item, item_value, product
	, isnull([2015-12-04],0) as day1
	, isnull([2015-12-05],0) as day2
	, isnull([2015-12-06],0) as day3
	, isnull([2015-12-07],0) as day4
	, isnull([2015-12-08],0) as day5
from
(
	select remittance_no, remit_date, messenger, item, item_value, rate.product, rate.product_value
	FROM remittance
	INNER JOIN rate
	ON rate.product=remittance.item
) as sourcedata
PIVOT
(
	sum(product_value) for remit_date in ([2015-12-04], [2015-12-05], [2015-12-06],[2015-12-07],[2015-12-08])
)  pvt
which provides the results
REMMKT00003 MESSENGER 3	PRODX	150	PRODX	6.75	0	0	0	0
REMMKT00004 MESSENGER 3	PRODX	7	PRODX	0	6.75	6.75	5.25	5.25


It is highly likely though, that you are not going to know which dates are required in the final result. In which case you need to know about Dynamic Pivoting. This post[^] includes an example of Itzik Ben-Gan's approach to building the pivot column list dynamically.


[EDIT - some further information after a comment below]
There are lots of articles here on CodeProject that can guide you through connecting to MySQL - e.g. Connecting to MySQL Database using C# and .NET[^]

To get your new query built using a DateTimePicker you can do something similar to the code that follows. I've assumed that you want the data for the 14 days preceding the date in the dtp plus the date on the dtp.

VB.NET
Dim sCommand As MySqlCommand = New MySqlCommand()
Dim sqlQuery As StringBuilder = New StringBuilder()

sqlQuery.Append("select MSGR,PRODNAME,RteVal,")

'Set up the start date for 14 days prior to the datepicker (i.e. datepicker date is 15th date)
Dim CalcDate As Date = DateTimePicker1.Value.Date.AddDays(-14)
For i As Integer = 1 To 15
    sqlQuery.Append(String.Format("sum(case when rmdte = @Date{0} then ProdVal else 0 end) `DAY {1}`,", 15 - i + 1, i))

    sCommand.Parameters.AddWithValue(String.Format("@Date{0}", i), CalcDate)
    CalcDate = CalcDate.AddDays(1)
Next

sqlQuery.Append("sum(ProdVal)*rteval as Total from")
sqlQuery.Append("select date(remit_date) as rmdte, messenger as Msgr, item as ProdName, item_value as ProdVal,")
sqlQuery.Append("b.product as RteProd,b.product_value as Rteval FROM remittance a JOIN rate b on a.item=b.product")
sqlQuery.Append("where a.messenger = 'MESSENGER 3' and b.ratecode = '5i.35' ) as SOURCEDATA group by ProdName")

sCommand.CommandText = sqlQuery.ToString()
 
Share this answer
 
v2
Comments
craft_trone 15-Dec-15 22:04pm    
Hi Chill60,
Thank you so much for those helps.

I did create some code base on some examples which works with workbench and gather all things i've wanted:



select
MSGR,PRODNAME,RteVal,
sum(case when rmdte = '2015-12-04' then ProdVal else 0 end) `DAY 1`,
sum(case when rmdte = '2015-12-05' then ProdVal else 0 end) `DAY 2`,
sum(case when rmdte = '2015-12-06' then ProdVal else 0 end) `DAY 3`,
sum(case when rmdte = '2015-12-07' then ProdVal else 0 end) `DAY 4`,
sum(case when rmdte = '2015-12-08' then ProdVal else 0 end) `DAY 5`,
sum(case when rmdte = '2015-12-09' then ProdVal else 0 end) `DAY 6`,
sum(case when rmdte = '2015-12-10' then ProdVal else 0 end) `DAY 7`,
sum(case when rmdte = '2015-12-12' then ProdVal else 0 end) `DAY 8`,
sum(ProdVal)*rteval as Total
from
(
select
date(remit_date) as rmdte,
messenger as Msgr,
item as ProdName,
item_value as ProdVal,
b.product as RteProd,
b.product_value as Rteval
FROM remittance a
JOIN rate b on a.item=b.product
where a.messenger = 'MESSENGER 3' and b.ratecode = '5i.35'
) as SOURCEDATA
group by ProdName



by the way, i wanted to put this in my vb2013 windows forms? how can i make it generate a 15 days columns (BASE ON THE COLUMNS GIVEN ABOVE)base on the value of my datepicker? using datagridview ???
i dont know what to do. ;(
CHill60 16-Dec-15 5:20am    
I've added some extra information to my solution.
craft_trone 16-Dec-15 20:30pm    
OMG, thank you very much chill, Whats with the code:

Private Sub TEST_CHILL20()

Dim sCommand As MySqlCommand = New MySqlCommand()
Dim sqlQuery As System.Text.StringBuilder = New System.Text.StringBuilder()

sqlQuery.Append("select MSGR,PRODNAME,RteVal,")
'Set up the start date for 14 days prior to the datepicker (i.e. datepicker date is 15th date)
Dim CalcDate As Date = dtFrom.Value.Date.AddDays(-14)
For i As Integer = 1 To 15
sqlQuery.Append(String.Format("sum(case when rmdte = @Date{0} then ProdVal else 0 end) `DAY {1}`,", 15 - i + 1, i))

sCommand.Parameters.AddWithValue(String.Format("@Date{0}", i), CalcDate)
CalcDate = CalcDate.AddDays(1)
Next

sqlQuery.Append("sum(ProdVal)*rteval as Total from")
sqlQuery.Append("select date(remit_date) as rmdte, messenger as Msgr, item as ProdName, item_value as ProdVal,")
sqlQuery.Append("b.product as RteProd,b.product_value as Rteval FROM remittance a JOIN rate b on a.item=b.product")
sqlQuery.Append("where a.messenger = 'MESSENGER 3' and b.ratecode = '5i.35' ) as SOURCEDATA group by ProdName")

connectionString = "Server=localhost ; Port=3306 ; Database=zipdb ; user id=root; password='L3265_Vi42ck9_Ho83l13_';"
cnn = New MySqlConnection(connectionString)
cnn.Open()

sCommand.CommandText = sqlQuery.ToString()
Dim dscmd As New MySqlDataAdapter(Xsql, cnn)
Dim ds As New DataSet
dscmd.Fill(ds)
dgw.DataSource = ds.Tables(0)
cnn.Close()
End Sub





the error says that: "An unhandled exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll
Additional information: The CommandText property has not been properly initialized."

thank you so much
CHill60 17-Dec-15 5:10am    
A good idea is to Debug.Print or Console.WriteLine the sqlQuery.ToString() and put it into SSMS and attempt to run it - that way you can use the SQL parser to find the errors in the T-Sql.
I'd (wrongly) assumed the sql you had presented in your comment already worked and simply showed you how to get the 15 days from the datepicker.
Some things to look at ...
1. There are strange characters around "Day {1}" - ` instead of ' or use [ and ] instead
2. You need a space in front of select date... and where a.messenger ...
3. There is a missing ( after as Total from
4. You need to group by ProdName, Msgr, Rteval if you want those last two columns in the results (or use another sub-query or a CTE)
I think fixing those 4 things will get your SQL right in which case the error in the VB should go away

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900