Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. MySQL to MSSQL conversion of stmt

MySQL to MSSQL conversion of stmt

Scheduled Pinned Locked Moved Database
mysqlsql-serverhelplearning
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tim_gunning
    wrote on last edited by
    #1

    hi all im converting a load of queries from MySQL to MSSQL and have run into a problem and was wondering if anyone had any ideas..... I currently have:

    SELECT 'PI' AS transaction_type, t1.supplier_code, t1.nominal_code, '01-01-2006' AS my_dispatch_date, t2.name AS resourcename, t2.reference, '1.20' AS royalty_amount,
    t3.code, '1.21' AS tax_amount, '1' AS blank_field_2, '_' AS blank_field_3, '_' AS blank_field_4
    FROM order_line AS t4 INNER JOIN
    resource AS t2 ON t4.resource_id = t2.resource_id INNER JOIN
    supplier_codes AS t1 ON t2.reference = t1.reference AND t4.dispatch_date >= t1.payment_start_date AND t4.dispatch_date <= t1.payment_end_date INNER JOIN
    partners AS t5 ON t1.partner_id = t5.partner_id INNER JOIN
    tax_codes AS t3 ON t5.tax_code = t3.id INNER JOIN
    partner_type AS t6 ON t1.partner_type_id = t6.partner_type_id
    WHERE (t4.status = 1) AND (t4.dm <> 'ZX') AND (t4.dispatch_date >= @from) AND (t4.dispatch_date <= @to)
    ORDER BY t4.dispatch_date, t2.reference

    but i need to group by t1.reference, t1.supplier_code but obviously it keeps telling me I can't do that, my brains a bit fried atm, if anyones got any great ideas it would be really helpful ta tim

    T 1 Reply Last reply
    0
    • T tim_gunning

      hi all im converting a load of queries from MySQL to MSSQL and have run into a problem and was wondering if anyone had any ideas..... I currently have:

      SELECT 'PI' AS transaction_type, t1.supplier_code, t1.nominal_code, '01-01-2006' AS my_dispatch_date, t2.name AS resourcename, t2.reference, '1.20' AS royalty_amount,
      t3.code, '1.21' AS tax_amount, '1' AS blank_field_2, '_' AS blank_field_3, '_' AS blank_field_4
      FROM order_line AS t4 INNER JOIN
      resource AS t2 ON t4.resource_id = t2.resource_id INNER JOIN
      supplier_codes AS t1 ON t2.reference = t1.reference AND t4.dispatch_date >= t1.payment_start_date AND t4.dispatch_date <= t1.payment_end_date INNER JOIN
      partners AS t5 ON t1.partner_id = t5.partner_id INNER JOIN
      tax_codes AS t3 ON t5.tax_code = t3.id INNER JOIN
      partner_type AS t6 ON t1.partner_type_id = t6.partner_type_id
      WHERE (t4.status = 1) AND (t4.dm <> 'ZX') AND (t4.dispatch_date >= @from) AND (t4.dispatch_date <= @to)
      ORDER BY t4.dispatch_date, t2.reference

      but i need to group by t1.reference, t1.supplier_code but obviously it keeps telling me I can't do that, my brains a bit fried atm, if anyones got any great ideas it would be really helpful ta tim

      T Offline
      T Offline
      Tim Carmichael
      wrote on last edited by
      #2

      To use group by, you need an aggregate... sum(...) or count(...) or avg(...), etc. Tim

      T 1 Reply Last reply
      0
      • T Tim Carmichael

        To use group by, you need an aggregate... sum(...) or count(...) or avg(...), etc. Tim

        T Offline
        T Offline
        tim_gunning
        wrote on last edited by
        #3

        hi tim problem is...I dont want to do anything with them apart from get the values in the same way you do in MySQL, I just want to get rid of the duplicates that get pulled based on certain criteria. this is what I'm converting:

        SELECT

        'PI' as transaction_type,
        supplier_codes.supplier_code,
        supplier_codes.nominal_code,
        COUNT(o.orderline_id) as sale_volume,
        IF ( EXTRACT(MONTH FROM STR_TO_DATE(CONCAT(YEARWEEK(dispatch_date),'0'),'%x%v%w')) = MONTH(dispatch_date),
        DATE_FORMAT(STR_TO_DATE(CONCAT(YEARWEEK(dispatch_date),'0'),'%x%v%w'),'%d/%m/%Y'),
        DATE_FORMAT(STR_TO_DATE(CONCAT('01/',EXTRACT(MONTH FROM dispatch_date),'/',YEAR(dispatch_date)),'%d/%m/%Y'),'%d/%m/%Y')) as my_dispatch_date,

        resource.name as resourcename,
        resource.reference,
        SUM(supplier_codes.license_fee) as royalty_amount,
        tax_codes.code,
        ROUND(((SUM(supplier_codes.license_fee) / 100) * tax_codes.multiplier),2) as tax_amount,
        "1" as blank_field_2,
        "_" as blank_field_3,
        "_" as blank_field_4

        FROM order_line o

        LEFT JOIN resource ON o.resource_id = resource.resource_id
        LEFT JOIN supplier_codes ON resource.reference = supplier_codes.reference
        LEFT JOIN partners ON supplier_codes.partner_id = partners.partner_id
        LEFT JOIN partner_type ON supplier_codes.partner_type_id = partner_type.partner_type_id
        LEFT JOIN tax_codes ON partners.tax_code = tax_codes.id

        WHERE supplier_codes.payment_start_date <= dispatch_date
        AND supplier_codes.payment_end_date >= dispatch_date
        AND o.status = 1
        AND DATE_FORMAT(o.dispatch_date,'%Y-%m-%d') >= '$from'
        AND DATE_FORMAT(o.dispatch_date,'%Y-%m-%d') < '$to'
        AND o.dm !='ZX'

        GROUP BY reference, supplier_code, my_dispatch_date
        ORDER BY dispatch_date, reference

        dont worry about the difference in some of the vars in it, its only the group by thats doing my head in

        T 1 Reply Last reply
        0
        • T tim_gunning

          hi tim problem is...I dont want to do anything with them apart from get the values in the same way you do in MySQL, I just want to get rid of the duplicates that get pulled based on certain criteria. this is what I'm converting:

          SELECT

          'PI' as transaction_type,
          supplier_codes.supplier_code,
          supplier_codes.nominal_code,
          COUNT(o.orderline_id) as sale_volume,
          IF ( EXTRACT(MONTH FROM STR_TO_DATE(CONCAT(YEARWEEK(dispatch_date),'0'),'%x%v%w')) = MONTH(dispatch_date),
          DATE_FORMAT(STR_TO_DATE(CONCAT(YEARWEEK(dispatch_date),'0'),'%x%v%w'),'%d/%m/%Y'),
          DATE_FORMAT(STR_TO_DATE(CONCAT('01/',EXTRACT(MONTH FROM dispatch_date),'/',YEAR(dispatch_date)),'%d/%m/%Y'),'%d/%m/%Y')) as my_dispatch_date,

          resource.name as resourcename,
          resource.reference,
          SUM(supplier_codes.license_fee) as royalty_amount,
          tax_codes.code,
          ROUND(((SUM(supplier_codes.license_fee) / 100) * tax_codes.multiplier),2) as tax_amount,
          "1" as blank_field_2,
          "_" as blank_field_3,
          "_" as blank_field_4

          FROM order_line o

          LEFT JOIN resource ON o.resource_id = resource.resource_id
          LEFT JOIN supplier_codes ON resource.reference = supplier_codes.reference
          LEFT JOIN partners ON supplier_codes.partner_id = partners.partner_id
          LEFT JOIN partner_type ON supplier_codes.partner_type_id = partner_type.partner_type_id
          LEFT JOIN tax_codes ON partners.tax_code = tax_codes.id

          WHERE supplier_codes.payment_start_date <= dispatch_date
          AND supplier_codes.payment_end_date >= dispatch_date
          AND o.status = 1
          AND DATE_FORMAT(o.dispatch_date,'%Y-%m-%d') >= '$from'
          AND DATE_FORMAT(o.dispatch_date,'%Y-%m-%d') < '$to'
          AND o.dm !='ZX'

          GROUP BY reference, supplier_code, my_dispatch_date
          ORDER BY dispatch_date, reference

          dont worry about the difference in some of the vars in it, its only the group by thats doing my head in

          T Offline
          T Offline
          Tim Carmichael
          wrote on last edited by
          #4

          Ok.... in SQLServer, a group by is used if there are any aggregate fields (sum, count, etc). In your original query (from MySQL), there is a COUNT and 2 SUM aggregate fields. If you want the sales volumne (COUNT), royalty amount (SUM) and tax amount (SUM) in your SQLServer query, GROUP BY any non-aggregate fields: GROUP BY supplier_codes.supplier_code, supplier_codes.nominal_code, resource.name, resource.reference, tax_codes.code The GROUP BY clause must go after the WHERE and before the ORDER BY If you are currently getting duplicate records, change the SELECT to SELECT DISTINCT. Hope that helps... Tim

          T 1 Reply Last reply
          0
          • T Tim Carmichael

            Ok.... in SQLServer, a group by is used if there are any aggregate fields (sum, count, etc). In your original query (from MySQL), there is a COUNT and 2 SUM aggregate fields. If you want the sales volumne (COUNT), royalty amount (SUM) and tax amount (SUM) in your SQLServer query, GROUP BY any non-aggregate fields: GROUP BY supplier_codes.supplier_code, supplier_codes.nominal_code, resource.name, resource.reference, tax_codes.code The GROUP BY clause must go after the WHERE and before the ORDER BY If you are currently getting duplicate records, change the SELECT to SELECT DISTINCT. Hope that helps... Tim

            T Offline
            T Offline
            tim_gunning
            wrote on last edited by
            #5

            cheers tim that helped a lot,i've been looking at this (migration overall) for about a week now and i can't really see the trees for the woods anymore, my brains withered. ta tim

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups