Business Objects: last_value()-type function - business-objects

I need to get the last value associated with a specific ID field in Business Objects. Is there a "last_value()" function in Business Objects, or what is the best way to achieve this?
InsuredParty
RecID|InsuredID|InsuredName
1 | 101 | CompanyName1
2 | 101 | CompanyName1, LLC
3 | 101 | CompanyName1 & Sons, LLC
Policy
RecID|InsuredID|Policy Number
1 | 101 | ABC 1234567 00
2 | 101 | ABC 1234567 01
3 | 101 | ABC 1234567 02
Desired Record Output:
InsID|Insured Name |Policy Number
101 | CompanyName1 & Sons, LLC | ABC 1234567 00
101 | CompanyName1 & Sons, LLC | ABC 1234567 01
101 | CompanyName1 & Sons, LLC | ABC 1234567 02
The ultimate report will look like:
Insured Name Policy Number
CompanyName1 & Sons, LLC
ABC 1234567 00
ABC 1234567 01
ABC 1234567 02

Related

MySQL - Is there a better way to structure one of my table to reduce the amount of columns?

I have a (3x3) table on my website which gives the users the option to unlock a car slot, purchase a car and upgrade the car level.
In total I end up with 9 slots, all 9 slots can have different values (car_unlocked (bool), car_level (int), car_type (int)).
my current table : (user_cars)
id (AUTO_INCR) | user_id | car_slot_unlocked01 | car_level01 | car_type01 | car_slot_unlocked02 | car_level02 | car_type02 | car_slot_unlocked03 | car_level03 | car_type03 | car_slot_unlocked04 | car_level04 | car_type04 …
And so on until 9. I end up with 29 columns. And then I retrieve the values with the user_id on my website. Values are unique to every user.
How would I go about reducing the columns amount, because if I was to add more rows (4x4) or (5x5) to my website table I would end up with alot more columns in my database.
Do I create different db table and join it if that is even an option?
Thank you in advance.
Maybe you can give an ID to the cars so you don't need the repeating columns... something like:
id | user_id | car_id | car_slow_unlocked | car_level | car_type
-----------------------------------------------------------------
1 | 001 | 01 | 1 | 3 | 3
2 | 001 | 02 | 0 | 5 | 2
3 | 001 | 03 | 0 | 5 | 3
4 | 001 | 04 | 1 | 6 | 1
5 | 001 | 05 | 1 | 1 | 1
6 | 001 | 06 | 0 | 5 | 2
7 | 001 | 07 | 0 | 5 | 3
8 | 001 | 08 | 1 | 6 | 1
9 | 001 | 09 | 1 | 1 | 1
10 | 002 | 01 | 1 | 5 | 3
11 | 002 | 02 | 0 | 3 | 1
12 | 002 | 03 | 1 | 1 | 1
... and so on
Seems like you need to have a think about your data model. I don't quite understand your business but start with identifying the entities and their attributes outside of any presentation concerns.
e.g.
CarInstance (ID, type, unlocked)
CarType(ID, CarID, Level)
User(ID, name)
UserToCar(userID,carID)
I doubt those are right but that's the kind of thing you need to be aiming for. You can then generate your table from that.

How can I return each record associated with State and City while returning State and City name only once? (MySql)

I'm not really sure how to ask this and I've been search-engining for awhile and haven't come up with anything useful.
Say I have the following three tables People, Cities and States:
PeopleID | Name | Age | CityIDFK
--------------------------------------------
1 | John | 24 | 20
2 | Jim | 28 | 21
3 | Joan | 49 | 10
4 | Mike | 37 | 10
5 | Bruce | 26 | 2
6 | Peter | 22 | 20
7 | Oprah | 27 | 3
7 | Jake | 21 | 1
CityIDPK | City | StateIDFK
---------------------------------------
1 | Seattle | 1
2 | Gotham | 2
3 | Oakland | 4
10 | Boise | 5
20 | Austin | 6
21 | Tyler | 6
StateIDPK | StateName
----------------------------
1 | Washington
2 | New York
3 | Oregon
4 | California
5 | Idaho
6 | Texas
How can I achieve the following output:
StateName | City | Name
---------------------------------------
California | Oakland | Oprah
Idaho | Boise | Mike
| | Joan
New York | Gotham | Bruce
Washington | Seattle | Jake
Texas | Austin | John
| | Peter
| Tyler | Jim
I'm not sure if the above output is possible or not, I would probably just do something like this:
SELECT StateName, City, Name FROM People
INNER JOIN Cities
ON Cities.CityIDPK = People.CityIDFK
INNER JOIN States
ON States.StateIDPK = Cities.StateIDFK
But that would return the StateName and City for every person, instead of just once.
StateName | City | Name
---------------------------------------
California | Oakland | Oprah
Idaho | Boise | Mike
Idaho | Boise | Joan
New York | Gotham | Bruce
Washington | Seattle | Jake
Texas | Austin | John
Texas | Austin | Peter
Texas | Tyler | Jim
If the output I want to achieve is possible, would someone show me an example of how to write the query, or point me in the right direction?
I agree with Tim, this should be handled in your presentation layer.
But you can do it if you're using MySQL 8 by taking advantage of window functions. We can use row_number() to determine the first row per state and per city, then use a case to only display the name for the first row.
SELECT
case row_number() over states_w
when 1 then states.name
else '' end as StateName,
case row_number() over cities_w
when 1 then cities.name
else '' end as CityName,
people.name as Name
FROM People
INNER JOIN Cities ON Cities.id = People.City_id
INNER JOIN States ON States.id = Cities.state_id
window states_w as (partition by states.id),
cities_w as (partition by cities.id);
If you're using MySQL before 8... upgrade. If you can't upgrade, it can be emulated.

mysql - conditionals with joins - only distinct rows

Sorry for not precise subject. More precise description is here:
Consider example tables:
City:
cityId | cityName
----------------------
51 | NY
52 | Chicago
53 | SanFrancisco
People:
peopleId | peopleName |
-----------------------
21 | John |
22 | Emma |
23 | Frank |
24 | George |
25 | Albert |
Goods:
goodsId | good | peopleId | cityId
------------------------------------
1 | bread | 21 | 51
2 | steel | 22 | 51
3 | onion | 23 | 0
4 | chair | 22 | 52
5 | knife | 22 | 0
Input data:
cityId: 51
peopleId: (21, 22, 23, 25)
Expected result:
peopleId | peopleName | cityId | cityName | goodId | good
-----------------------------------------------------------
21 | John | 51 | NY | 1 | bread
22 | Emma | 51 | NY | 2 | steel
23 | Frank | 0 | --- | 3 | onion
25 | Albert | --- | --- | --- | ----
22 | Emma | 0 | --- | 5 | knife
Even if there is almost empty data still some row is displayed (here peopleId 25). What is going on here?
I want to select all the goods.good that have people.peopleId in input data (21, 22, 23, 25) and cityId = 51. However not every row in goods table have specified goods.cityId, and also not every goods.peopleId is in goods table. I want my result to be conditionally dependent:
if peopleId exists in goods table and this row have also cityId i am looking - print it.
else if peopleId exists in goods table but goods.cityId = 0 - select it.
else if peopleId doesnt exist in goods table - print only peopleId and peopleName, rest of the fields leave empty.
I already made this:
SELECT DISTINCT people.peopleId, people.peopleName,
CASE
WHEN goods.cityId = 51
THEN (SELECT goods.good FROM goods WHERE goods.zalozenie = 51 AND goods.peopleId = people.peopleId)
ELSE
CASE
WHEN goods.cityId = 0
THEN (SELECT goods.good FROM goods WHERE goods.cityId = 0 AND goods.peopleId = people.peopleId)
ELSE -1
END
END AS goods FROM people LEFT JOIN goods ON people.peopleId = goods.peopleId WHERE people.peopleId IN ( 21, 22, 23, 25 )
This works almost fine but result shows:
peopleId | peopleName | cityId | cityName | goodId | good
-----------------------------------------------------------
21 | John | 51 | NY | 1 | bread
22 | Emma | 51 | NY | 2 | steel
23 | Frank | 0 | --- | 3 | onion
25 | Albert | --- | --- | --- | ----
22 | Emma | 0 | --- | 5 | knife
Unwanted row is the last one - it should only select second line and ignore last one if peopleId = 22 with cityId > 0 is already on result.
Hope you can help me!
Kalreg
I think you are trying to do something like this:
SELECT p.peopleId, p.peopleName, c.cityId, c.cityName, g.goodId, g.good
FROM people p left join goods g
on p.peopleId = g.peopleId
left join city c
on c.cityId = g.cityId
WHERE
(g.goodId is null or g.cityId = 51 or (g.cityId = 0 and NOT EXISTS (SELECT * FROM goods g2 WHERE g2.peopleId=p.peopleId and g2.cityId>0)))
and p.peopleId IN (21, 22, 23, 25);
See this sql fiddle to play around with it a bit. Hopefully that's a starting point, anyway.

MySQL query to find missing values in time series

I have a table containing thousands of customer records, with customer ids ideally repeating for every month.
Customer Org | CustomerID | Month
001 | 111 | Jan-15
001 | 112 | Jan-15
001 | 113 | Jan-15
001 | 111 | Feb-15
001 | 112 | Feb-15
001 | 111 | Mar-15
001 | 113 | Mar-15
I would like to write a SQL query that gives me 'inconsistent' customers within a customer org, i.e those who do not have records every month.
For eg, if I pass the customer org '001', the expected output needs to be
Jan-15 | Feb-15 | Mar-15
112 | 112 | -
113 | - | 113
Thanks for your help in advance

delete duplicated row and merge cell value mysql

I have a MySQL table which has three columns:
Name | Email | BDate
---------------------------------------------------------
John Doe | jdoe#company.com | June 1, 1980
Kelly Smith | | June 2, 1983
Richmond White | rwhite#company.com |
John Doe | |
Kelly Smith | ksmith#company.com |
Richmond White | | June 3, 1984
What I want to do is to delete duplicate name and merge the cell value. I want my table to look like this:
Name | Email | BDate
---------------------------------------------------------
John Doe | jdoe#company.com | June 1, 1980
Kelly Smith | ksmith#company.com | June 2, 1983
Richmond White | rwhite#company.com | June 3, 1984
How would my query look like to return my desire table?
Thanks in advance!
Query:
SQLFIDDLEExample
SELECT t1.Name,
MAX(t1.Email) Email,
MAX(t1.BDate) BDate
FROM Table1 t1
GROUP BY t1.Name
Result:
| NAME | EMAIL | BDATE |
------------------------------------------------------
| John Doe | jdoe#company.com | June 1, 1980 |
| Kelly Smith | ksmith#company.com | June 2, 1983 |
| Richmond White | rwhite#company.com | June 3, 1984 |
One option is to use the following query to create a new table and then delete the old table:
SELECT Name, GROUP_CONCAT(Email) AS Email, GROUP_CONCAT(BDate) AS DBate
FROM YourTable
GROUP BY Name