How to input value from different table range in SQL - sql

I want to ask how to enter NULL in Table 2 with values in LITHOLOGY column.
Thanks
Table 1
LITHOLOGY
SITE_ID
DEPTH_FROM
DEPTH_TO
BXg
DB-01
0.00
4.50
BXg2
DB-01
4.50
10.00
BXg2
DB-02
4.00
10.00
BXg2
DB-02
0.00
4.00
Table 2
SITE_ID
DEPTH_FROM
DEPTH_TO
CORE_RECOV
LITHOLOGY
DB-01
0.00
2.00
0.20
DB-01
2.00
4.00
0.30
DB-01
4.00
6.00
0.22
DB-01
6.00
8.00
0.32
DB-01
8.00
10.00
0.42
DB-02
0.00
3.00
0.12
DB-02
3.00
5.00
0.42
DB-02
5.00
10.00
0.92

As per your question, the below will put NULL in every LITHOLOGY column where LITHOLOGY is not null.
If you want to replace blank fields with null use WHERE LITHOLOGY = '' instead
UPDATE Table2
SET LITHOLOGY = NULL
WHERE LITHOLOGY IS NOT NULL;

Related

SQL Query Join Table using where and or

I want to ask for join 4 table.
this query and result
select a.POH_NOPO, b.pod_prdcd, c.PRD_KODE, c.prd_nama, d.merk_namamerk,
b.pod_hrgbeli, b.POD_QTYPO, b.POD_PPN, b.POD_DISC1, b.POD_DISC2
from tbTr_PurchaseOrder_H a
Left join tbTr_PurchaseOrder_D b on a.POH_NOPO =
b.POD_NOPO
Left join tbMaster_Barang c on c.PRD_PRDCD = b.pod_prdcd
Left join tbMaster_Merk d on c.prd_kodemerk = d.MERK_KODEMERK where
b.pod_prdcd = '3710273' or c.PRD_KODE ='6920354814785' and
a.POH_NOPO='PO-0104-202302-00029'
POH_NOPO pod_prdcd PRD_KODE prd_nama merk_namamerk pod_hrgbeli POD_QTYPO POD_PPN POD_DISC1 POD_DISC2
PO-0104-202302-00007 3710273 6920354814785 P.GIGI CHARCOAL DC 72/150GR COLGATE TOTAL 31743.00 6.00 3491.73 0.00 0.00
PO-0104-202302-00009 3710273 6920354814785 P.GIGI CHARCOAL DC 72/150GR COLGATE TOTAL 31743.00 11.00 3491.73 0.00 0.00
PO-0104-202302-00015 3710273 6920354814785 P.GIGI CHARCOAL DC 72/150GR COLGATE TOTAL 31743.00 6.00 3491.73 0.00 0.00
PO-0104-202302-00017 3710273 6920354814785 P.GIGI CHARCOAL DC 72/150GR COLGATE TOTAL 31743.00 6.00 3491.73 10.00 0.00
PO-0104-202302-00029 3710273 6920354814785 P.GIGI CHARCOAL DC 72/150GR COLGATE TOTAL 31743.00 4.00 3491.73 10.00 0.00
PO-0104-202302-00030 3710273 6920354814785 P.GIGI CHARCOAL DC 72/150GR COLGATE TOTAL 31743.00 12.00 3491.73 10.00 0.00
But I want is the result like this:
POH_NOPO pod_prdcd PRD_KODE prd_nama merk_namamerk pod_hrgbeli POD_QTYPO POD_PPN POD_DISC1 POD_DISC2
PO-0104-202302-00029 3710273 6920354814785 P.GIGI CHARCOAL DC 72/150GR COLGATE TOTAL 31743.00 4.00 3491.73 10.00 0.00

Output last row of each year

My dataframe look like this:
Close Volume Dividends
Date
2014-08-07 14.21 4848000 0.00
2014-08-08 13.95 5334000 0.00
2014-08-11 14.07 4057000 0.00
2014-08-12 14.13 2611000 0.00
2014-08-13 14.15 3743000 0.28
... ... ... ...
2020-08-03 19.45 7352600 0.00
2020-08-04 19.69 4250500 0.00
2020-08-05 19.83 3414080 0.00
2020-08-06 20.40 6128100 0.00
2020-08-07 20.60 8295000 0.00
I like to output the closing price for the last day of each year. I tried the following:
df = df.groupby(df.index.year)['Close'].tail(1)
Date
2014-12-31 16.39
2015-12-31 13.67
2016-12-30 14.78
2017-12-29 21.83
2018-12-31 21.64
2019-12-31 25.00
2020-08-07 20.60
I want the output to be:
Date
2014 16.39
2015 13.67
2016 14.78
2017 21.83
...
Any help would be very much appreciated. Many Thanks!
Try with last
df = df.groupby(df.index.year)['Close'].last()

Mssql summary row using ROLLUP

I'm trying to generate a summary row using ROLLUP grouping,
Here is my query
SELECT nic as NIC,branch_id,SUM(as_share),SUM(as_deposit) as as_deposit,SUM(as_credits) as as_credits,SUM(as_fixed) as as_fixed,SUM(as_ira) as as_ira,SUM(as_saviya) as as_saviya
FROM As_Member_Account_Details
GROUP BY nic,branch_id
WITH ROLLUP
But it give me this output,
112233 1 30.00 0.00 0.00 50.00 0.00 0.00
112233 2 20.00 0.00 0.00 0.00 0.00 0.00
112233 3 0.00 0.00 0.00 0.00 0.00 0.00
112233 NULL 50.00 0.00 0.00 50.00 0.00 0.00
NULL NULL 50.00 0.00 0.00 50.00 0.00 0.00
The row before the last row is unnecessary. Because there should be only 3 data rows + a summary row. How can I eliminate that row
Grouping sets allows more granular control when cubeing data.
SELECT nic as NIC
, branch_id,SUM(as_share)
, SUM(as_deposit) as as_deposit
, SUM(as_credits) as as_credits
, SUM(as_fixed) as as_fixed
, SUM(as_ira) as as_ira
, SUM(as_saviya) as as_saviya
FROM As_Member_Account_Details
GROUP BY GROUPING SETS ((nic,branch_id),())
WITH CTE_YourQuery AS
(
SELECT nic as NIC,branch_id,SUM(as_share),SUM(as_deposit) as as_deposit,SUM(as_credits) as as_credits,SUM(as_fixed) as as_fixed,SUM(as_ira) as as_ira,SUM(as_saviya) as as_saviya
FROM As_Member_Account_Details
GROUP BY nic,branch_id
WITH ROLLUP
)
SELECT *
FROM CTE_YourQuery
WHERE NOT (nic IS NOT NULL AND branch_id IS NULL)

SQL Calculating Turn-Around-Time with Overlapping Concideration

I have a Table (parts) where I store when an item was requested and when it was issued. With this, I can easily compute each items turn-around-time ("TAT"). What I'd like to do is have another column ("Computed") where any overlapping request-to-issue dates are properly computed.
RecID Requested Issued TAT Computed
MD0001 11/28/2012 12/04/2012 6.00 0.00
MD0002 11/28/2012 11/28/2012 0.00 0.00
MD0003 11/28/2012 12/04/2012 6.00 0.00
MD0004 11/28/2012 11/28/2012 0.00 0.00
MD0005 11/28/2012 12/10/2012 12.00 0.00
MD0006 11/28/2012 01/21/2013 54.00 54.00
MD0007 11/28/2012 11/28/2012 0.00 0.00
MD0008 11/28/2012 12/04/2012 6.00 0.00
MD0009 01/29/2013 01/30/2013 1.00 1.00
MD0010 01/29/2013 01/30/2013 1.00 0.00
MD0011 02/05/2013 02/06/2013 1.00 1.00
MD0012 02/07/2013 03/04/2013 25.00 25.00
MD0013 03/07/2013 03/14/2013 7.00 7.00
MD0014 03/07/2013 03/08/2013 1.00 0.00
MD0015 03/13/2013 03/25/2013 12.00 11.00
MD0016 03/20/2013 03/21/2013 1.00 0.00
Totals 133.00 99.00 <- waiting for parts TAT summary
In the above, I manually filled in the ("Computed") column so that there is an example of what I'm trying to accomplish.
NOTE: Notice how MD0013 affects the computed time for MD0015 as MD0013 was "computed" first. This could have been where MD0015 was computed first, then MD0013 would be affected accordingly - the net result is there is -1 day.

Simple SQL math operation gives incorrect results

I am running into an issue with a simple SQL math operation of qty * price is returning an incorrect value.
This is SQL server 2005. Compatibility is set to 80 for 2000 SQL server.
Any help on understanding why I am having the problem shown below
For example:
Transaction Table:
id price qty
1 2.77 20.00
1 2.77 25.00
1 2.77 10.00
2 0.10 50.00
2 0.10 80.00
3 0.10 50.00
3 0.10 60.00
SQL
Select id, price, qty, (qty * price) from transact
The actual problem was this and it was my fault :(
Select id, CAST(price AS DECIMAL(5,2)), qty, (qty * price) from transact
Returns the following:
id price qty Total
1 2.77 20.00 55.400000 Correct
1 2.77 25.00 69.250000 Correct
1 2.77 10.00 27.700000 Correct
2 0.10 50.00 4.800000 Should be 5.0000
2 0.10 80.00 7.680000 Should be 8.0000
2 0.10 50.00 5.050000 Should be 5.0000
2 0.10 60.00 6.060000 Should be 6.0000
3 39.00 1.00 39.000000 Correct
3 39.00 2.00 78.000000 Correct
3 39.00 3.00 117.000000 Correct
You price is being rounded somewhere. The select you are running is not showing the actual price.
select round(0.096, 2) price, 0.096 * 50.00 total
Result:
price total
0.10 4.80000