How do I create a running sum for the count of the number of items associated with a value in a query? [duplicate] - ms-access

This question already has answers here:
SQL Server: create an incremental counter for records in the same year?
(3 answers)
Closed 1 year ago.
I am trying to use a query to create a Bill of Materials list that gives an item number to a material. I have a column for Material and BOMPart. The Material column gives a list of item numbers that repeat for each item in the Bill of Materials. The BOMPart column lists each part of the item in Material. So Material 1 is created using items a, b, and c. I would like the third column, ItemNo, to start at 1 for each Material and count each BOMPart associated to the material. It should then reset to 1 for the next Material. Any suggestions? I am still pretty new to Access. Here is what I would like the columns to look like, I replaced my actual material numbers for simplicity.
Material
BOMPart
ItemNo
1
a
1
1
b
2
1
c
3
2
a
1
2
f
2
3
g
1
3
h
2
3
i
3
4
k
1
4
m
2

You want to use row_number for this. I am not certain this works in Access.
select Material
, BOMPart
, Row_Number() over (partition by Material order by BOMPart)
from xxxx

Related

select highest value in mysql [duplicate]

This question already has answers here:
Select max value of each group
(8 answers)
How to select a maximum value row in mysql table
(7 answers)
Closed 4 years ago.
So I have a database that has some values like this:
Level , Indicator
4 1
3 2
4 3
3 4
4 5
3 6
What I want to do is to select the highest level value in every indicator.
Is there any sql query that I can use that will generate a result like this?
Level , Indicator
4 1
4 3
4 5
If not, can you help me out using php and mysqli? Thank you so much.
To get Indicators having just highest level value -
select distinct Indicator, level
from your_table
where level = (select max(level) from your_table)
Also, you can use group by to get highest level for each Indicator value -
select Indicator, max(Level) from your_table group by Indicator

Combine multi rows in access - 1 field

I have data in multiple rows, and I need to combine data in similar columns and separate with a semi colon, to end up with one row with grouped by ID.
I have
ID Type
1 A
1 B
1 C
2 D
3 A
3 F
I want results to be
1 A;B;C
2 D
3 A;F
I have limited knowledge of access, but know this should be basic and easy. I appreciate assistance.

mysql: specific item to be first and then to sort the rest chronologically to end then chronologically from beginning

Lets say I have the following table:
friends
_______
id name
1 johnny
2 tam
3 slick
4 mat
5 Rhanda
6 Tommy
7 ike
8 Spencer
9 Alan
I want to get all the friends list but I want the id 5 to be the first item in the list. I want the next item to follow chronologically until the end. Then the list start from the beginning until all results have been returned. So it should end like this...
friends
_______
id name
5 Rhanda
6 Tommy
7 ike
8 Spencer
9 Alan
1 johnny
2 tam
3 slick
4 mat
I have found and tried this but as you can imagine it's only returning the first item and then ordering the rest from 1 up. I have searched and worked on this for 2 days now and really could use some help. Any suggestions?
You can use:
order by (id >= 5) desc, id
This puts all ids 5 or greater first. Then it sorts each of the parts in ascending order.
MySQL treats boolean expressions as integers in an integer context, with 0 for false and 1 for true. So, to put the true values first, desc is needed.
You can add an expression to order clause like this:
select id, name from table
order by
case when id >= 5 then 0 else 1 end
, id

ClosedXML Outline

I am trying to create a Group in the exported Excel Workbook using OpenXML.
My source data table looks like this:
Row State Product Sales
1 NY A 100
2 NY A 200
3 NY B 300
4 CA A 100
5 CA A 200
6 CA B 300
I would like to create an outline by State and then Product with a subtotal on each group
I tried
ws.Outline.SummaryVLocation = XLOutlineSummaryVLocation.Top;
ws.Rows(1, 3).Group(); // Create an outline (level 2) for rows 1-4
ws.Rows(4, 6).Group();
But it's not giving me what I want, and I don't see an option to add the subtotals.
How can I achieve this?
The code example in the documentation which you use is either outdated or just wrong.
If you want to group rows 2 to 4 you need to use the code ws.Rows(3, 4).Group(); (see picture). This is consistent with Excel itself, there you have to select only rows 3 and 4 before clicking the group button to get the same result.
When you try to group rows 1 to 3 like in your code you group them all under row 0 which leads to errors since there is no row 0.
You can control this behaviour to some extend with the XLOutlineSummaryVLocation property. If you use Bottom instead of top you use the top two rows to group rows 2 to 4: ws.Rows(2, 3).Group();
With all this said two more points:
You need to use Excel row numbers not the numbers in your column "Row".
All this grouping and collapsing is only for display purposes. To sum up the sales numbers you have to use the subtotal functions in Excel (which I find rather confusing and unhelpful in this case) or add columns and results directly in C#.
Using this code should lead to your desired result (see picture below):
ws.Outline.SummaryVLocation = XLOutlineSummaryVLocation.Top;
ws.Cell(1, 5).SetValue("Product subtotals");
ws.Cell(1, 6).SetValue("State subtotals");
ws.Rows(3, 4).Group(); // group rows 2 to 4 (state NY), outline level 1
ws.Cell(2, 6).SetFormulaA1("=SUM(D2:D4)"); // subtotal for all NY sales
ws.Row(3).Group(); // group rows 2 and 3 (product A), outline level 2
ws.Cell(2, 5).SetFormulaA1("=SUM(D2:D3)"); // subtotal for all NY, product A sales
ws.Cell(4, 5).SetFormulaA1("=SUM(D4)"); // subtotal for all NY, product B sales
ws.Rows(6, 7).Group(); // group rows 5 to 7 (state CA), outline level 1
ws.Row(6).Group(); // group rows 5 and 6 (product A), outline level 2
ws.CollapseRows(2); // collapse group level 2 (products)

Group data on X-axis

I have a SharePoint list with 5 options (questions). Each option has a dropdown with values 1-6. The user (employee of a company) needs to select an option, then select a value from the dropdown and hit Submit. The selected value is unique. In other words, if the user selects the value 1 for the first option, that value cannot be chosen again. Here's an example form -
Category Rank
------------------------------
1. Work/Life Balance 4
2. Compensation 2
3. Commute 3
4. Work 1
5. Development 5
After filling the form, the data looks likes this on the Sharepoint list -
Employee Manager Work/Life Compensation Commute Work Development
--------------------------------------------------------------------------------
1. Employee 1 Manager 1 2 4 3 1 5
2. Employee 2 Manager 3 1 3 4 5 2
3. Employee 3 Manager 1 5 4 2 3 1
4. Employee 4 Manager 2 4 1 5 2 3
I'm able to get the Y-axis (for Rank) on the report just fine. The X-axis needs to display each category grouped by each Manager. Here's a sample of how I want it to look like -
Each colored bar on the X-axis is a Manager. This is my first time with SSRS (2012) and I'm just not sure how to accomplish this. If this is not possible, will moving the data to a SQL table in a different layout help? Any help is greatly appreciated.
You could aggregate each employee's response into an average in your dataset (I'm assuming you know how to do this):
Averages (just pretend)
Manager Work/Life Compensation Commute Work Development
--------------------------------------------------------------------------------
1. Manager1 2 4 3 1 5
2. Manager3 1 3 4 5 2
3. Manager2 4 1 5 2 3
Then you can use the categories as you have, with the manager as the series field. Pretty sure that should achieve what you're looking for.