I have a table
each different disease create a new row with the same ID, i want to group this rows to transform the ID into a primary key. I already tried a bunch of queryes without success.
With group by you can group by data with a column. Here you want to group by your ID and keep the maximum of every ID. So you can use GROUP BY and display the maximum value with MAX().
SELECT ID, MAX(DISEASE1), MAX(DISEASE2), MAX(DISEASE3)
FROM table
GROUP BY ID
Related
I have a table that in one column has security companies in it, another column has the id of the parent company, and the last column has that security company's market value.
I am trying to calculate the parent company's market value by adding together the market values of the security companies with the same parent id, how do I accomplish this?
The table is called security and I want the new column to be called ParentSecMktValue.
I have tried using SELECT statements:
SELECT security.Parent_id, security.SecMktValue
FROM security
ORDER BY Parent_id ASC;
This produces a table of the parent id's and their market values but I am unsure how to combine the data from the different rows.
SELECT s.Parent_id, SUM(s.market_value) AS ParentSecMktValue
FROM security s
GROUP BY s.Parent_id
;
You can get the sum by using group by on parent_id and using sum function on market value
SELECT security.parent_id, SUM(security.SecMktValue)
FROM security GROUP BY parent_id
ORDER BY Parent_id ASC;
I'm trying to write a SQL statement that outputs a table with the two columns show name and number of seasons. The show column must contain no duplocates and the number of seasons column counts the number of seasons associated with the show entity.
Here are my two tables
Shows Table
id | name
Seasons Table
id | show_id | season_number
Here's what I've tried so far
SELECT DISTINCT shows.name
FROM shows
INNER JOIN seasons on show.id = seasons.show_id;
The above code works for grabbing distinct names but whenever I try adding COUNT(season.id) it breaks.
Any suggestions?
Thanks!
Use group by to aggregate multiple rows with the same name into a group. With count(distinct id) you calculate the number of distinct values of the id column in that group.
SELECT name
, COUNT(DISTINCT seasons.id)
FROM shows
JOIN seasons
ON shows.show_id = seasons.show_id
GROUP BY
name
By the way, I'd expect a season to have a one to many relation to show, not the other way around.
Hi I am making a restaurant management system for an assignment and I have a customer table, item table and order table. I want to enter the customer ID and the item ID to the order table when a customer places an order. My question is how to handle this when one customer orders several items together, how can I insert this to the order table? Is there a way to enter the foreign key of multiple items to the item ID column of the order table?
I am using MySQL server 5.7, Java 1.8 and NetBeans 8.2
Thanks!
Entry in Order table should per user not per Item. And if you want to insert items their are two ways one is in Order table as meta with order entry and second is create another item table with order id as ref key.
You need two table. Order and Order Details.
Order Table will contain Order ID, Customer ID, Date, Total Price, Tax, Offer, Coupon and other details regarding order.
And Order Details will contain Order ID, Item IT, Price, Quantity, Discount, and other details regarding each order item.
I have a table which called "users" which contain these columns of
id,first_name,last_name,phone,gender,user_type
i have another table which called "entrance_logs" which contain 3 colunms
Id. and user id,expiration
I have another table which called :"gifts" which 2 columns
ID and user ID.
I am tring to create a query which will give me the Maximum of the expiration from the "entrance logs" column and the amount of id from table gifts.
I tried this query and it is not return to me the amount of the table of ID correctly.
SELECT users.first_name,users.last_name,users.phone,users.gender,users.user_type,
count(users.id), MAX(gifts.expiration),MAX(entrance_logs.datetime)
FROM users,gifts,entrance_logs
where
users.id= gifts.user_id
and entrance_logs.user_id=users.id
GROUP BY users.id
According to the question description, count(gifts.id) should be used.
Try this:
SELECT users.first_name,users.last_name,users.phone,users.gender,users.user_type,
count(gifts.id), MAX(gifts.expiration),MAX(entrance_logs.datetime)
FROM users,gifts,entrance_logs
where
users.id= gifts.user_id
and entrance_logs.user_id=users.id
GROUP BY users.id
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
)