Select query when one column value is equal with column name - sql

So I have this query where I need to select subtraction value of parent with busines of the codes.
For example let's say in the container table we have Parent value 0.39 and the child value 0.7 than the value fromthe selection would be -0.31‬.
Now this value (-0.31‬) I need to multiply it with the value of Quality column which is found in another table. Than I need the top 3 values. That means ordering by desc ofcourse.
But ofcourse it should be multiplied when NetNames is equal with BetNames and Codes column value is equal with one the columns in table container. (Q_1, Q_2, Q_3).
I'm lost here guys.
Below is info about my tables.
/*Table Container*/
BetNamesID | Parent_B_Id | Year | Month | Q_1 | Q_2 | Q_3
1 null 2020 5 0.36 0.3 0.21
6 2 2020 8 0.39 0.64 1.0
7 1 2020 9 0.76 0.65 0.29
8 3 2020 13 0.62 0.34 0.81
9 2 2020 2 0.28 0.8 1.0
/*Table Configuration*/
NetNames | Codes | Quality
Test 1 Q_1 5
Test 2 Q_5 7
Test 3 Q_2 24
Test 4 Q_3 98
Test 5 Q_4 22
/*Table BetNames Info*/
ID | Parent_B_Id | Name
1 null Test 1
6 2 Test 2
7 1 Test 3
8 3 Test 4
9 2 Test 5
What I have done until now is this query :
SELECT
child.[BetNamesID],
child.[Parent_B_Id],
child.[Q_1] - parent.[Q_1] AS Q_1,
child.[Q_2] - parent.[Q_2] AS Q_2,
child.[Q_3] - parent.[Q_3] AS Q_3,
// this is just a test case.. this is how it is supposed in my mind(child.[Q_3] - parent.[Q_3]) * qualityvalue AS Q_3, //this is how it is supposed to be
, n.name
FROM [dbo].[Container] child
join [dbo].[Container] parent on child.Parent_B_Id = parent.BetNamesID
join dbo.NetNames n on n.id = parent.Parent_B_Id //with this I get the names for BetNamesID
And this is the result of my query until now:
BetNamesID | Parent_B_Id | Q_1 | Q_2 | Q_3
3 2 0.21 -0.3 -0.1
5 4 -0.39 0.64 -0.9
8 5 0.99 0.65 0.59
What I need now is to multiply the values of Q_1, Q_2, Q_3 columns, with the values found in Config table (Quality column), only when BetNames is equal with NetNames and Codes row value is equal with Q_1 or Q_2 or Q_3 column.
These are the expected values.
BetNamesID | Parent_B_Id | Q_1 | Q_2 | Q_3
3 2 1.05‬(0.21 * 5) -7.2(-0.3* 24) -9.8 (-0.1* 98)
5 4 1.95(0.39*5) 15.36(0.64*24) -88.2 (-0.9*98)
How does the new Table come in play? How can I join? How does the where condition work in this case?

Related

how to get average value of a partition data with sql?

I have this table:
type id qty hour date
a manual 1 2 2020-05-06
a manual 2 3 2020-05-06
a manual 4 6 2020–05-06
b manual 2 4 2020-05-03
b manual 1 2 2020-05-03
b manual 4 5 2020-05-03
I need to get the productivity by dividing qty and hour, my query is:
select type, date, qty/hour as prod,
from table1
where id = ‘manual’
group by type, date
with the query above, I got:
type id qty hour date prod
a manual 1 2 2020-05-06 0.5
a manual 2 3 2020-05-06 0.67
a manual 4 6 2020–05-06 0.67
b manual 2 4 2020-05-03 0.5
b manual 1 2 2020-05-03 0.5
b manual 4 5 2020-05-03 0.8
Next, I need to get the average productivity based on the type and also date, the expected result:
type date avg
a 2020-05-06 0.613
b 2020-05-03 0.6
How should I write my query to get the expected result?
If I understand correctly, you want:
select type, date, avg(qty/hour) as prod,
from table1
where id = 'manual'
group by type, date
I don't know how you generated your current intermediate results, but grouping by type and date must generate a result set of only 2 records, based on your sample input data.

Python/Pandas: Transformation of column within a list of columns

I'd like to select a subset of columns from a DataFrame while applying a transformation to some of those columns at the same time. Is it possible to transform a column when that column is selected as one in a list of columns?
For example, I have a column StartDate that is of type np.datetime[64] that I'd like to extract the month from.
When dealing with that Series on its own, I'd do something like
print(df['StartDate'].transform(lambda x: x.month))
to see the transformed data. Can I accomplish the same thing when the above expression is part of a list of columns? Something like:
print(df[['ColumnA', 'ColumnB', 'StartDate'.transform(lambda x: x.month)]])
Of course the above gives the error
AttributeError: 'str' object has no attribute 'month'
So, if my data looks like:
Metadata | Metadata | 2020-01-01
Metadata | Metadata | 2020-02-06
Metadata | Metadata | 2020-02-25
I'd like to see:
Metadata | Metadata | 1
Metadata | Metadata | 2
Metadata | Metadata | 2
Without appending a new separate "Month" column to the DataFrame. Is this possible?
If you have some data like below
df = pd.DataFrame({'col1' : np.random.randint(10, size = 366), 'col2': np.random.randint(10, size = 366),'StartDate' : pd.date_range('2018', '2019')})
which looks like
col1 col2 StartDate
0 0 2 2018-01-01
1 8 0 2018-01-02
2 0 5 2018-01-03
3 3 4 2018-01-04
4 8 6 2018-01-05
... ... ... ...
361 8 8 2018-12-28
362 9 9 2018-12-29
363 4 1 2018-12-30
364 2 4 2018-12-31
365 0 9 2019-01-01
You could redefine the column, or you could assign and create a temporary view, like.
df.assign(StartDate = df['StartDate'].dt.month)
which outputs.
col1 col2 StartDate
0 0 2 1
1 8 0 1
2 0 5 1
3 3 4 1
4 8 6 1
... ... ... ...
361 8 8 12
362 9 9 12
363 4 1 12
364 2 4 12
365 0 9 1
This also doesn't change the original dataframe. If you want to create a permanent version, then just reassign.
df = df.assign(StartDate = df['StartDate'].dt.month)
You could also take this further, such as.
df.assign(StartDate = df['StartDate'].dt.month, col1 = df['col1'] + 100)[['col1', 'StartDate']]
You can apply whatever transform you need and then access any columns you want after assigning these transforms.
col1 StartDate
0 105 1
1 109 1
2 108 1
3 101 1
4 108 1
... ... ...
361 104 12
362 102 12
363 109 12
364 102 12
365 100 1
I guess you could use the attribute name of the Series.
Something like:
dt_to_month = lambda x: [d.month for d in x] if x.name == 'StartDate' else x
df[['ColumnA', 'ColumnB', 'StartDate']].apply(dt_to_month)
will do the trick.

Count number of rows before date per id

I'm not sure how else to explain it other than the title. I'm basically trying to get the number of rows per id before the date on that specific row. I've tried a bunch of things and scoured the internet to no avail. Please help!
Before
id date
1 3/3/2015
2 3/27/2015
2 4/15/2015
2 5/1/2015
3 3/7/2015
3 5/17/2015
3 7/9/2015
3 7/19/2015
After
id date count
1 3/3/2015 0
2 3/27/2015 0
2 4/15/2015 1
2 5/1/2015 2
3 3/7/2015 0
3 5/17/2015 1
3 7/9/2015 2
3 7/19/2015 3
-1 + row_number() over (partition by id order by date)

SUM in SQL Server with PARTITION BY clause

I have the following table
QuotationId QuotationDetailId DriverId RangeFrom RangeTo FixedAmount UnitAmount
-------------------------------------------------------------------------------------------
10579 7 1 1 1 1154.00 0.00
10579 7 2 2 2 1731.00 0.00
10579 11 1 0 10 0.00 88.53
10579 11 2 11 24 885.30 100.50
10579 11 3 25 34 2292.30 88.53
I need to write a query in SQL Server with the following logic,
The grouping is QuotationId + QuotationDetailId.
For each of this block I need to sum from the second line on the value of the previous line for fixed
Amount + UnitAmount * RangeFrom + FixedAmount of the current row
So in this case the resulting output should be
QuotationId QuotationDetailId DriverId RangeFrom RangeTo FixedAmount UnitAmount
10579 7 1 1 1 1154.00 0.00
10579 7 2 2 2 2885.00 0.00
10579 11 1 0 10 0.00 88.53
10579 11 2 11 24 1770.60 100.50
10579 11 3 25 34 7174.90 88.53
I've tried several queries but without success, can someone suggest me a way to do that ?
Best regards
Fabrizio
In SQL Server 2012+, you can do a cumulative sum. I'm not sure exactly what the logic is you want, but this seems reasonable given the data set:
select t.*,
sum(FixedAmount*UnitAmount) over (partition by QuotationId, QuotationDetailId
order by DriverId
) as running_sum
from t;
you can use a subquery, your 'amount' column would appear on the list of columns as a query in brackets,
SELECT ...fields...,
(SELECT SUM(A.unitAmount * A.RangeFrom + A.fixedAmount)
From YourTable A
WHERE A.QuotationId = B.QuotationId
AND A.QuotationDetailId = B.QuotationDetailId
AND A.DriverId <= B.DriverId) AS Amount
From YourTable B

SQL - Retrieve Closest Lower Value

When a column value does not equal, I would like to retrieve the closest lower pay value.
For instance: 10 yearsOfService should equal the value 650.00; 14 yearsOfService would equal the value 840.00 in the below incentive table,
ID Pay yearsOfService
1 125.00 0
2 156.00 2
3 188.00 3
4 206.00 4
5 650.00 6
6 840.00 14
7 585.00 22
8 495.00 23
9 385.00 24
10 250.00 25
I have tried several different approaches; including:
SELECT TOP 1 (pay) as incentivePay
FROM incentive
WHERE yearsOfService = '10'
This works but only for yearsOfService that match.
With 10 yearsOfService:
RESULTSET = [1 650.00]
Any ideas?
Please try:
SELECT TOP 1 (pay) as incentivePay
FROM incentive
WHERE yearsOfService <= '10'
ORDER BY yearsOfService desc