[Solved] Adding and displaying monthly sales for salesmen
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm
[Solved] Adding and displaying monthly sales for salesmen
I have the simple query as shown in my screenshot query1. There are 2 salesmen and the query lists the total of each individual order. Salesman one has a total of $11,035 and Salesman 2 has a total of $43,700. How do I get the query to show those two totals instead of individual totals? I tried using the sum function and got the error shown in query2. Every month I want to run a query of how much the salesman has sold so I can calculate their commission.
PO invoice is the main form and PO ITEMS is my sub form if that matters at all.
PO invoice is the main form and PO ITEMS is my sub form if that matters at all.
- Attachments
Last edited by dazedandconfused on Wed Oct 02, 2013 9:30 pm, edited 2 times in total.
Open office 4.0, win7
Re: Help adding and displaying monthly sales for salesmen
The query in SQL to get total sales for each person should be
You can also limit that to a particular month by adding a WHERE clause, though I'm not sure which column you want to use for the date.
Code: Select all
SELECT "SALESMAN", SUM("Order Price") FROM "PO ITEMS" GROUP BY "SALESMAN"
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Help adding and displaying monthly sales for salesmen
Since, this is an AGGREGATE query, each VISIBLE column ( field ) in the output query, . . . must contain a FUNCTION .
In your case, you want the SUM of the column "Order Price" . . . for each UNIQUE "SALESMAN" . Therefore, the FUNCTION to use for column "SALESMAN" is . . . Group .
Explanation: The above, in words means, to bring back data from the database, and, for each unique ( group ) of "SALESMAN" . . . calculate the SUM from the column "Order Price". You will probably want to include, on the ALIAS line for "Order Price" . . . something meaningful . . . as the column header output. Perhaps, Total Order Price . . . or . . . whatever you want.
I hope this helps, please be sure to let me / us know.
Sliderule
Thanks to add [Solved] in your 1st post Subject (edit button top right) if this isue has been resolved.
In your case, you want the SUM of the column "Order Price" . . . for each UNIQUE "SALESMAN" . Therefore, the FUNCTION to use for column "SALESMAN" is . . . Group .
Explanation: The above, in words means, to bring back data from the database, and, for each unique ( group ) of "SALESMAN" . . . calculate the SUM from the column "Order Price". You will probably want to include, on the ALIAS line for "Order Price" . . . something meaningful . . . as the column header output. Perhaps, Total Order Price . . . or . . . whatever you want.

I hope this helps, please be sure to let me / us know.
Sliderule
Thanks to add [Solved] in your 1st post Subject (edit button top right) if this isue has been resolved.
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm
Re: Help adding and displaying monthly sales for salesmen
Thank you very much. This is exactly what I was wondering and it worked, however it doesn't display in dollars even though the column in my table displays in dollars. Now that you mention it I would like to run the report at the beginning of the month to show sales only for the prior month. How would I do that?
Open office 4.0, win7
Re: Help adding and displaying monthly sales for salesmen
You said / asked:
Sliderule
Can you please post here the SQL ( Structured Query Language ) . . . that is . . . the SELECT statement . . . that you are using . . . AND . . . please include in your post . . . the TABLE and FIELD NAME that contains the DATE column.dazedandconfused wrote:This is exactly what I was wondering and it worked, however it doesn't display in dollars even though the column in my table displays in dollars. Now that you mention it I would like to run the report at the beginning of the month to show sales only for the prior month. How would I do that?
Sliderule
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm
Re: Help adding and displaying monthly sales for salesmen
Sliderule wrote:You said / asked:
Can you please post here the SQL ( Structured Query Language ) . . . that is . . . the SELECT statement . . . that you are using . . . AND . . . please include in your post . . . the TABLE and FIELD NAME that contains the DATE column.dazedandconfused wrote:This is exactly what I was wondering and it worked, however it doesn't display in dollars even though the column in my table displays in dollars. Now that you mention it I would like to run the report at the beginning of the month to show sales only for the prior month. How would I do that?
Sliderule
I'm using the one FJCC gave me,
Code: Select all
SELECT "SALESMAN", SUM("Order Price") FROM "PO ITEMS" GROUP BY "SALESMAN"
I had some sensitive info, so I made a copy of the database and included it here
- Attachments
-
- Copy of database.odb
- (101.76 KiB) Downloaded 304 times
Open office 4.0, win7
Re: Help adding and displaying monthly sales for salesmen
- Please try this Query . . . and . . . it will prompt user to input two numbers:
- A month number ( 1 to 12 ) . . . for example . . . 9
- A year number as a 4 digit number . . . for example . . . 2013
Code: Select all
SELECT "PO ITEMS"."SALESMAN", TO_CHAR("PO INVOICE"."Date",'MM-YYYY') as "Month", SUM("PO ITEMS"."Order Price") AS "Total Order Price" FROM "PO ITEMS", "PO INVOICE" WHERE "PO INVOICE"."ID" = "PO ITEMS"."FK_ID_PO" AND MONTH("PO INVOICE"."Date") = :Enter_Month_Number AND YEAR("PO INVOICE"."Date") = :Enter_Year_Number_as_YYYY GROUP BY "PO ITEMS"."SALESMAN", TO_CHAR("PO INVOICE"."Date",'MM-YYYY')
- Alternate Query, that will always return data ONLY from the prior month ( prior month from CURRENT_DATE ) with NO required input ( entries ) from user:
Code: Select all
SELECT "PO ITEMS"."SALESMAN", TO_CHAR( "PO INVOICE"."Date", 'MM-YYYY' ) AS "Month", SUM( "PO ITEMS"."Order Price" ) AS "Total Order Price" FROM "PO ITEMS", "PO INVOICE" WHERE "PO INVOICE"."ID" = "PO ITEMS"."FK_ID_PO" AND "PO INVOICE"."Date" >= CASE WHEN MONTH( CURRENT_DATE ) = 1 THEN ( CAST( YEAR( CURRENT_DATE ) - 1 || '-12-01' AS DATE ) ) ELSE CAST( YEAR( CURRENT_DATE ) || '-' || RIGHT( '0' || MONTH( CURRENT_DATE ) - 1, 2 ) || '-01' AS DATE ) END AND "PO INVOICE"."Date" < CAST( TO_CHAR( CURRENT_DATE, 'YYYY-MM-01' ) AS DATE ) GROUP BY "PO ITEMS"."SALESMAN", TO_CHAR( "PO INVOICE"."Date", 'MM-YYYY' )
I hope this helps, please be sure to let me / us know.
Sliderule
Thanks to add [Solved] in your 1st post Subject (edit button top right) if this issue has been resolved.
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm
Re: Help adding and displaying monthly sales for salesmen
That's outstanding. Thank you very much. Is there a way to change it so it shows the salesman name instead of their number?
One other thing not related to a query...I tried entering data for Donald Duck just to double check since he had no sales...poor Donald. I had a problem where it's not saving the last entry very well. I enter 2 product types and for the last one I entered $200 for order price then used the navigator to move to the next record. When I came back to the one I just entered I noticed that the $200 wasn't saved however product and type and salesman were. However, if I tab 4 times to the next row or if I click on the next row below it's saved. How can I change it to save after I hit the enter key or if I tab to a new cell? Is that possible or is this just how open office operates?
Just to test it I decided to enter text for quantity, description, company, date promised, original quote price and order price in that order. The only cells that were saved when I moved to the next record and came back were product type and salesman. Odd.
Next I entered data for product type, quantity, description, company, date promised, order price and then orig quote price in that order. I moved to the next record and came back and everything was saved except orig. quote price.
One other thing not related to a query...I tried entering data for Donald Duck just to double check since he had no sales...poor Donald. I had a problem where it's not saving the last entry very well. I enter 2 product types and for the last one I entered $200 for order price then used the navigator to move to the next record. When I came back to the one I just entered I noticed that the $200 wasn't saved however product and type and salesman were. However, if I tab 4 times to the next row or if I click on the next row below it's saved. How can I change it to save after I hit the enter key or if I tab to a new cell? Is that possible or is this just how open office operates?
Just to test it I decided to enter text for quantity, description, company, date promised, original quote price and order price in that order. The only cells that were saved when I moved to the next record and came back were product type and salesman. Odd.
Next I entered data for product type, quantity, description, company, date promised, order price and then orig quote price in that order. I moved to the next record and came back and everything was saved except orig. quote price.
Open office 4.0, win7
Re: Help adding and displaying monthly sales for salesmen
I do NOT understand the 'relationship' you want / need . . . since . . . in the table "SALESMAN" . . . the Primary Key is "ID" . . . an INTEGER . . . but . . . in your other tables . . . for example . . . "PO INVOICE" and "PO ITEMS" . . . the column "Salesman" are VARCHAR . . . NOT, also INTEGER . . . therefore . . . you canNOT relate them.Is there a way to change it so it shows the salesman name instead of their number?
I suspect . . . after you change either / both of those tables / column to an INTEGER, with the sample data . . . it should work as you want.
I strongly recommend you perform the above . . . but . . . as a TEST ( sample ) I coded the following Query . . . changing . . . in the CODE . . . the value of "PO ITEMS"."SALESMAN" to an INTEGER ( using the COALESCE function ) . . . and . . . when it ( in the table ) is NULL ( undefined ) defaulted it to an integer of 1.
Code: Select all
SELECT
"SALESMAN"."Salesman",
TO_CHAR( "PO INVOICE"."Date", 'MM-YYYY' ) AS "Month",
SUM( "PO ITEMS"."Order Price" ) AS "Total Order Price"
FROM "PO ITEMS",
"PO INVOICE",
"SALESMAN"
WHERE CAST(COALESCE("PO ITEMS"."SALESMAN",1) as INTEGER) = "SALESMAN"."ID"
AND "PO INVOICE"."ID" = "PO ITEMS"."FK_ID_PO"
AND "PO INVOICE"."Date" >= CASE WHEN MONTH( CURRENT_DATE ) = 1 THEN ( CAST( YEAR( CURRENT_DATE ) - 1 || '-12-01' AS DATE ) ) ELSE CAST( YEAR( CURRENT_DATE ) || '-' || RIGHT( '0' || MONTH( CURRENT_DATE ) - 1, 2 ) || '-01' AS DATE ) END
AND "PO INVOICE"."Date" < CAST( TO_CHAR( CURRENT_DATE, 'YYYY-MM-01' ) AS DATE )
GROUP BY "SALESMAN"."Salesman",
TO_CHAR( "PO INVOICE"."Date", 'MM-YYYY' )
Code: Select all
SELECT
"SALESMAN"."Salesman",
TO_CHAR( "PO INVOICE"."Date", 'MM-YYYY' ) AS "Month",
SUM( "PO ITEMS"."Order Price" ) AS "Total Order Price"
FROM "PO ITEMS",
"PO INVOICE",
"SALESMAN"
WHERE CAST(COALESCE("PO ITEMS"."SALESMAN",1) as INTEGER) = "SALESMAN"."ID"
AND "PO INVOICE"."ID" = "PO ITEMS"."FK_ID_PO"
AND MONTH( "PO INVOICE"."Date" ) = :Enter_Month_Number
AND YEAR( "PO INVOICE"."Date" ) = :Enter_Year_Number_as_YYYY
GROUP BY "SALESMAN"."Salesman",
TO_CHAR( "PO INVOICE"."Date", 'MM-YYYY' )

I hope this helps, please be sure to let me / us know.
Sliderule
Thanks to add [Solved] in your 1st post Subject (edit button top right) if this issue has been resolved.
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm
Re: Help adding and displaying monthly sales for salesmen-SO
OK. Thanks. I'll take a look at it again tomorrow.
Open office 4.0, win7
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm
Re: Help adding and displaying monthly sales for salesmen
All my primary keys are INTEGER'S. Salesman is a VARCHAR in all tables. I don't understand why I'd change it to an integer? I wouldn't be able to enter their name that way would I?Sliderule wrote:I do NOT understand the 'relationship' you want / need . . . since . . . in the table "SALESMAN" . . . the Primary Key is "ID" . . . an INTEGER . . . but . . . in your other tables . . . for example . . . "PO INVOICE" and "PO ITEMS" . . . the column "Salesman" are VARCHAR . . . NOT, also INTEGER . . . therefore . . . you canNOT relate them.Is there a way to change it so it shows the salesman name instead of their number?
I suspect . . . after you change either / both of those tables / column to an INTEGER, with the sample data . . . it should work as you want.
I strongly recommend you perform the above . . . but . . . as a TEST ( sample ) I coded the following Query . . . changing . . . in the CODE . . . the value of "PO ITEMS"."SALESMAN" to an INTEGER ( using the COALESCE function ) . . . and . . . when it ( in the table ) is NULL ( undefined ) defaulted it to an integer of 1.
Prompt for Month Number ( 1 to 12 ), and Year Number ( as 4 digit number ):Code: Select all
SELECT "SALESMAN"."Salesman", TO_CHAR( "PO INVOICE"."Date", 'MM-YYYY' ) AS "Month", SUM( "PO ITEMS"."Order Price" ) AS "Total Order Price" FROM "PO ITEMS", "PO INVOICE", "SALESMAN" WHERE CAST(COALESCE("PO ITEMS"."SALESMAN",1) as INTEGER) = "SALESMAN"."ID" AND "PO INVOICE"."ID" = "PO ITEMS"."FK_ID_PO" AND "PO INVOICE"."Date" >= CASE WHEN MONTH( CURRENT_DATE ) = 1 THEN ( CAST( YEAR( CURRENT_DATE ) - 1 || '-12-01' AS DATE ) ) ELSE CAST( YEAR( CURRENT_DATE ) || '-' || RIGHT( '0' || MONTH( CURRENT_DATE ) - 1, 2 ) || '-01' AS DATE ) END AND "PO INVOICE"."Date" < CAST( TO_CHAR( CURRENT_DATE, 'YYYY-MM-01' ) AS DATE ) GROUP BY "SALESMAN"."Salesman", TO_CHAR( "PO INVOICE"."Date", 'MM-YYYY' )
I do not understand your other question / comment . . . since I do NOT know where / how you entered the data. Also . . . it has nothing to do with this original post . . . so . . . if necessary . . . please start another forum topic.Code: Select all
SELECT "SALESMAN"."Salesman", TO_CHAR( "PO INVOICE"."Date", 'MM-YYYY' ) AS "Month", SUM( "PO ITEMS"."Order Price" ) AS "Total Order Price" FROM "PO ITEMS", "PO INVOICE", "SALESMAN" WHERE CAST(COALESCE("PO ITEMS"."SALESMAN",1) as INTEGER) = "SALESMAN"."ID" AND "PO INVOICE"."ID" = "PO ITEMS"."FK_ID_PO" AND MONTH( "PO INVOICE"."Date" ) = :Enter_Month_Number AND YEAR( "PO INVOICE"."Date" ) = :Enter_Year_Number_as_YYYY GROUP BY "SALESMAN"."Salesman", TO_CHAR( "PO INVOICE"."Date", 'MM-YYYY' )
![]()
I hope this helps, please be sure to let me / us know.
Sliderule
Thanks to add [Solved] in your 1st post Subject (edit button top right) if this issue has been resolved.
Open office 4.0, win7
Re: Adding and displaying monthly sales for salesmen
You said / asked:
I thought, you were developing a Relational Database . . . where . . . you do NOT duplicate data. That is why, if you RELATE the "Salesman" ( or is it "SALESMAN" . . . I mean . . . you are consistent in the name . . . that is . . . consistently inconsistent . . . sometimes "Salesman", somtimes "SALESMAN" ) . . . to the ID of the appropriate table . . . you only enter the NAME ( full text name ) once, and, if / when a name changes . . . only ONE change in the appropriate table is needed. And, this way, if you misspelled . . . for example . . . 'DONALD DUC' rather than 'DONALD DUCK' . . . the problem would never occur.
This is all part of designing a Normalized Relational Database.
Bottom line, creating a well designed database, is just like, constructing a building , , , a large skyscraper. If it, the building, is NOT set on a firm foundation, the building will collapse. Likewise, if your database is not constructed as a normalized database, it will either collapse, or, cause ridiculous permutations to get what you want from it.
Sliderule
Thanks to add [Solved] in your 1st post Subject (edit button top right) if this issue has been resolved.
A Primary Key does NOT have to be an INTEGER, that is NOT a requirement.dazedandconfused wrote:All my primary keys are INTEGER'S. Salesman is a VARCHAR in all tables. I don't understand why I'd change it to an integer? I wouldn't be able to enter their name that way would I?
I thought, you were developing a Relational Database . . . where . . . you do NOT duplicate data. That is why, if you RELATE the "Salesman" ( or is it "SALESMAN" . . . I mean . . . you are consistent in the name . . . that is . . . consistently inconsistent . . . sometimes "Salesman", somtimes "SALESMAN" ) . . . to the ID of the appropriate table . . . you only enter the NAME ( full text name ) once, and, if / when a name changes . . . only ONE change in the appropriate table is needed. And, this way, if you misspelled . . . for example . . . 'DONALD DUC' rather than 'DONALD DUCK' . . . the problem would never occur.
This is all part of designing a Normalized Relational Database.
Bottom line, creating a well designed database, is just like, constructing a building , , , a large skyscraper. If it, the building, is NOT set on a firm foundation, the building will collapse. Likewise, if your database is not constructed as a normalized database, it will either collapse, or, cause ridiculous permutations to get what you want from it.
Sliderule
Thanks to add [Solved] in your 1st post Subject (edit button top right) if this issue has been resolved.
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm
Re: [Solved] Adding and displaying monthly sales for salesme
Thanks. I can't figure out how to have it display USD instead of a number. Is there a way to do that?
Open office 4.0, win7
Re: [Solved] Adding and displaying monthly sales for salesme
USD . . . ¿ what is that ?dazedandconfused wrote:Thanks. I can't figure out how to have it display USD instead of a number. Is there a way to do that?
The way any Number ( prefix, thousands separator, number of decimal places, choice of character representing a decimal place, etc ), Date, Time, or Boolean field is displayed ( shows up on a screen ) depends on how you FORMAT the output data ( column ), whether a Result Set from a Query, a Form, or, a Report.
Sliderule
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm
Re: [Solved] Adding and displaying monthly sales for salesme
US dollars. How do I set the format of us dollars?
Open office 4.0, win7
Re: [Solved] Adding and displaying monthly sales for salesme
You have NOT told me what your output is . . . a Query, a Form, a Report, other?
Sliderule
Sliderule
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm
Re: [Solved] Adding and displaying monthly sales for salesme
My apologies. When I run a query or a report I would like the column "Total Order Price" to be shown in US dollars instead of as a numberSliderule wrote:You have NOT told me what your output is . . . a Query, a Form, a Report, other?
Sliderule
Open office 4.0, win7
Re: [Solved] Adding and displaying monthly sales for salesme
Sliderule wrote:The way any Number ( prefix, thousands separator, number of decimal places, choice of character representing a decimal place, etc ), Date, Time, or Boolean field is displayed ( shows up on a screen ) depends on how you FORMAT the output data ( column ), whether a Result Set from a Query, a Form, or, a Report.
- So, if it is a Query, after running the Query and you have a Result Set, format the column "Total Order Price" the way you want. For instance:
- Right click on the output column, "Total Order Price"
- From the pop-up, choose: Column Format...
- Click on the Format tab button
- Under Category, click on Currency
- Make any additional modifications, if needed
- If you desire to change the Alignment, Click on the Alignment tab button
- Make any modifications you desire
- Press the OK button
- For a report, format the field in the report as you desire.
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm
Re: [Solved] Adding and displaying monthly sales for salesme
Thank you. What I want to do is have it set to us dollars by default every time I run the query or when I run the report. Is that possible?
Open office 4.0, win7
Re: [Solved] Adding and displaying monthly sales for salesme
With the current OpenOffice / LibreOffice Base front end, the format output of a Query must be made each time the Query is run.dazedandconfused wrote: What I want to do is have it set to us dollars by default every time I run the query or when I run the report. Is that possible?
However, when displaying a Table, or a View, the format of the fields are stored as a part of the database front-end in the OpenOffice / LibreOffice Base file ( *.odb ) . . . therefore . . . you can create a View of the Query, save the format, and merely display that View.
This is the last time I will repeat myself, and, just to make sure you can see it . . . I will display ( format the output ) it with big letters:
SlideruleSliderule wrote:
For a report, format the field in the report as you desire.
-
- Posts: 73
- Joined: Tue Sep 03, 2013 8:38 pm