ms sql Query for POS item sales - sql

I have a SQL question regarding POS system item sales data. In the database, there is a "Totals" table. In that table, contains various records identified by the "TtlType" field. I'm working with 2 types. Values 11, which is the Item POS Sales, and 12, which is the Item Cost Sales.
When I do my SQL Query, i'm doing something along the lines of
SELECT ItemNumber,
SUM(ItemCount),
SUM(ItemAmount)
FROM Totals
WHERE TtlType = 11
AND Date = 8/1/2012
etc.
I'd like to also include TtlType 12. How can I merge all of the data into one row?
If I change my WHERE to include TtlType 11 & 12, I get 2 seperate rows for each menu item. The first one being TtlType 11 which is the Sell Price, and the next being 12 which is the cost. BOTH TtlType use the SAME field names to record information (ItemCount, ItemAmount)
I hope i explained this well enough. When I use the GROUP BY, I still get the same results.

I'm not sure I understand your data structure fully, or the significance of ItemNumber.
However, you don't need a GROUP BY to do SUM as you already know, but including ItemNumber in your SELECT & GROUP BY will definitely cause you to have duplicate rows as it will be grouped on that column.
You shouldn't have any issues with the two SUM functions with just
SELECT SUM(ItemCount), SUM(ItemAmount) FROM Totals WHERE TtlType IN (11,12)
AND Date = 8/1/2012

Related

Join multiple tables in Microsoft SQL Server where there is only one line match from table 1 and multiple lines from table 2 and 3

I am stuck on something, which I have never used in my 10 years of SQL. I thought it would be useful if there was someway of doing this. Firstly I am running SQL Server Express (latest free version) on Windows. To talk to the database I am using SSMS.
There are three tables/queries.
1 table (A) has one data value I want to pull through.
2 tables (B)/(C) have multiple values.
Column common to all tables is CAMPAIGN NAME
Column common to (B)/(C) is PRODUCT NAME
This is an example of the data:
OUTPUT GOAL
I have tried the following:
UNION ALL (but this does not assist when I want to calculate AMOUNT - MARKETING - TOTAL INVESTMENT
I tried PARTITION (but I simple could now get it to work.
If I use joins, it brings through a head count / total investment and marketing cost per product, which when using SUM brings through the incorrect values for head count / total investment and marketing cost vs total amount, quantity.
I tried splitting the costs based on Quantity / Total Quantity or Amount / Total Amount, but the cost associated with the product is not correct or directly relating to the product this way.
Am I trying to do something impossible, or is there a way to do this in SQL?
The following comes pretty close to what you want:
select . . . -- select the columns you want here
from a join
b
on b.campaign_name = a.campaign_name join
c
on c.campaign_name = b.campaign_name and
c.product_name = b.product_name;
This produces a result set with a separate row for each campaign/product.

TOTAL vs Aggr in QlikView

I'm trying to understand how TOTAL and Aggr work in QlikView. Could someone please explain the difference between the two examples below, and if possible please illustrate with a SQL query?
Example1:
Max({<Field1=>} Aggr(Sum({<Field2={'Value'}, Field1=>} StuffCount), Field1))
Example2:
Max({<Field1=>} TOTAL Aggr(Sum({<Field2={'Value'}, Field1=>} StuffCount), Field1))
Not sure what you mean with and SQL query in this example. Anyway, imagine you have this list of Customers (CustomerID) and Sales (Sales):
CustomerID/ Sales
Customer1 25
Customer2 20
Customer1 10
Customer1 5
Customer1 20
Customer3 30
Customer2 30
Then you want to show it on a pivot table with dimension CustomerID and two expressions:
Max(Aggr(Sum(Sales), CustomerID)) // this will show 60 for the first customer, 50 for the second and 30 for the third one
Max(TOTAL Aggr(Sum(Sales),CustomerID)) //this will show 60 in every row of your table (which is the maximum sum of sales among all customers)
So basically AGGR creates a temporal list of whatever you put in the first function input (in this case sum(Sales)) using the dimension of the second (CustomerID). Then you can perform operations on that list (such as Max, Min, Avg...). If you write TOTAL and use the expression in a pivot table, then you 'ignore' the dimensions that might be affecting the operations.
Hope it helps
TOTAL keyword is useful in charts/pivot tables. It applies the same calculation on every datapoint in the chart/pivot, with independence of dimentions.
Therefore - if you put your expression into pivot table - 1st option may display different values per cell (if the Aggr is rellevant) when the 2nd will result in same values.
Aggr function allows making double aggregations (avg of sum, max of count etc..) on different group by bases.

counting and numbering in a select statement in Access SQL

Could you please help me figuring out how to accomplish the following.
I have a table containing the number of products available between one date and another as per below:
TABLE MyProducts
DateProduct ProductId Quantity Price
26/02/2016 7 2 100
27/02/2016 7 3 100
28/02/2016 7 4 100
I have created a form where users need to select a date range and the number of products they are looking for (in my example, the number of products is going to be 1).
In this example, let's say that a user makes the following selection:
SELECT SUM(MyProducts.Price) As TotalPrice
FROM MyProducts WHERE MyProducts.DateProduct
Between #2/26/2016# And #2/29/2016#-1 AND MyProducts.Quantity>=1
Now the user can see the total amount that 1 product costs: 300
For this date range, however, I want to allow users to select from a combobox also the number of products that they can still buy: if you give a look at the Quantity for this date rate, a user can only buy a maximum of 2 products because 2 is the lowest quantity available is in common for all the dates listed in the query.
First question: how can I feed the combobox with a "1 to 2" list (in this case) considering that 2 is lowest quantity available in common for all the dates queried by this user?
Second question: how can I manage the products that a user has purchased.
Let's say that a user has purchased 1 product within this date range and a second user has purchased for the very same date range the same quantity too (which is 1) for a total of 2 products purchased already in this date range. How can I see that for this date rate and giving this case the number of products actually available are:
DateProduct ProductId Quantity Price
26/02/2016 7 0 100
27/02/2016 7 1 100
28/02/2016 7 2 100
Thank you in advance and please let me know should you need further information.
You could create a table with an integer field counting from 1 to whatever max qty you could expect. Then create a query that will only return rows from your new table up to the min() qty in the MyProducts table. Use that query as the control source of your combobox.
EDIT: You will actually need two queries. The first should be:
SELECT Min(MyProducts.Quantity) AS MinQty FROM MyProducts;
which I called "qryMinimumProductQty". I create the table called "Numbering" with a single integer field called "Sequence". The second query:
SELECT Numbering.Sequence FROM Numbering, qryMinimumProductQty WHERE Numbering.Sequence<=qryMinimumProductQty.MinQty;
AFAIK there is no Access function/feature that will fill in a series of numbers in a combobox control source. You have to build the control source yourself. (Anyone with more VBA experience might have a solution to solve this, but I do not.)
It makes me ache thinking of an entire table with a single integer column only being used for a combobox though. A simpler approach to the combobox would just to show the qty available in a control on your form, give an unbound text box for the user to enter their order qty, and add a validation rule to stop the order and notify them if they have chosen a number greater than the qty on hand. (Just a thought)
As for your second question, I don't really understand what you're looking for either. It sounds like there may be another table of purchases? It should be a simple query to relate MyProducts to Purchases and take the difference between your MyProducts!qty and the Purchases!qty. If you don't have a table to store Purchases, it might be warranted based on my cursory understanding of your system.

Cognos - Conditionally hide a column, and summarise the remaining columns

I have a report with a static choice on the prompt page. The user can choose 'Full Detail', or 'Summarised'.
For a simplified example, say my report has these columns: Customer, Product, Date, Quantity, Value.
I would like to be able to show/hide the Date column based on the detail level choice, and have the Quantity and Value columns aggregate into a single Customer/Product line. I know how to show/hide the column (tying the choice variable to the column's Render Variable), but this does not do the aggregation, only makes the column invisible.
I have thought about doing a separate report page for Full Detail and Summary, but in my actual report I have a second choice box with which the user can choose a field to summarise by (e.g. Customer or Product), and the report will section-group by that field. At the moment I am doing that one per page (5 of them). Doing the detail choice the same way would mean I would need 10 pages. There is surely a better way.
Full detail:
Customer Product Date Qty Value
ABCD Things 22/10/2014 10 1.00
21/10/2014 40 4.00
23/10/2014 50 5.00
Summarised (How it looks at the moment, after hiding the Date column):
Customer Product Qty Value
ABCD Things 10 1.00
40 4.00
50 5.00
Summarised (How I would like it to look):
Customer Product Qty Value
ABCD Things 100 10.00
I am using Cognos Report Studio 10.1.1
You should not just hide column.
You should also set same value for this column in all rows
Instead of just [Date] in this column set
if (?HideDate? = 1) then ('') else ([Date])
or, if you prefer CASE
case ?HideDate? when 1 then '' else [Date] end
replace ?HideDate? = 1 with you own condition
Alexey's answer is great if you are using the standard Cognos 'auto-group and summarize' functionality.
If you have a custom aggregate that includes the [Date] column in its definition, you might squeeze a bit of performance gain out of modifying the aggregate function itself to disregard the [Date] column when a summarized total is desired.
If your aggregate function was:
total([Value] for [Customer],[Product],[Date])
..you might change this to a CASE statement like so:
CASE ?HideDate?
WHEN 1 then total([Value] for [Customer],[Product])
ELSE total([Value] for [Customer],[Product],[Date])
END
The data items after a 'for' clause usually end up in a GROUP BY clause in the resultant SQL. Limiting the items grouped, when possible, can help performance. In this case the performance improvement would likely be slight since there will only be one distinct value in Alexey's solution, but it's something to consider.

Microsoft Access - Group By Feature

I'm wondering if there is a way to modify the results of one of my queries. Right now, I have a query in which the output shows the number of items in certain type of category by date, by using the Group By feature. So just as an example, if I enter 9/15/13 as the date the output would be as follows:
Apples 1
Bannas 5
Pears 16
Is there a way for it to just show Apples and total all other items into one category so it would output:
Apples 1
All Others 21
Thanks in advance for all of your help!
A bit difficult without table names or column names, but something along the lines of this (you'll have to enter it in SQL view)
Select
IIf([Product] = 'Apples', 'Apples', 'All Others'),
Count(*)
From
[Inventory]
Where
[InventoryDate] = [Enter Date]
Group By
IIf([Product] = 'Apples', 'Apples', 'All Others')
You need to replace Inventory with your table name, and Product with the column that contains the category and InventoryDate with the column that has the date. Count(*) might be sum([Quantity]), it depends on your structure.