tags:

views:

22

answers:

1

I have a stored procedure that takes data from multiple tables. The stored procedure is called from a PHP script and the resulting data is placed in a grid.

The issue I'm having is I'm not sure how to get the dayname to be unique so that at maximum there are seven columns, one for each day of the week, as opposed to getting results like 'Tuesday' and 'Tuesday1'

Here is a snippet

    select concat(
                 'select   tbl_kiosks.name "DCI ERP",tbl_retailers.abbr "Retailer",
   tbl_retaillocations.storeNumber "Store",
   tbl_retaillocations.city "City",
   tbl_retaillocations.state "State"'
             ,   group_concat(
                     concat(',sum(if(ks.StartDate="',ks3.StartDate,'", numOfSessions, null)) "', dayname(DATE_FORMAT(ks3.StartDate,'%Y-%m-%d')) ,'" '
                     )
                     separator '  '
                 )
A: 

Somewhere in your group_concat, you have to select distinct dayname(...) instead of select dayname(...). That would give you only one column of each day.

Or you could consider a group by:

select 
    tbl_kiosks.name "DCI ERP"
,   tbl_retailers.abbr "Retailer"
,   tbl_retaillocations.storeNumber "Store"
,   tbl_retaillocations.city "City"
,   tbl_retaillocations.state "State"
,   dayname(ks3.StartDate) "Weekday"
,   sum(case when dayname(ks3.StartDate) = 'Monday' 
        then numOfSessions else 0 end) as Monday
,   sum(case when dayname(ks3.StartDate) = 'Tuesday' 
        then numOfSessions else 0 end) as Tuesday
,   ....
from <your join list here>
group by "DCI ERP", Retailer, Store, City, State, dayname(ks3.StartDate)
Andomar
I actually tried doing that but for some reason selecting the dayname as distinct produced the same results. I'm still trying things, so I'll post back with more information if I sort it out.
AmateurHour