t-sql query to fill the null values - tsql

I am a newbie to sql server 2012 and would like to find out how to create the following output using t-sql. there are many club numbers so there has to be a loop or cursor. Please help!!
Tables
club_number name number
---------- -------------------------------------------------- -----------
355292 NULL NULL
NULL Giviton Mbunge 355308
NULL Etero Aaron 355317
NULL Evason Banda 355326
NULL Kachibobo Batoni 355335
NULL Kashamba Nkhani 355344
355353 NULL NULL
NULL Daniel Banda 355362
NULL James Aaron 355371
NULL Amson Kamanga 355380
NULL Gostino George 355399
355405 NULL NULL
NULL Yohane Zimba 355414
NULL Haward M.Chilembwe 355423
NULL Zikiele Blangete 355432
355441 NULL NULL
Result: I would like to see the above TABLE as below, which query can do it? please help
club_number name number
---------- -------------------------------------------------- -----------
355292 NULL NULL
355292 Giviton Mbunge 355308
355292 Etero Aaron 355317
355292 Evason Banda 355326
355292 Kachibobo Batoni 355335
355292 Kashamba Nkhani 355344
355353 NULL NULL
355353 Daniel Banda 355362
355353 James Aaron 355371
355353 Amson Kamanga 355380
355353 Gostino George 355399
355405 NULL NULL
355405 Yohane Zimba 355414
355405 Haward M.Chilembwe 355423
355405 Zikiele Blangete 355432
355441 NULL NULL

SELECT club_number = MAX(club_number) OVER
(
ORDER BY COALESCE(club_number, number)
ROWS UNBOUNDED PRECEDING
),
name, number
FROM dbo.your_table
ORDER BY club_number;

Related

Postgres - How to use a LATERAL SELECT CASE to return a unique value instead of a list?

Considering the following table signatures referencing the signature date of a document by 2 persons
id
p1_signed_at
p2_signed_at
1
NULL
NULL
2
01/01/2022
NULL
3
NULL
07/08/2022
4
03/04/2022
04/04/2022
I want to identify the next signatory of each document.
I tried to use a FROM LATERAL to be able to filter non-null rows, it's working, but the result is a list.
How can i make postgres understand that the identity comlumn is a single value ?
SELECT
"id",
"identity"
FROM
"signatures",
LATERAL (
SELECT CASE
WHEN "p1_signed_at" IS NULL THEN 'p1'
WHEN "p2_signed_at" IS NULL THEN 'p2'
END) AS "identity"
WHERE
"identity" IS NOT NULL
id
identity
1
(p1)
2
(p2)
3
(p1)
"identity" is a table alias, not a column alias. If you use that in the SELECT list, it will be shown as an anonymous record. You need to give your CASE expression a proper alias to refer to the column:
SELECT
id,
p."identity"
FROM
signatures,
LATERAL (
SELECT CASE
WHEN p1_signed_at IS NULL THEN 'p1'
WHEN p2_signed_at IS NULL THEN 'p2'
END) AS p("identity")
WHERE
p."identity" IS NOT NULL
p("identity") defines a table alias with the name p and a column with the name "identity"
The lateral cross join seems unnecessary, a simple CASE expression in the SELECT list would achieve the same.
SELECT
id,
CASE
WHEN p1_signed_at IS NULL THEN 'p1'
WHEN p2_signed_at IS NULL THEN 'p2'
END as "identity"
FROM
signatures
WHERE p1_signed_at is null
OR p2_signed_at is null;
If you want to access the column alias of the CASE expression by name, you need to wrap this in a derived table:
select *
from (
SELECT
id,
CASE
WHEN p1_signed_at IS NULL THEN 'p1'
WHEN p2_signed_at IS NULL THEN 'p2'
END as "identity"
FROM
signatures
) x
where "identity" is not null

Trying to import data (.tsv file) into custom tables using SQL developer

As a part of project, I did change the field names and lengths of 2 columns. And the since I moved these database tables into Production environment. The data in those 2 columns is lost. How can i upload the data into those 2 columns. I got the copy of the file (*.tsv file). Its quite huge around 200 MB. And the only tool i got is SQL developer.
What are the best possible ways to import these 2 columns in production environment.
Here's how the updated table structure is-
2 fields which i updated are- RPTC_PAY_NBR and RPTC_PAYROLL_NBR
desc PS_RPTC_TAX_WAGE
Name Null Type
PAYGROUP NOT NULL VARCHAR2(3)
FILE_NBR NOT NULL VARCHAR2(6)
CHECK_DT DATE
WEEK_NBR NOT NULL VARCHAR2(2)
RPTC_PAYROLL_NBR NOT NULL VARCHAR2(2)
CHECK_NBR NOT NULL NUMBER(10)
PAY_END_DT DATE
EMPLID NOT NULL VARCHAR2(11)
EMPL_RCD_NBR NOT NULL NUMBER(4)
VOID_IND NOT NULL VARCHAR2(1)
RPTC_PAY_NBR NOT NULL VARCHAR2(2)
RPTC_CUR_FEDTAX NOT NULL NUMBER(14,2)
RPTC_CUR_FEDWGS NOT NULL NUMBER(14,2)
RPTC_CUR_FUTAWGS NOT NULL NUMBER(14,2)
RPTC_FUTA_ER_LIMIT NOT NULL NUMBER(14,2)
RPTC_CUR_EE_MCWG NOT NULL NUMBER(14,2)
RPTC_CUR_ER_MCWG NOT NULL NUMBER(14,2)
RPTC_CUR_MC_SUR NOT NULL NUMBER(14,2)
RPTC_CUR_MC_SURWG NOT NULL NUMBER(14,2)
RPTC_CUR_EE_SSWG NOT NULL NUMBER(14,2)
RPTC_CUR_MEDTAX NOT NULL NUMBER(14,2)
RPTC_CUR_ER_SSWG NOT NULL NUMBER(14,2)
RPTC_SS_RATE NOT NULL NUMBER(16,4)
RPTC_SS_TAX_LMT NOT NULL NUMBER(14,2)
RPTC_SS_TXBL_LMT NOT NULL NUMBER(14,2)
RPTC_CUR_ER_SSMEDW NOT NULL NUMBER(14,2)
RPTC_CUR_SSTAX NOT NULL NUMBER(14,2)
RPTC_CUR_SSMED NOT NULL NUMBER(14,2)
RPTC_YTD_MED_UNC NOT NULL NUMBER(14,2)
RPTC_YTD_SS_UNC NOT NULL NUMBER(14,2)
Please advise.

Hide some datetimes in a query

I have a query that returns me this result:
-----DATE--------------VALUE1---VALUE2
|2016-09-20 11:15:00| 5653856 | 37580
|2016-09-20 11:16:00| NULL NULL
|2016-09-20 11:18:00| NULL NULL
|2016-09-20 11:20:00| NULL NULL
|2016-09-20 11:30:00| 5653860 37580
|2016-09-20 11:32:00| NULL NULL
|2016-09-20 11:34:00| NULL NULL
In this table, only the records in xx:00, xx:15, xx:30, xx:45, have values, other records are null.
How can I make a condition in my query to get only 00,15,30 and 45 times records and dont show the others?
This is the query:
SELECT t.date,
MAX(CASE WHEN t.id= '924' THEN t.value END) - MAX(CASE WHEN t.id= '925' THEN t.valueEND) as IMA_71,
MAX(CASE WHEN t.id= '930' THEN t.value END) as IMA_73
FROM records t
where office=10
and date between '2016-09-20 11:15:00' and '2016-10-21 11:15:00'
GROUP BY t.office,t.date order by t.date asc;
You could use extract to determine the minute, and filter on that:
where extract('minute' from t.date) in (0, 15, 30, 45)

UNION query in DB2

My query is:
SELECT BRAND,BRAND_GROUP, SUB_BRAND ,SUM(INCOME) AS TOTAL_INCOME FROM
"tema".MMT WHERE BRAND_GROUP IS NULL AND SUB_BRAND IS NULL GROUP BY
BRAND,BRAND_GROUP,SUB_BRAND
UNION
SELECT BRAND,BRAND_GROUP, SUB_BRAND ,SUM(INCOME) AS TOTAL_INCOME FROM
"tema".BGT WHERE BRAND_GROUP IS NULL AND SUB_BRAND IS NULL GROUP BY
BRAND,BRAND_GROUP,SUB_BRAND;
and my output is :
BRAND BRAND_GROUP SUB_BRAND TOTAL_INCOME
----- ----------- --------- ------------
GBS NULL NULL 10000
SWG NULL NULL 10000
GBS NULL NULL 20000
STG NULL NULL 20000
GTS NULL NULL 30000
The problem is that i have 2 categories of BRAND and I want to have just 1. Like this :
Brand Brand_Group Sub_brand Total_Income
GBS - - 30000
STG - - 20000
GTS - - 30000
SWG - - 10000
Can someone help me with an ideea?
I think you want to push your UNION query down into a sub-query, and then do the sum on the results of that, like below.
SELECT
BRAND
,BRAND_GROUP
,SUB_BRAND
,SUM(INCOME) AS TOTAL_INCOME
FROM (
SELECT
BRAND
,BRAND_GROUP
,SUB_BRAND
,INCOME
FROM "tema".MMT
WHERE BRAND_GROUP IS NULL
AND SUB_BRAND IS NULL
GROUP BY
BRAND
,BRAND_GROUP
,SUB_BRAND
UNION ALL
SELECT
BRAND
,BRAND_GROUP
,SUB_BRAND
,INCOME
FROM "tema".BGT
WHERE BRAND_GROUP IS NULL
AND SUB_BRAND IS NULL
GROUP BY
BRAND
,BRAND_GROUP
,SUB_BRAND
) tbl
GROUP BY
BRAND
,BRAND_GROUP
,SUB_BRAND
Two comments:
I changed your query to use UNION ALL vs UNION, because UNION will eliminate duplicates.
Do you need to select the BRAND_GROUP and SUB_BRAND, if you are only getting the rows that are null for those columns? Seems somewhat redundant to me.

TSQL grouping/select help

Hi all wonder if someone can lend a hand; i've got this tsql script (shown below) that is currently returning data based on the owner id, if the record is active and if the record created date is less than todays date. I am then grouping the data together. What i want to achieve is return the most recent record per company.
Currently the data i return is this:
COMPANY A JOE BLOGS NULL 10088 Green NULL NULL 21/07/2007 16:57 Phone Call
COMPANY B JOE BLOGS NULL 10059 Green NULL NULL 20/07/2007 14:57 Phone Call
COMPANY B JOE BLOGS NULL 10059 Green NULL NULL 18/07/2006 09:47 E-mail
COMPANY B JOE BLOGS NULL 10059 Green NULL NULL 19/07/2006 13:19 E-mail
COMAPANY C JOE BLOGS NULL 10866 Green NULL NULL 17/08/2007 12:57 Phone Call
COMAPANY C JOE BLOGS NULL 10866 Green NULL NULL 13/08/2007 10:59 E-mail
COMAPANY C JOE BLOGS NULL 10866 Green NULL NULL 15/08/2007 14:57 E-mail
This is how i want the data to return:
COMPANY A JOE BLOGS NULL 10088 Green NULL NULL 21/07/2007 16:57 Phone Call
COMPANY B JOE BLOGS NULL 10059 Green NULL NULL 20/07/2007 14:57 Phone Call
COMAPANY C JOE BLOGS NULL 10866 Green NULL NULL 17/08/2007 12:57 Phone Call
Could someone, point me in the right direction please?
SELECT fa.name, fa.owneridname, fa.new_technicalaccountmanageridname, fa.new_customerid, fa.new_riskstatusname,
fa.new_numberofopencases, fa.new_numberofurgentopencases, fap.actualend, fap.activitytypecodename, fap.createdby, fap.createdbyname
FROM FilteredAccount fa
INNER JOIN FilteredActivityPointer fap ON fa.accountid = fap.regardingobjectid
WHERE fa.statecodename = 'Active'
AND fap.ownerid = '0F995BDC'
AND fap.createdon < getdate()
GROUP BY fa.name, fa.owneridname, fa.new_technicalaccountmanageridname, fa.new_customerid, fa.new_riskstatusname,
fa.new_numberofopencases, fa.new_numberofurgentopencases, fap.actualend, fap.activitytypecodename, fap.createdby, fap.createdbyname
Try this
SELECT * FROM (
SELECT fa.name, fa.owneridname, fa.new_technicalaccountmanageridname, fa.new_customerid, fa.new_riskstatusname,
fa.new_numberofopencases, fa.new_numberofurgentopencases, fap.actualend, fap.activitytypecodename, fap.createdby, fap.createdbyname ,
RN = ROW_NUMBER() OVER (PARTITION BY fa.name ORDER BY fap.createdby DESC)
FROM FilteredAccount fa
INNER JOIN FilteredActivityPointer fap ON fa.accountid = fap.regardingobjectid
WHERE fa.statecodename = 'Active'
AND fap.ownerid = '0F995BDC'
AND fap.createdon < getdate()
) a WHERE RN = 1