How to get a record from two columns - mysql

I have a table name Vouchers in which i have three columns named "GroupName","LedgerName" and "Amount".
I have another Table named Accounts in which i have two columns "Name" and "Amount", now i want the column "Name" to have the the GroupName from the Vouchers Table and then all the LedgerName that have same GroupName to fall under that Record. The images explain my question asked above

Isn't this ORDER BY operation??
I don't think you can get your result in that format. But using ORDER BY operation you can get a similar output but with an extra column. :)

Related

Explode json map into multiple columns

I have a dataframe with two columns : ID and Demographic_distribution. ID is just a number (ex:123456). demographic_impression has a list for each ID. here's an example of what you get in the demographic_distribution column for one ID :
ID : 123456
Demographic_distribution : {"age":"25-34","gender":"male","percentage":2.6e-5},{"age":"13-17","gender":"female","percentage":0.000102},{"age":"13-17","gender":"male","percentage":0.000153},{"age":"65+","gender":"unknown","percentage":0.002118},{"age":"55-64","gender":"female","percentage":0.114114},{"age":"45-54","gender":"male","percentage":0.106534},{"age":"35-44","gender":"female","percentage":0.220674},{"age":"35-44","gender":"male","percentage":0.168249},{"age":"55-64","gender":"male","percentage":0.076748},{"age":"65+","gender":"female","percentage":0.086192},{"age":"45-54","gender":"female","percentage":0.152144},{"age":"65+","gender":"male","percentage":0.056202},{"age":"35-44","gender":"unknown","percentage":0.009239},{"age":"55-64","gender":"unknown","percentage":0.002552},{"age":"45-54","gender":"unknown","percentage":0.004952}
You can see that there are 5 age groups, 3 genders and many percentages. I would like to split the demographic column into three different columns for each parameters. Let's not forget that these informations are liked to an ID in each row, otherwise it doesn't make sense. I tried .explode, but it didn't work.
Any idea how to do this ?

how can I use Count distinct function for column A and add a condition of sum for column B

I have a table where column A is "id" and column B is "State" as these columns are in relation ship with other table the values get repeated, but I want to calculate distinct "id" with State their which are fixed(Like deliverd, not delivered, Inprocess). But I am not able to get the count in table. Though I can do that in a chart but it needs to be shown in table.

Row value to column, with JOIN,WHERE,SUM, similar values to different column name

I have 2 tables : rc_course and rc_resultat.
rc_resultat :
rc_course :
Tables should be joined on rc_resultat.course_id=rc_course.id
Here is the SQL query result I'm looking for with WHERE championnat_id=1
Explanation :
rc_resultat.id is the user ID, must be unique (not 2 rows with the same)
R1,R2,...etc are 1 colum per course_id (the column name could be the course_id, no matter, but if a user doesn't have a value for this column it should be handled). Important : as you can see, one course_id can have many points so it should be the SUM of these values.
TOTAL is the total amount of the row.
To understand better, it's a championship points calculation based on multiple events where users participated or not.

SQL Select Statement - Column Ambiguously Defined even when defining which table each column comes from

I'm doing a beginners course on SQL and databases, and I'm trying to write a select statement that will join two tables on Oracle Apex. I'm using join...on... to do so. The two tables each have a column named the same, with the columns filled with matching values. Obviously, they tables need to join where the values match in each column. Here's the code:
select TRANSACTION_ID, BUYER_ID, FIRST_NAME
from TRANSACTIONS
join BUYER
on TRANSACTIONS.BUYER_ID = BUYER.BUYER_ID;
The two Tables are named "TRANSACTIONS" and "BUYER".
Both tables have a column named "BUYER_ID".
Only the table "TRANSACTIONS" has a column named "LOT_ID", which is associated with the buyer ID
Only the table "BUYER" has a column named "FIRST_NAME", which is associated with the buyer ID
So if I were to simply write,
on BUYER_ID = BUYER_ID;
as I understand that would return the error saying "Columns Ambiguously Defined". But even when I specify the tables, as in,
on TRANSACTIONS.BUYER_ID = BUYER.BUYER_ID;
I still get a "Columns Ambiguously Defined". I wondered if it was an issue with the two columns having to be presented with the same name, so I tried,
on TRANSACTIONS.BUYER_ID as "BUYER_ID1" = BUYER.BUYER_ID as "BUYER_ID2";
but then that returns "invalid relational operator".
Any ideas?
The problem isn't in the join, you also have to fully specify the column in the SELECT statement as well, or it won't know which of the BUYER_ID columns to display. Change it to SELECT BUYER.BUYER_ID and it will work.

MySQL join 2 tables on non-unique column and with timestamp conditions

I have 2 MySQL Tables: "parts_revisions" and "categories_revisions". My goal is to use the revisions data in these tables to create a log that lists out all the changes made to parts and categories. Listing the changes to "parts" in one single SQL statement has proven tricky though! Here is the situation:
All entries of each table have "timestamp" columns.
Every parts_revisions entry has a "categoryId" that basically links it to the categories_revisions table. (Every part is a child of a parent category.)
All I want to do is list out all the parts_revisions, but use the human-friendly "name" column from the categories_revisions table based on the categoryId column in parts_revisions. This will make the log more readable.
The trick is that, because there are usually multiple revisions for each category within the categories_revisions table, I cannot do just one big 'ol join on the categoryId column to get the name. The categoryId column is non-unique, and "name"s may vary. What I have to do is get the latest category_revisions entry that has a timestamp that is no later than the timestamp of the part_revisions entry. In other words, we want to get the appropriate category name that was in use AT THE TIME the part revision was made.
Not sure if this matches your table structure, but here's a go at it. It's a bit of an ugly subquery inside a subquery. Guessing it won't be terribly efficient
select part_name,
category,
(select name
from categories_revisions
where categories_revisions.match_id = parts_revisions.category
and categories_revisions.timestamp = (select MAX(categories_revisions.timestamp)
from categories_revisions
where categories_revisions.match_id = parts_revisions.category
and categories_revisions.timestamp < parts_revisions.timestamp)) as name
from parts_revisions;
http://sqlfiddle.com/#!2/da74e/1/0