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. Complicate SQL question...?? anyone know? urgent~~

Complicate SQL question...?? anyone know? urgent~~

Scheduled Pinned Locked Moved Database
questiondatabasesalestutorial
4 Posts 3 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.
  • C Offline
    C Offline
    campbells
    wrote on last edited by
    #1

    I not sure is there way to show percentage of my sales. I have done a SQL on showing the total sales and now i want to calculate the percentage of total for each department. Example TOTAL sales= $100 Vendor1 =$20 Vendor2 =$10 ........ how can correctly show another column that will calculate the percentage?? I have try many method but not working... i was thinking to write as sum(ti.totalsales)/sum(sum(it.totalsales))*100 as precentage is SQL able to support those complicated calculation? or i have to do it in vb... ---HEre is the basic show the total sales with out percentage... select d.deptdesc,v.[name],ti.deptcode ,sum(ti.totalsales) as Total from tot_item ti , dept d, item i, vendor v where ti.itemcode=i.itemcode and v.vendorid=i.vendorid and ti.deptcode=d.deptcode group by v.[name], ti.deptcode, d.deptdesc ORDER BY v.[name], ti.deptcode ASC

    M E 2 Replies Last reply
    0
    • C campbells

      I not sure is there way to show percentage of my sales. I have done a SQL on showing the total sales and now i want to calculate the percentage of total for each department. Example TOTAL sales= $100 Vendor1 =$20 Vendor2 =$10 ........ how can correctly show another column that will calculate the percentage?? I have try many method but not working... i was thinking to write as sum(ti.totalsales)/sum(sum(it.totalsales))*100 as precentage is SQL able to support those complicated calculation? or i have to do it in vb... ---HEre is the basic show the total sales with out percentage... select d.deptdesc,v.[name],ti.deptcode ,sum(ti.totalsales) as Total from tot_item ti , dept d, item i, vendor v where ti.itemcode=i.itemcode and v.vendorid=i.vendorid and ti.deptcode=d.deptcode group by v.[name], ti.deptcode, d.deptdesc ORDER BY v.[name], ti.deptcode ASC

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      The sample query you list has both Department and Vendor in it (you could get departments listed more than once if they use different vendors). You are trying to get a percentage of total for each department but your sample result set lists no percentages and values are listed by vendor. Are you sure you know what you really need? Given your 2nd line and assuming you are using SQL Server, first calculate a good total sales per department. SELECT d.deptdesc, SUM(ti.totalsales) AS DeptTotal FROM tot_item ti INNER JOIN dept d ON (ti.deptcode=d.deptcode) Now write a query that calculates total sales.SELECT SUM(ti.totalsales) AS FullTotal FROM tot_item ti Now put them together SELECT dep.deptdesc, (dep.DeptTotal / (SELECT SUM(ti.totalsales) FROM tot_item ti)) * 100 AS PercentOfSales FROM (SELECT d.deptdesc, SUM(ti.totalsales) AS DeptTotal FROM tot_item ti INNER JOIN dept d ON (ti.deptcode=d.deptcode)) AS dep ORDER BY deptdesc Note: I am using a more modern form of the JOIN syntax. There are issues with expressing the JOIN in the WHERE clause. You can make this a lot cleaner using variables in a stored proc.

      M 1 Reply Last reply
      0
      • M Michael Potter

        The sample query you list has both Department and Vendor in it (you could get departments listed more than once if they use different vendors). You are trying to get a percentage of total for each department but your sample result set lists no percentages and values are listed by vendor. Are you sure you know what you really need? Given your 2nd line and assuming you are using SQL Server, first calculate a good total sales per department. SELECT d.deptdesc, SUM(ti.totalsales) AS DeptTotal FROM tot_item ti INNER JOIN dept d ON (ti.deptcode=d.deptcode) Now write a query that calculates total sales.SELECT SUM(ti.totalsales) AS FullTotal FROM tot_item ti Now put them together SELECT dep.deptdesc, (dep.DeptTotal / (SELECT SUM(ti.totalsales) FROM tot_item ti)) * 100 AS PercentOfSales FROM (SELECT d.deptdesc, SUM(ti.totalsales) AS DeptTotal FROM tot_item ti INNER JOIN dept d ON (ti.deptcode=d.deptcode)) AS dep ORDER BY deptdesc Note: I am using a more modern form of the JOIN syntax. There are issues with expressing the JOIN in the WHERE clause. You can make this a lot cleaner using variables in a stored proc.

        M Offline
        M Offline
        Michael Potter
        wrote on last edited by
        #3

        Sorry - forgot the GROUP BY clause in the aggregate:

        SELECT     
            d.deptdesc,    
            SUM(ti.totalsales) AS DeptTotal 
        FROM     
            tot_item ti 
        INNER JOIN     
            dept d    
            ON (ti.deptcode=d.deptcode)
        GROUP BY
            deptdesc
        

        This will also need to be modified in the final query.

        1 Reply Last reply
        0
        • C campbells

          I not sure is there way to show percentage of my sales. I have done a SQL on showing the total sales and now i want to calculate the percentage of total for each department. Example TOTAL sales= $100 Vendor1 =$20 Vendor2 =$10 ........ how can correctly show another column that will calculate the percentage?? I have try many method but not working... i was thinking to write as sum(ti.totalsales)/sum(sum(it.totalsales))*100 as precentage is SQL able to support those complicated calculation? or i have to do it in vb... ---HEre is the basic show the total sales with out percentage... select d.deptdesc,v.[name],ti.deptcode ,sum(ti.totalsales) as Total from tot_item ti , dept d, item i, vendor v where ti.itemcode=i.itemcode and v.vendorid=i.vendorid and ti.deptcode=d.deptcode group by v.[name], ti.deptcode, d.deptdesc ORDER BY v.[name], ti.deptcode ASC

          E Offline
          E Offline
          ednrgc
          wrote on last edited by
          #4

          You can try something like this: DECLARE @TempTotal int SELECT @TempTotal=sum(ti.totalsales) from ti select d.deptdesc,v.[name],ti.deptcode ,(sum(ti.totalsales)/@TempTotal) as Total from tot_item ti, dept d, item i, vendor v where ti.itemcode=i.itemcode and v.vendorid=i.vendorid and ti.deptcode=d.deptcode group by v.[name], ti.deptcode, d.deptdesc ORDER BY v.[name], ti.deptcode ASC

          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