Our system creates 2 lines per journal number and stamps a segment on each line.
Line 1 is system generated and line 2 is what the user has entered front end.
The issue is the 2nd line of each journal the system populates the segment in the way the user entered it front end, where as the 1st line populates the segment how its stored in the back end - this means we can have case sensitive issues across the journal itself. Not an issue on the journal but causes issues further down the line.
Currently the table looks like below
HMY
Journal #
Segment
1
10001
House
2
10001
HouSE
3
10002
FLAT
4
10002
flat
5
10003
Unit
6
10003
UniT
The 2nd lines segment of each journal must be updated to match the segment on the 1st line.
Desired end result:
HMY
Journal #
Segment
1
10001
House
2
10001
House
3
10002
FLAT
4
10002
FLAT
5
10003
Unit
6
10003
Unit
It has to update to how the system populated the segment so if it shows as all capitals, the 2nd line must be all capitals etc.
I have tried various methods but nothing is quite working how I would expect it. For example I was trying to find the min(hmy) grouped by journal number and update the segment of the max(hmy) grouped by journal number but it wouldn't.
Also looked at updating value based on previous row but could not work how I can incorporate that code into my issue.
You can use LAG to get previous value and update:
update t
set Segment = PrevSegment
FROM (
select *
, LAG(Segment) OVER(PARTITION BY [Journal #] ORDER BY HMY) AS prevSegment
FROM journaltable j
) t
WHERE prevSegment IS NOT NULL -- prevents writing the system generated value
Related
I have a table called "tax_info", this table stores the information of the land tax of my city, it goes like this:
taxpayer_code | condominium_num | lot_area | built_area
-------------------------------------------------------------
0010030078-2 | 00-0 | 143 | 130
0010030079-1 | 02-7 | 283 | 57
0010030080-1 | 02-7 | 283 | 48
0010030081-1 | 02-7 | 283 | 50
the taxpayer code first 3 numbers refer to the city district, the next 3 to the block within the district, and the next 4 can refer to the lot in a block if the condo number is 00-0, or to an apartment, or store, etc if the condo number is different than 00-0, in which case all equal condo numbers refer to the same lot within the block.
what I want to do is pass a list of "taxpayer_code" and get the "lot_area" and "built_area" for the lots. the problem is, if the person lives in a condo, her apartment is a fraction of the total built area for the lot. So, if I search for code 0010030078% (the -X number doesn't matter) the result is:
Lot Area = 143 and Built Area = 130
But if I search for 0010030080%, the result I expect is:
Lot Area = 283 and Built Area 155
And if I search for 0010030078%, 0010030079%, the result:
Lot Area = 426 and Built Area 285
So the database should get the taxpayer codes, then look if the condominium number is different from 00-0 for each code passed, if so, it should add to the sum all the other taxpayer codes that share the same condo number within the same district and block. (ideally, if tax codes belonging to different districts or blocks are passed a warning should be returned, and if more tax codes are added to the sum, a listing with all codes added would be nice, but it's okay if that's too much of a hassle!).
I am new to SQL and can't wrap my head around this, I appreciate every help you can give me, thanks!
Hmmm . . . use a subquery and window functions to add up the values that you want:
select ti.*
from (select ti.*,
(case when condominium_num <> '00-0'
then sum(built_area) over (partition by condominium_num)
else built_area
end) as real_built_area
from tax_info ti
) ti
where . . .
In the database I'm working with, we have a certain naming convention that should be followed and I'm trying to fix errors.
For example, one of the correctly named records would be:
Blue Insurance | Blue Agency | Blue Agency | BL26 | Blue Insurance
Is there any way to search for all records that do not have 4 vertical bars in them?
Thanks!
select * from tablename
where length(colname) - length(replace(colname,'|','')) <> 4
Change length according to the database being used.
I have a requirement, where from front end (JSP)
If market is selected, sectors mapped to Markets have to populate
Likewise, based on sector value selected, End Equipment has to
populate.
Market --1st Drop down
Sector --2nd Drop down
Webcategory -- 3rd Drop down
So initially for getting this page, there will be a database hit,
so as oracle DB developer, I have to send OUT parameter containing this mapping.
End Equipment mapped to Sector and mapped to Market.
I am using below query which is not giving my result as required.
SELECT C.MARKET_SEGMENT_NAME,
C.MARKET_SEGMENT_ID,
B.SECTOR_NAME,
B.SECTOR_ID,
A.WEB_CATEGORY_NAME,
A.WEB_CATEGORY_ID
FROM MSE_WEB_CATEGORY_MASTER A,
MSE_SECTOR_MASTER B,
MSE_MARKET_SEGMENT_MASTER C
WHERE C.MARKET_SEGMENT_ID = B.FK_MARKET_SEGMENT_ID
AND B.SECTOR_ID = A.FK_SECTOR_ID(+);
Output:
MARKET_SEGMENT_NAME MARKET_SEGMENT_ID Sector Sector_id WEB_cate Web_category_id
GOOGLE 90 Slawn 1 FLIPKART 1
BING 100 Clown 2 SNAPDEAL 2
YAHOO 110 VERICON 4 AMAZON 3
YAHOO 110 VERICON 4 E-KART 4
YAHOO 110 QUALCOMM 3
Expected Output:
MARKET_SEGMENT_NAME-MARKET_SEGMENT_ID-SECTOR_NAME-SECTOR_ID-WEB_CATEGORY_NAME-WEB_CATEGORY_ID
GOOGLE-90-Slawn-1-FLIPKART-1
BING-100-Clown-2-SNAPDEAL-2
YAHOO-110-VERICON-4-AMAZON-3
YAHOO-110-VERICON-4-E-KART-4
YAHOO-110-QUALCOMM-3
so that I can send this combination as an Output parameter from my procedure.
Since there is a restriction for not to have DB queries in JSP/presentation layer, everything is been done on DB layer.
I agree with #Ben. Your query appears to return the required data. Concatenating the values into a dash-separated string is a task for the display layer.
Having said that, here is a solution which does what you ask for. || is the concatenation operator. The NVL2() function handles the outer-joined columns, and suppresses the dash when they're blank.
SELECT C.MARKET_SEGMENT_NAME
||'-'||C.MARKET_SEGMENT_ID
||'-'||B.SECTOR_NAME
||'-'||B.SECTOR_ID
||nvl2(A.WEB_CATEGORY_NAME,'-'||A.WEB_CATEGORY_NAME,null)
||nvl2(A.WEB_CATEGORY_ID,'-'||A.WEB_CATEGORY_ID,null) concat_str
FROM MSE_WEB_CATEGORY_MASTER A,
MSE_SECTOR_MASTER B,
MSE_MARKET_SEGMENT_MASTER C
WHERE C.MARKET_SEGMENT_ID = B.FK_MARKET_SEGMENT_ID
AND B.SECTOR_ID = A.FK_SECTOR_ID(+);
Using Access 2010 and its version of SQL, I am trying to find a way to relate two tables in a query where I do not have strict, unique values in each table, using concatenated fields that are mostly unique, then matching each unmatched next record (measured by a date field or the record id) in each table.
My business receives checks that we do not cash ourselves, but rather forward to a client for processing. I am trying to build a query that will match the checks that we forward to the client with a static report that we receive from the client indicating when checks were cashed. I have no control over what the client reports back to us.
When we receive a check, we record the name of the payor, the date that we received the check, the client's account number, the amount of the check, and some other details in a table called "Checks". We add a matching field which comes as close as we can get to a unique identifier to match against the client reports (more on that in a minute).
Checks:
ID Name Acct Amt Our_Date Match
__ ____ ____ ____ _____ ______
1 Dave 1001 10.51 2/14/14 1001*10.51
2 Joe 1002 12.14 2/28/14 1002*12.14
3 Sam 1003 50.00 3/01/14 1003*50.00
4 Sam 1003 50.00 4/01/14 1003*50.00
5 Sam 1003 50.00 5/01/14 1003*50.00
The client does not report back to us the date that WE received the check, the check number, or anything else useful for making unique matches. They report the name, account number, amount, and the date of deposit. The client's report comes weekly. We take that weekly report and append the records to make a second table out of it.
Return:
ID Name Acct Amt Their_Date Unique1
__ ____ ____ ____ _____ ______
355 Dave 1001 10.51 3/25/14 1001*10.51
378 Joe 1002 12.14 4/04/14 1002*12.14
433 Sam 1003 50.00 3/08/14 1003*50.00
599 Sam 1003 50.00 5/11/14 1003*50.00
Instead of giving us back the date we received the check, we get back the date that they processed it. There is no way to make a rule to compare the two dates, because the deposit dates vary wildly. So the closest thing I can get for a unique identifier is a concatenated field of the account number and the amount.
I am trying to match the records on these two tables so that I know when the checks we forward get deposited. If I do a simple join using the two concatenated fields, it works most of the time, but we run into a problem with payors like Sam, above, who is making regular monthly payments of the same amount. In a simple join, if one of Sam's payments appears in the Return table, it matches to all of the records in the Checks table.
To limit that behavior and match the first Sam entry on the Return table to the first Sam entry on the Checks table, I wrote the following query:
SELECT return.*, checks.*
FROM return, checks
WHERE (( ( checks.id ) = (SELECT TOP 1 id
FROM checks
WHERE match = return.unique1
ORDER BY [our_date]) ));
This works when there is only one of Sam's records in the Return table. The problem comes when the second entry for Sam hits the Return table (Return.ID 599) as the client's weekly reports are added to the table. When that happens, the query appropriately (for my purposes) only lists that two of Sam's checks have been processed, but uses the "Top 1 ID" record to supply the row's details from the Return table:
Checks_Return_query:
Checks.ID Name Acct Amt Our_Date Their_Date Return.ID
__ ____ ____ ____ _____ ______ ________
1 Dave 1001 10.51 2/14/14 3/25/14 355
2 Joe 1002 12.14 2/28/14 4/04/14 378
3 Sam 1003 50.00 3/01/14 3/08/14 433
4 Sam 1003 50.00 4/01/14 3/08/14 433
In other words, the query repeats the Return table info for record Return.ID 433 instead of matching Return.ID 599, which is I guess what I should expect from the TOP 1 operator.
So I am trying to figure out how I can get the query to take the two concatenated fields in Checks and Return, compare them to find matching sets, then select the next unmatched record in Checks (with "next" being measured either by the ID or Our_Date) with the next unmatched record in Return (again, with "next" being measured either by the ID or Their_Date).
I spent many hours in a dark room turning the query into various joins, and back again, looking at functions like WHERE NOT IN, WHERE NOT EXISTS, FIRST() NEXT() MIN() MAX(). I am afraid I am way over my head.
I am beginning to think that I may have a structural problem, and may need to write the "matched" records in this query to another table of completed transactions, so that I can differentiate between "matched" and "unmatched" records better. But that still wouldn't help me if two of Sam's transactions are on the same weekly report I get from my client.
Are there any suggestions as to query functions I should look into for further research, or confirmation that I am barking up the wrong tree?
Thanks in advance.
I'd say that you really need another table of completed transactions, it could be temporary table.
Regarding your fears "... if two of Sam's transactions are on the same weekly report ", you can use cursor in order to write records "one-by-one" instead of set based transaction.
I'm new to working with SQL Server 2005 Reporting Servives using RDLs in BIDS.
I need to modify an existing report so that I can merge cells in adjacent rows which would have the same value for that particular column.
Ex: consider this is the table returned from the stored procedure used by the reports RDL.
_________________________________________________
Id SubCategory Field1 Field2 Total
_________________________________________________
1 a Bob US 17
1 b John UK 17
2 a Mary AUS 12
3 d Ram IND 19
4 b Alex UK 09
4 c Abby FR 09
5 e Tim IT 03
_________________________________________________
Table Example - Couldn' Format Text :( Image here : http_://i.stack.imgur.com/gWEH5.png_
What I need to do is I want the cells merged into one where two adjacent rows in the same column have the same value.
Like Id 1 is repeated twice, so the cells for these must be merged. (Also 4)
Similarly for the last column Total for cells with Ids 1 and 4 must be merged.
The RDL has "TextBox" for columns, I saw some other questions in this forum but were related to Tablix or Matrix, so I thought it would be better if I mentioned it.
I need this merging to be done in the RDL, and this should also be present when exported to Excel.
Hoping someone will be able to help soon.
Change the SQL query to use a group by on ID.
in reporting services drag Id On Row group Of Tablix Upaer than Detail Group
and drag other Fields(except Total) Besid ID and befor vertical dash line In the table.
and drag Total to first cell afte vertical dash line