I've got a list of items and two locations (X and Y) that these items can be in.these two locations have these items in different quantities.
So When someone places an order for a few items, the items can be pulled from either of these two locations.
Below is the 'Orders' table I've created but it shows two columns for two locations and available stock.
ItemNumber Location Stock X Stock Y
A X 12 32
B X 10 54
C X 5 23
A Y 54 30
C Y 65 36
D Y 76 23
E X 12 31
F X 32 19
F Y 72 40
What I want to see is available stock for the requested location in a column, not both locations and stock availability in two columns as I've done above.
Result table I Want to see is,
ItemNumber Location Avail Stock
A X 12
B X 10
C X 5
A Y 30
C Y 36
D Y 23
E X 12
F X 32
F Y 40
I just cant get my head around this to do it. great if anyone could help or tell me if its even possible.
Thanks
You can use a CASE WHEN expression:
SELECT ItemNumber,
Location,
CASE WHEN Location = 'X' THEN [Stock X]
WHEN Location = 'Y' THEN [Stock Y]
END Avail_Stock
FROM Orders
You have the union tag, so:
SELECT ItemNumber,
Location,
[Stock X] AS Avail_Stock
FROM Orders
WHERE Location = 'X'
UNION
SELECT ItemNumber,
Location,
[Stock Y] AS Avail_Stock
FROM Orders
WHERE Location = 'Y'
Related
I have a df with 5 columns:
What i'm trying to do is mark value of first customer interaction after talking to human in every specific group.
Hopefully the outcome would be like this:
What I have tried is shifting type column to put previous row in front of type to check if its customer and prev row is human. However, I can't figure out a grouping option to get min index for each group for each occurrence.
This works:
k = pd.DataFrame(df.groupby('group').apply(lambda g: (g['type'].eq('customer') & g['type'].shift(1).eq('human')).pipe(lambda x: [x.idxmax(), x[::-1].idxmax()])).tolist())
df['First'] = ''
df['Last'] = ''
df.loc[k[1], 'First'] = 'F'
df.loc[k[1], 'Last'] = 'L'
Output:
>>> df
group type First Last
0 x bot
1 x customer
2 x bot
3 x customer
4 x human
5 x customer F
6 x human
7 x customer L
8 y bot
9 y customer
10 y bot
11 y customer
12 y human
13 y customer F
14 y human
15 y customer L
16 z bot
17 z customer
18 z bot
19 z customer
20 z human
21 z customer F
22 z human
23 z customer L
24 z customer
25 z customer
In Pentaho's PRD, I am working with an object datasource (i.e. I do not have a SQL query I may edit to group the data). To realize the required report, I must group the data within the PRD (OK) and only show these grouped values (OK). How can I sum the group values in the group headers to generate totals (MY PROBLEM) when there are multiple records per group? Here is a simplified example:
Assume the dataset I provide to the PRD is:
X 42 1
X 42 2
X 42 3
Y 10 12
Y 10 7
Z 8 22
Z 8 92
So, I need to display groups based upon column 1 and 2 only.
Column 3 is excluded; but, I can't remove it from the dataset.
Then, I must provide a total for the 2nd column, as follows:
X 42
Y 10
Z 8
---------
Total 60
I am trying to build a query to create a basket analysis of products based on their type grouping. They have two levels of grouping beyond the product ids.
Dept level
1
2
3
4
and buying group level
MA
M
MC
WA
W
WC
KA
KC
K
the hierarchy is
1 > W, WC
2 > M, MC
3 > K, KC
4 > MA, KA, WA
right now the query I have
Select
i.buying_group,
Sum(d.sales),
Sum(d.units),
count(distinct d.trans_nbr) transaction_count
From
sales_details d, item_data i, (select trans_nbr from sales_details where item_dept = 1 group by trans_nbr) main_group
Where
d.trans_nbr = main_group.trans_nbr
d.item_nbr = i.item_nbr
group by i.buying_group;
Right now I get the data that I need for most of the buying groups but because this is being run at the dept level it does not give me the correct basket information for W and WC. Is there a way to do this at the dept level that would show if the customer bought something from either of these groups and had the other in their basket without double counting it?
the results at the moment are something like this
buyyin_group Sum(sales) Sum(units) transaction_count
MA 100 5 4
M 75 3 3
MC 56 1 1
WA 48 3 2
W 250 6 6
WC 200 9 9
KA 164 7 5
KC 400 12 7
K 521 14 12 `
i have a table which has localities numbered with unique numbers. Each locality has some buildings numbered that have the status as Activated = Y or N. i want to pick localities which have the min Building Activated = 'Y' count of 15.
Sample Data :
Locality ACTIVATED
1 Y
1 Y
1 N
1 N
1 N
1 N
2 Y
2 Y
2 Y
2 Y
2 Y
Eg : i need count of locality that with min. 5 Y in ACTIVATED Column
SELECT l.*
FROM Localities l
WHERE (SELECT COUNT(*) FROM Building b
WHERE b.LocalityNumber = l.LocalityNumber
AND b.Activated = 'Y') >= 15
I need a sql solution for this problem I am dealing with:
I have the following rows in a table
cod coda pricea priceb pricec
x1 y 20 50
x2 y 20 50
x3 y 60
x4 z 80
x5 z 85
x6 z 85
I need to get this result in only one row considering prices are always the same by coda
coda pricea priceb pricec
y 20 50 60
z 80 85 85
How can I get this result with sql?
I tried to do it by sum and group by coda but it returns the sum of prices.
If prices are always same for a coda value, you could use group by and use min/max to get it in the same row...
select coda
, min(pricea) pricea
, min(priceb) priceb
, min(pricec) pricec
FROM table
group by coda