Consider values from multiple columns into 1 final output - mysql

Requirement:
(Related to: MySQL)
I have set of unique names in column A, then column B,.. with each column containing a unique product.
Eg.
enter image description here
I expect an output, that considers the total of all Columns, under 1 single header and outputs something like this:
enter image description here
Is this possible? How do I go about that?
Thank you!
Not really sure how to go about this. Would concat/ Union work here?

select person, sum(item) from
(SELECT person_1 as person, shoes as item from YOURTABLE
union SELECT person_2 as person, watches as item from YOURTABLE
union SELECT person_3 as person, clothes as item from YOURTABLE)
temp group by person

Related

Making a row a column in SSRS

I am trying to make what I think is a very simple report, however I can't seem to get the result I want and I don't even know if it is possible in SSRS.
It's a list of student applicants and the courses they have applied for. Where student apply for more than 1 course, for example 3 A Levels (the maximum they apply for is 5), I want the subjects to appear in different columns rather than a list in one column. A simple table shows the report like this: 3 Columns: student ref, applications status, course(s)
I want it to look like this:
7 columns: Student ref, Application status, course1, course2, course3 etc
I have tried to use a Matrix but the best I can get is where each subject has its own column, so I have a lot of blank text boxes and a very long report as we have 50 plus courses on offer.50+ Columns: Student ref, Application status, English, Maths, Biology, sport, French etc
Assuming this will work with your database, then this should be a pretty simple solution.
A matrix is the way to go I think as as you said, if you have lots of courses with at least one application then the report will be very wide and in manageable.
So, I recreated your sample and then queried it, adding a column that calculated a 'choice number'. this 'choice number' will only ever be between 1 and 5 (based on you statement that no student applies for more than 5 subjects)
Here's the sample data and simple query beneath
DECLARE #t TABLE([Student] varchar(10), [Status] varchar(10), [Subject] varchar(20))
INSERT INTO #t
SELECT 'Stu001', 'OA', 'English' UNION
SELECT 'Stu001', 'OA', 'Maths' UNION
SELECT 'Stu002', 'OA', 'English' UNION
SELECT 'Stu002', 'OA', 'Biology' UNION
SELECT 'Stu002', 'OA', 'Sport' UNION
SELECT 'Stu003', 'OA', 'French' UNION
SELECT 'Stu003', 'OA', 'Chemistry' UNION
SELECT 'Stu003', 'OA', 'English' UNION
SELECT 'Stu004', 'OA', 'BTEC'
SELECT *
, SubjectChoiceNumber = ROW_NUMBER() OVER(PARTITION BY Student, Status ORDER BY Subject)
FROM #t
The SubjectChoiceNumber just assigns a sequential number. The sequence is sorted by the subject name.
This gives us the following output...
Now all you need to do is use a matrix control, set the column grouping to group by SubjectChoiceNumber and set the data cell expression to something like =FIRST(Fields!Subject.Value)
The report design looks like this
An the final output looks like this

MYSQL query to Select the first duplicate record in JAVA

I am trying to retrieve the the first row among the duplicate row, THE FIRST OCCURED ***
--Table--
Order_No Product User
1 Book Student
2 Book Student
3 Book Student
I want to get the Order_No of the first duplicate row in JAVA, I have used DISTINCT and DISTINCT TOP 1 etc but nothing worked, NEED HELP
SELECT min(order_no), product, user
FROM 'table'
GROUP BY user, product
This is basic SQL?
SELECT min(order_no), product, user FROM table GROUP BY product, user
See also more information on GROUP BY
All fields not part of your group by must have some sort of way to determine which to pick of the n potentially different values. min() will pick the lowest value (even with strings and dates) while max() will pick the highest. You can also use First() and Last() to grab the value according to when they show up.
Supposing you had other values to pick from, you might see something like:
SELECT min(order_no), product, user, min(creation_date),
sum(quantity), first(billing_address)
FROM orders GROUP BY product, user
SELECT t.*
FROM table t
WHERE NOT EXISTS ( SELECT a
FROM table t2
WHERE t2.Product = t.Product
AND t2.User = t.User
AND t2.Order_No < t.Order_No
)

how to find an amount of certain duplicates in mysql table?

So I have a table with persons.
This table connects with vehicles with manyToMany.
I need to find how many times does id appear on the table person_vehicle.
How can I do this?
Thanks everybody for asking
Here are two suggestion.
For all the names:
Gives number of times the names are repeated.
SELECT count(name) FROM persons GROUP BY name;
Specifically for the jake:
SELECT count(name) FROM persons WHERE name = "Jake";
What have you tried? Depending on the table, you can do something like:
SELECT * FROM persons WHERE first_name = 'Jake'
Which will return the number of rows with the first name of "Jake"
Alternatively, if you only want one row with the total count of occurrences:
SELECT COUNT(first_name) FROM persons WHERE first_name = 'Jake'
You're looking for GROUP BY.
A table contains multiple columns. You have to decide first which column(s) of the data that you need are in common.
Suppose I have a table address(person_name, plot_no, pin);
If I try to select, for a particulate pin, how many people are living there:
SELECT pin, COUNT(*)
FROM address
GROUP BY pin;
If I try to select, for a particulate plot_no and pin, how many people are living there:
SELECT plot_no, pin, COUNT(*)
FROM address
GROUP BY plot_no, pin;
Use group by statement on the column, on which you are looking for duplicates if the count comes more than 1, it means they are repeating.
Select count(id), name from table_name group by id HAVING count(id) > 1;
Only the duplicates records will come.
Try this:
SELECT count(name) FROM persons WHERE name = "Jake";

Using Distinct in a subquery

How to display all fields of a row based on 1 field that is distinct (i.e there should no duplicates for this field)
Suppose there's a table called Office_Roles with the fields and data as below
Name Department Designation
John Marketing Executive
John Sales Executive
John PR Executive
So i want the end result to display all the fields in a row but for just 1 John (distinct)
Output like -
John Marketing(or Sales) (or Pr) Executive
I was thinking of something like
select * from Office_Roles where name =(select distinct name from Office_Roles);
How do we do something like this correctly ? I also want to do a Order By and Limit the number of results per page on the end result...
You can't have a variable number of columns for a query. One thing you can do is to concatenate the values together:
select name, group_concat(Department) as departments,
group_concat(Designation) as designations
from Office_Roles o
group by name
This will not output exactly how you want , but see if it works for you. Just to give you an idea.
select name, group_concat(Department SEPARATOR ' or ') as departments,
Designation
from Office_Roles a
group by name,Designation
Yoy can play with SQL Fiddle here Demo
You are going to get 3 rows either way since there are 3 in the table that match, which rows are you trying to exclude and how is the query supposed to know what to NOT show?
I would just use GROUP BY
SELECT * FROM Office_Roles GROUP BY name;
easy.

Combine 2 tables with 2 common fields into 1 table with the 2 fields and a source indicator

this is my first time posting so forgive any errors please :)
Source File: Fields
Table 1: ID, Client Number
Table 2: ID, Client Number
Table 1 shows the Customer, and table 2 shows the Bill Payer.Both tables use ID as the transaction ID. So one transaction has 1 record in table 1, and 1 in table 2.
Desired Output:
Table 3: ID, Client Number, Customer/Payer.
I am aware that i can do to append table queries to 1 destination table to achieve but if i can do this with ONE SELECT query that would make the "flow" of my database alot smoother, as i am replacing an old query. Any help would be appreciated and thank you.
SELECT ID, Client_Number, 'Customer' AS Type FROM Table 1
UNION ALL
SELECT ID, Client_Number, 'Payer' AS Type FROM Table 2
SELECT ID, ClientNumber, 'Customer' as Kind FROM Customer
UNION ALL
SELECT ID, ClientNumber, 'Player' as Kind FROM Player