1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

SQL help

Discussion in 'BBS Hangout' started by Luckyazn, Jun 4, 2012.

Tags:
  1. Luckyazn

    Luckyazn Member

    Joined:
    Jun 23, 2003
    Messages:
    4,375
    Likes Received:
    68
    I have a query I wrote, so that I can count number of days with (Credit) in a time frame
    dropping the time from month/day/yr:


    select count (distinct extract (day from column_name)) as Number_of_days
    from table_name
    where (enter time frame to time frame)**date
    and (column_name='credit')
    order by extract (day from column_name)

    If I put April 1, 2012 to April 30, 2012 I would get :
    Number_of_days
    21


    **21 days of April have "Credit"

    I remove some of the real info and added examples.

    Anyway, does another know what I would need to do to get multiple months and results?

    Like if I wanted to show April and May and count for each to display.

    I thought I would just add :
    select count Month (distinct extract (day from column_name)) as Number_of_days
    from table_name
    where (enter time frame to time frame)**date
    and (column_name='credit')
    order by extract (day from column_name)
    group by month (distinct extract (day from column_name)
     
    #1 Luckyazn, Jun 4, 2012
    Last edited: Jun 4, 2012
  2. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    I'm not too familiar with MySQL but this may work. You'll need to move the month in the select statement to before "count" and group it by the month, not the day.

    select extract (month from column_name), count (distinct extract (day from column_name)) as Number_of_days
    from table_name
    where (enter time frame to time frame)**date
    and (column_name='credit')
    order by extract (day from column_name)
    group by extract (month from column_name)

    This works in SQL Server to give you and idea of it, but I think it would group data from the same month in different years as it's written now.

    select MONTH(column), count(distinct (CONVERT(date, column)))
    from Table
    group by MONTH(column)
     
  3. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
  4. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    I don't think DateDiff will give him what he's looking for, since not all days in that range will have an entry. For the range:

    6/1/12
    6/2/12
    6/3/12
    6/4/12

    There are four days, but there may only be entries for two of the days.
     
  5. Luckyazn

    Luckyazn Member

    Joined:
    Jun 23, 2003
    Messages:
    4,375
    Likes Received:
    68
    Thanks! This one help

    Now it shows "4" (April) "21" Credit
    "5" (May) "23" Credit

    How do you convert "4" to display "April" ?
     
  6. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    Try using the MONTHNAME function.

    I'm not sure if it will work, but try this:
    select MONTHNAME(column_name), count (distinct extract (day from column_name)) as Number_of_days
    from table_name
    where (enter time frame to time frame)**date
    and (column_name='credit')
    order by extract (day from column_name)
    group by MONTHNAME (column_name)
     
  7. Luckyazn

    Luckyazn Member

    Joined:
    Jun 23, 2003
    Messages:
    4,375
    Likes Received:
    68
    seems like I have to add in a

    to_char('mm' ... or

    to_char('month' ...

    type statement
     
  8. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    Are you getting an error message? Can you post what it says?
     
  9. Luckyazn

    Luckyazn Member

    Joined:
    Jun 23, 2003
    Messages:
    4,375
    Likes Received:
    68
    I added

    to_char(column_name,'Month') as Month
    now I have two columns

    one with Month with the numbers "3"

    another with Month_1 with the name "March"


    for some reason I can't get to_char to go into the extract(month from column_name)
    statement to only display the NAME column
     
  10. Prince

    Prince Member

    Joined:
    Apr 27, 2009
    Messages:
    5,375
    Likes Received:
    161
    You might want to create your own business calendar.. if you just want remove weekends... tehre's an oracle function for that..
     
  11. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    I'm not sure what's going on there, and not knowing MySQL very well and not having a place to try stuff out I'm not sure how to solve it.
     
  12. Luckyazn

    Luckyazn Member

    Joined:
    Jun 23, 2003
    Messages:
    4,375
    Likes Received:
    68
    Thanks for the help anyway, DrLudicrous.

    I think I have it now, but I'm just stuck on:

    how do you order by "month order"

    Is listing April, Feb, Jan, March, May in ABC order

    how do you get "month order" like "jan, feb, march, april, may"

    thanks anyone,
     
  13. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    You should be able to order it by the numeric month value, even if it's not in the select statement.
     
  14. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    Sounds like someone missed a few SQL classes. :p

    SRSLY, though, GUISE, I don't think he's really "azn", or he would have already solved it. ;)
     
  15. David Stern

    David Stern Member

    Joined:
    Oct 7, 2008
    Messages:
    1,021
    Likes Received:
    33
    Ok…so let me start off by saying I’m not a programmer and I’ve never used marcos, VBA, modules, etc…

    Other than that I’m very proficient with Access (yes I do realize I’m greatly limiting my ability to maximize utility I can get from Access but I plan on schooling myself on VBA in the near future)

    My problem is that I have about 60 excel files that I would like to batch import into separate tables in access. I’ve read blogs and tried some of the custom functions posted but I can’t get anything to work.

    Can anyone help me out? I’m probably missing something very basic on wheather I should be utilizing a procedure, a module or a class module.

    Any help would be much appreciated!

    -Basketball reasons
     
  16. EssTooKayTD

    EssTooKayTD Member

    Joined:
    Dec 15, 2005
    Messages:
    3,343
    Likes Received:
    74
    Dumb question, but do you have access to any developer resources? Just have them write something up for you real quick.
     
  17. David Stern

    David Stern Member

    Joined:
    Oct 7, 2008
    Messages:
    1,021
    Likes Received:
    33
    At my previous employer i had a programmer that worked closely with me but not at my current place of employment..
     
  18. EssTooKayTD

    EssTooKayTD Member

    Joined:
    Dec 15, 2005
    Messages:
    3,343
    Likes Received:
    74
    Ah that sucks. As far as doing it all as one batch, I'm not too sure there's a way outside of some code or at least VB script(I'm not well versed on Access myself), but you might be stuck doing the excel files one by one :/

    http://support.microsoft.com/kb/141228

    60 is not TOOO bad, heh. I'm probably wrong, but it's just my guess.


    As the commish, can't you get me season tickets?
     
  19. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    lottery??? :confused:

    :grin: Your last question was pretty good, EssTooKayTD.
     

Share This Page