SELECT field GROUP BY max(field) and GROUP BY other field MySQL - mysql

I have record like this pic, PLEASE CLICK TO SHOW PIC
how to get like this pic, PLEASE CLICK TO SHOW PIC
create table query:
create table lap ( no_faktur varchar(30) primary key, tgl_jual date,
kd_barang varchar(20), nm_barang varchar(100), harga_beli int (20),
harga_jual int(20), jum_jual int(10) );
insert table query:
insert into lap values ('FJ-2013001','2013-10-01','B-001','laptop
Apple','500000','700000','2'); insert into lap values
('FJ-2013002','2012-10-01','B-002','laptop
Apple','300000','500000','4'); insert into lap values
('FJ-2013003','2013-10-03','B-001','laptop
Apple','500000','700000','1'); insert into lap values
('FJ-2013004','2013-10-11','B-003','HDD 500
GB','700000','800000','1'); insert into lap values
('FJ-2013005','2012-10-01','B-001','laptop
Apple','500000','700000','3'); insert into lap values
('FJ-2013006','2013-10-04','B-004','MONITOR
14"','100000','800000','2'); insert into lap values
('FJ-2013007','2013-02-01','B-001','laptop
Apple','700000','850000','4'); insert into lap values
('FJ-2013008','2013-10-21','B-005','HDD 500
GB','800000','900000','3'); insert into lap values
('FJ-2013009','2013-10-27','B-001','laptop
Apple','500000','700000','1');
I think like this:
select
no_faktur,tgl_jual,kd_barang,nm_barang,harga_jual,jum_jual,sum(harga_jual*jum_jual) as total_jual from lap where month(tgl_jual)='10' and
year(tgl_jual)='2013' and jum_jual=(select max(jum_jual) from lap
group by kd_barang) order by total_jual desc;
But doesn't work because *jum_jual more than 1 rows.
Please help, thanks in advance

select no_faktur,tgl_jual,kd_barang,nm_barang,harga_jual,sum(jum_jual) as jum_jual,sum(harga_jual*jum_jual) as total_jual
from lap where month(tgl_jual)='10' and year(tgl_jual)='2013'
group by kd_barang
order by total_jual desc;

Related

Looking For Most Frequent Values SQL Statement

I have a data set that looks like this:
id | Unit_Ids
1 | {"unit_ids" : ["5442","28397"]}
2 | {"unit_ids" : ["5442","3492","2290"]}
etc.
And I'm trying to find the most frequently appearing values in Unit_Ids. As in my example 5442 appears in both lines 1 and 2, it would be the most frequent value. I was just having trouble finding a good way of creating this statement.
Thank you in advanced!
EDIT: Sorry everyone I'm working with MySQL
If 2016+
Example
Declare #YourTable Table ([id] int,[Unit_Ids] varchar(max))
Insert Into #YourTable Values
(1,'{"unit_ids" : ["5442","28397"]}')
,(2,'{"unit_ids" : ["5442","3492","2290"]}')
Select top 1 value
From #YourTable A
Cross Apply OpenJSON([Unit_Ids],'$.unit_ids') R
Order By sum(1) over (partition by value) desc
Returns
value
5442
I'm assuming you are storing JSON strings in the Unit_Ids field. If you do that, you won't be able to extract or aggregate data stored in that field.
You can however create a child table and query it to get aggregated data. Ie:
-- Master table
create table parent(id number primary key);
-- List of units per parent
create table units(
id number not null,
parent_id number not null,
primary key (id, parent_id),
foreign key (parent_id) references parent(id)
);
-- Insert sample data
insert into parent values 1;
insert into parent values 2;
insert into units(parent_id, id) values(1, 5442);
insert into units(parent_id, id) values(1, 28397);
insert into units(parent_id, id) values(2, 5442);
insert into units(parent_id, id) values(2, 3492);
insert into units(parent_id, id) values(2, 2290);
-- Count the number of times a unit id is in the table
select id, count(id) from units group by id;

How to get the higest number out of the table?

So I have a table of players which I created in sql:
CREATE TABLE Player(PlayerID varchar(10) PRIMARY KEY, level int, region varchar(2), name varchar(10));
And then with the following values:
INSERT INTO Player VALUES ('p001','20', 'eu','phatey');
INSERT INTO Player VALUES ('p002', '15', 'eu', 'hellowz');
INSERT INTO Player VALUES ('p003', '10', 'eu', '3xphhate');
Which I don't understand is how to make use of SUM in order to get the highest level of the players(Mostly because I am doing it wrong since I am pretty newbie with this) any suggestion?
select max(column_name) from table_name;
I hope it helps!

SQL Query, insert a max value from another table's column

I have two tables, a booking table and an invoice table. I am trying to update the booking table with booking information and get a max value from the invoice table and insert it into the booking table at the same time.
So far I have this, but it doesn't set any values to the Booking.Invoice_id column
INSERT INTO Booking( user_id, Location_id, Accom_Id,StartDate,EndDate,
Vehreg,PartySize,Invoice_id )
VALUES ('$User_id', '$pitch', '$Accom' , '$start',
'$end','$Vreg','$guests','SELECT Max Invoice_id FROM Invoice;');
any help would be much appreciated
Use insert . . . select:
INSERT INTO Booking(user_id, Location_id, Accom_Id, StartDate, EndDate,
Vehreg, PartySize, Invoice_id )
SELECT '$User_id', '$pitch', '$Accom' , '$start',
'$end', '$Vreg', ' $guests',
MAX(Invoice_ID)
FROM Invoice;
My guess, however, is that you want to run this immediately after inserting a row into Invoice. In that case, you should be using LAST_INSERT_ID().

SQL INSERT - Can I insert null values if column count doesn't match?

I get an error message that says "#1136 - Column count doesn't match value count at row 1" when I try to insert the values below. Is there any way to have the lines of data that are shorter automatically insert as null at the end without manually going into each line of code?
VALUES ('','BUF','','QB1','','QB BILLS','Face=0x52','#0','25','69','13','13','56','81','81','81','3','12','3');
VALUES ('','BUF','','QB2','frank REICH','Face=0x22','#14','25','69','13','13','31','44','25','50','3','3','2');
VALUES ('','BUF','','RB1','thurman THOMAS','Face=0x83','#34','38','69','63','25','75','50','10','7','8','8');
VALUES ('','BUF','','RB2','jamie MUELLER','Face=0x51','#41','44','69','25','88','50','25','5','3','6','2');
VALUES ('','BUF','','RB3','kenneth DAVIS','Face=0xa5','#23','38','69','25','19','50','31','5','2','7','2');
VALUES ('','BUF','','RB4','don SMITH','Face=0x8b','#30','38','69','25','19','50','31','3','3','8','2');
VALUES ('','BUF','','WR1','james LOFTON','Face=0x81','#80','25','69','38','13','50','56','1','8','13','6');
VALUES ('','BUF','','WR2','andre REED','Face=0xb6','#83','25','69','56','13','56','69','1','9','10','10');
VALUES ('','BUF','','WR3','don BEEBE','Face=0x40','#82','25','69','44','13','50','44','1','4','13','2');
VALUES ('','BUF','','WR4','al EDWARDS','Face=0x9a','#85','25','69','19','13','50','44','1','4','7','2');
VALUES ('','BUF','','TE1','keith MCKELLER','Face=0xb7','#84','25','69','38','50','50','50','1','6','7','6');
VALUES ('','BUF','','TE2','pete METZELAARS','Face=0x50','#88','25','69','19','44','50','31','1','3','5','2');
VALUES ('','BUF','','C','kent HULL','Face=0x1e','#67','25','69','38','69');
VALUES ('','BUF','','LG','jim RITCHER','Face=0x7','#51','25','69','38','56');
VALUES ('','BUF','','RG','john DAVIS','Face=0x24','#65','25','69','25','63');
VALUES ('','BUF','','LT','will WOLFORD','Face=0x48','#69','25','69','25','50');
VALUES ('','BUF','','RT','howard BALLARD','Face=0x88','#75','25','69','19','63');
VALUES ('','BUF','','RE','bruce SMITH','Face=0x88','#78','44','56','69','75','25','81','116','7');
VALUES ('','BUF','','NT','jeff WRIGHT','Face=0xb','#91','25','31','31','50','19','19','30','7');
VALUES ('','BUF','','LE','leon SEALS','Face=0xac','#96','25','31','38','44','31','50','25','18');
VALUES ('','BUF','','ROLB','darryl TALLEY','Face=0xad','#56','31','44','50','38','44','63','25','25');
VALUES ('','BUF','','RILB','ray BENTLEY','Face=0x30','#50','25','31','38','38','31','56','13','10');
VALUES ('','BUF','','LILB','shane CONLAN','Face=0x2f','#58','31','44','50','56','19','69','13','13');
VALUES ('','BUF','','LOLB','c. BENNETT','Face=0x82','#97','38','50','63','63','19','69','29','7');
VALUES ('','BUF','','RCB','nate ODOMES','Face=0xc3','#37','38','44','56','38','38','56','0','25');
VALUES ('','BUF','','LCB','kirby JACKSON','Face=0x89','#47','25','31','44','38','50','50','0','64');
VALUES ('','BUF','','FS','mark KELSO','Face=0x26','#38','31','38','50','38','44','44','2','39');
VALUES ('','BUF','','SS','leonard SMITH','Face=0x84','#46','31','38','50','44','44','50','2','40');
VALUES ('','BUF','','K','scott NORWOOD','Face=0x29','#11','56','81','81','31','44','44','6');
VALUES ('','BUF','','P','rick TUTEN','Face=0x20','#10','25','56','44','31','19','63','3');
Remove the unwanted column from your mysql statement
for example
mysqli_query($con,"INSERT INTO files (1, 2, unwanted, 4)
VALUES ('$01', '$02', '' ,'$04')");
make it
mysqli_query($con,"INSERT INTO files (1, 2, 4)
VALUES ('$01', '$02' ,'$04')");
or Add a variable which is null,
<?php
$null = '';
VALUES ('$null','BUF','$null','ROLB','darryl TALLEY','Face=0xad','#56','31','44','50','38','44','63','25','25');
?>
I have made the following assumptions base on your current post. I assume you want to add the score of a football player to the same table as the fixed data from each player. I would not recommend this because you will continue to add a new column every time you like to add a score.
I have made the following example for you how you can make a score table and player table that will separates the fixed data like name of the player, picture, etc from the scores.
Try this:
SQL query:
SELECT players.name, group_concat(score.score)
FROM players
JOIN score
ON players.id = score.id_players
GROUP BY players.id;
CREATE TABLE players (
id int auto_increment primary key,
name varchar(30)
);
CREATE TABLE score (
id int auto_increment primary key,
score int,
id_players int
);
INSERT INTO players (name)
VALUES ('QB BILLS'), ('frank REICH');
INSERT INTO score (id_players, score)
VALUES (1,25),(1,69), (1,13),(1,13),(1,56),(1,81),(1,81),
(1,81),(1,3),(1,12),(1,3), (2,25),(2,69),(2,13),(2,13),(2,31),(2,44),(2,25),(2,50),(2,3),(2,3),(2,2);
SQLFiddle demo

Fetch record from MySQL Table based on comma seperated value in column

I have fetching record from table named Pizza_Table.
Pizza_table Schema
id | pizza name | pizza_topping_ids
1 | pizza1 |1,2,3
2 | pizza2 |2,3
3 | pizza3 |4,5,6
Actually my functionality is searching pizza based on pizza_topping_ids
I have work allot in this searching. but I can't find proper solution. .
I can't use IN clause for fetching record.
I have tried out :
1)
SELECT *
FROM `pizza_table`
WHERE `pizza_topping_id` REGEXP '[[:<:]]2[[:>:]]'
Problem : fetch all record having value 2
2)
SELECT *
FROM `pizza_table`
WHERE `pizza_topping_id` REGEXP '[[:<:]]1,2,3,4[[:>:]]'
but MySQL returned an empty result set,
If comma separated values are not match to any record then return result which is most having value in topping_id column.
EX: If i have run above query for find out pizza having 1,2,3,4 topping_id, but In database no pizza exist having such topping_id combination then result will be most covered id should be displayed like 'pizza1' having pizza_topping_id 1,2,3.
Sorry for bad English as well as format, but try to understand my problem.
Thanks in advance.
Please help me.
Just try this-
SELECT * FROM `pizza_table` WHERE FIND_IN_SET(2,pizza_topping_id)
OR REGEXP
SELECT * FROM `pizza_table` WHERE pizza_topping_id REGEXP '^2,|,2$|,2,' OR pizza_topping_id =2
Please see the demo : demo
RegEx is slow on very big data sets.
MySQL has a FIND_IN_SET(value, column).
This may not answer your question directly but just only a suggestion :D
First, the rows on table pizza_topping doesn't need to have values separated as commas. It's hard to search for values like this using the current design. The values should be stored in rows not in column.
Consider this following schema,
CREATE TABLE PIZZA
(
PizzaID INT PRIMARY KEY,
NAME VARCHAR(50) UNIQUE,
PRICE DECIMAL(10,2)
);
INSERT INTO PIZZA VALUES (1,'Sunny Side Up Pizza', 120);
INSERT INTO PIZZA VALUES (2,'BBQ Chicken Pizza', 200);
INSERT INTO PIZZA VALUES (3,'Muffuletta Pizza', 175);
INSERT INTO PIZZA VALUES (4,'Caramelized Onion Pizza', 135);
INSERT INTO PIZZA VALUES (5,'Broccoli Deep Dish Pizza', 150);
CREATE TABLE TOPPINGS
(
ToppingID INT PRIMARY KEY,
NAME VARCHAR(50) UNIQUE
);
INSERT INTO TOPPINGS VALUES (1,'Pepperoni');
INSERT INTO TOPPINGS VALUES (2,'Mushroom');
INSERT INTO TOPPINGS VALUES (3,'Sausage');
INSERT INTO TOPPINGS VALUES (4,'Cheese');
INSERT INTO TOPPINGS VALUES (5,'Garlic');
INSERT INTO TOPPINGS VALUES (6,'Ham');
INSERT INTO TOPPINGS VALUES (7,'Tomato Sauce');
And the the records for PIZZA_TOPPING should have multiple rows of ToppingID for each PizzaID.
CREATE TABLE PIZZA_TOPPINGS
(
PizzaID INT,
ToppingID INT,
CONSTRAINT tb_fk1 FOREIGN KEY (PizzaID)
REFERENCES Pizza(PizzaID),
CONSTRAINT tb_fk2 FOREIGN KEY (ToppingID)
REFERENCES TOPPINGS(ToppingID),
CONSTRAINT tb_UQ UNIQUE (PizzaID, ToppingID)
);
INSERT INTO PIZZA_TOPPINGS VALUES (1,1);
INSERT INTO PIZZA_TOPPINGS VALUES (1,2);
INSERT INTO PIZZA_TOPPINGS VALUES (1,3);
INSERT INTO PIZZA_TOPPINGS VALUES (2,4);
INSERT INTO PIZZA_TOPPINGS VALUES (2,5);
INSERT INTO PIZZA_TOPPINGS VALUES (2,6);
INSERT INTO PIZZA_TOPPINGS VALUES (2,7);
INSERT INTO PIZZA_TOPPINGS VALUES (3,1);
INSERT INTO PIZZA_TOPPINGS VALUES (3,3);
INSERT INTO PIZZA_TOPPINGS VALUES (3,5);
INSERT INTO PIZZA_TOPPINGS VALUES (4,2);
INSERT INTO PIZZA_TOPPINGS VALUES (5,1);
INSERT INTO PIZZA_TOPPINGS VALUES (6,7);
INSERT INTO PIZZA_TOPPINGS VALUES (6,1);
The technique of searching records like this is called Relational Division
For example, you want to search for pizza's that have ingredients of: Pepperoni, Mushroom, Sausage.
SELECT a.PIZZAID, a.NAME, a.PRICE
FROM Pizza a
INNER JOIN Pizza_Toppings b
ON a.PizzaID = b.PizzaID
INNER JOIN Toppings c
ON b.ToppingID = c.ToppingID
WHERE c.Name IN ('Pepperoni', 'Mushroom', 'Sausage')
GROUP BY a.PIZZAID, a.NAME, a.PRICE
HAVING COUNT(*) = 3
SQLFiddle Demo
or pizza's that constains of atleast: Pepperoni, Mushroom,
SELECT a.PIZZAID, a.NAME, a.PRICE
FROM Pizza a
INNER JOIN Pizza_Toppings b
ON a.PizzaID = b.PizzaID
INNER JOIN Toppings c
ON b.ToppingID = c.ToppingID
WHERE c.Name IN ('Pepperoni', 'Mushroom')
GROUP BY a.PIZZAID, a.NAME, a.PRICE
HAVING COUNT(*) = 2
SQLFiddle Demo
Theses are much better than using any other functions like FIND_IN_SET, REGEXP, etc..
Try find_in_set of mysql useful in this kind of structures
$id = 2;
$query = $this->db->query("
SELECT *
FROM `pizza_table`
WHERE FIND_IN_SET($id,pizza_topping_id)";
return $query->result();