Count a value but below the given id in MYSQL - mysql

if user wants to fetch the total user_points by passing id 4 then the user_point which will be 4 because id 1,2,3,4 total user_point is 4
So if user wants to fetch the total_points by passing id 6 then the total user_point would be 6 because ids 1,2,3,4,5,6 total user_points is 6
CREATE TABLE IF NOT EXISTS `amount` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned DEFAULT NULL,
`user_point` smallint(1) DEFAULT '0',
`total` float(6,2) DEFAULT NULL,
`paid` float(6,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
INSERT INTO `amount` (`id`, `user_id`, `user_point`, `total`, `paid`) VALUES
(1, 29, 1, 44.00, 44.00),
(2, 29, 1, 125.4, 125.40),
(3, 29, 1, 95.00, 95.00),
(4, 29, 1, 44.00, 44.00),
(5, 29, 1, 94.00, 94.00),
(6, 29, 1, 50.00, 50.00);
Help me out please

select count(ID) as count from amount where user_id = <ID>
try this query

select sum(user_point) as tot_point from amount where user_id='29';

try this Query,
SELECT SUM(`user_point`) AS tot_points FROM `amount`
WHERE `user_id`= <userid>;

Try this:
SELECT COUNT(id) total FROM amount
WHERE user_id= <userid>;

Related

Mysql connect 3 tables with if case or maybe Join?

I have 3 tables and I have to connect them. The main table that consist all records for group trainings or individual trainings is called blocked_dates. The group trainings can be recognized if group_id > 0. If group_id = 0 than reservation_id shows that the reservation is for individual training..
I must get somehow the training id and the training date from either table **group_member_reservations ** or table reservations
Table blocked_dates
When blocked_dates.group_id > 0 the query must get id, training_date from table
`group_member_reservations for all records that have :
group_member_reservations.bdid = blocked_dates.id and
blocked_dates.blocked_date= group_member_reservations.training_date and
blocked_dates.group_id = group_member_reservations. group_id and
group_member_reservations.anulated = “0” and
group_member_reservations. paid_status = “not_paid”
The table that has all records for group trainings is called group_member_reservationsand has the following structure
When blocked_dates. reservation_id > 0 the query must get id, date from table reservations for all records that have:
reservations.id = blocked_dates.reservation_id and
reservations.status = “1” and
reservations.paid_status = “not_paid”
Here is the structure of table reservations
Can I request the needed information with one query. I hope I had explained correctly the desired results. Thanks for your time in advance
*** Here are the tables structure:
-- Table structure for table blocked_dates
DROP TABLE IF EXISTS `blocked_dates`;
CREATE TABLE `blocked_dates` (
`id` int(11) NOT NULL,
`customer_id` int(11) NOT NULL DEFAULT 0,
`group_id` int(11) NOT NULL DEFAULT 0,
`reservation_id` int(11) NOT NULL DEFAULT 0,
`blocked_date` date NOT NULL DEFAULT '0000-00-00',
`anulated` enum('0','1') CHARACTER SET utf8 NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Table structure for table group_members_reservations
DROP TABLE IF EXISTS `group_members_reservations`;
CREATE TABLE `group_members_reservations` (
`id` int(11) NOT NULL,
`bdid` int(11) NOT NULL DEFAULT 0,
`group_id` int(1) NOT NULL DEFAULT 0,
`training_date` date NOT NULL DEFAULT '0000-00-00',
`paid_status` enum('paid','not_paid') NOT NULL DEFAULT 'not_paid',
`anulated` enum('0','1') NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Table structure for table reservations
DROP TABLE IF EXISTS `reservations`;
CREATE TABLE `reservations` (
`id` int(11) NOT NULL,
`customer_id` int(11) NOT NULL,
`date` date NOT NULL DEFAULT '0000-00-00',
`status` int(1) NOT NULL DEFAULT 1 COMMENT '1-open;2-canceled_by_customer;3-canceled_by_admin',
`paid_status` enum('paid','not_paid') CHARACTER SET utf8 NOT NULL DEFAULT 'not_paid'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Insert statement
INSERT INTO `blocked_dates` VALUES(1, 2, 0, 1, '2021-08-01', '0');
INSERT INTO `blocked_dates` VALUES(2, 0, 1, 0, '2021-08-01', '0');
INSERT INTO `blocked_dates` VALUES(3, 0, 2, 0, '2021-08-01', '0');
INSERT INTO `blocked_dates` VALUES(4, 3, 0, 2, '2021-08-01', '0');
INSERT INTO `group_members_reservations` VALUES(1, 2, 1, '2021-08-01', 'not_paid', '0');
INSERT INTO `group_members_reservations` VALUES(2, 3, 2, '2021-08-01', 'not_paid', '0');
INSERT INTO `reservations` VALUES(1, 2, '2021-08-01', 1, 'not_paid');
INSERT INTO `reservations` VALUES(2, 3, '2021-08-01', 2, 'not_paid');
COMMIT;

how to calculate percentage and generate below result

I have a table schema
CREATE TABLE `charge` (
`charge_id` int(11) NOT NULL AUTO_INCREMENT,
`selling_date` date DEFAULT NULL,
`amount` decimal(10,2) DEFAULT NULL,
`is_deleted` tinyint(1) NOT NULL DEFAULT '0',
`charge_type_id` int(11) DEFAULT NULL,
PRIMARY KEY (`charge_id`)
);
CREATE TABLE `charge_type` (
`charge_type_id` int(11) NOT NULL AUTO_INCREMENT,
`charge_type` varchar(20) NOT NULL,
`is_deleted` tinyint(1) DEFAULT '0',
PRIMARY KEY (`charge_type_id`)
);
CREATE TABLE `charge_type_tax_list` (
`tax_type_id` int(11) NOT NULL,
`charge_type_id` int(11) NOT NULL,
PRIMARY KEY (`tax_type_id`,`charge_type_id`)
);
CREATE TABLE `tax_type` (
`tax_type_id` int(11) NOT NULL AUTO_INCREMENT,
`tax_type` varchar(35) NOT NULL,
`percentage` decimal(5,4) NOT NULL DEFAULT '0.0000',
`is_deleted` tinyint(1) DEFAULT '0',
PRIMARY KEY (`tax_type_id`)
);
INSERT INTO charge (
`selling_date`,
`amount`,
`is_deleted`,
`charge_type_id`
)
VALUES
("2013-12-01", 50, 0, 1),
("2013-12-01", 20, 0, 2),
("2013-12-02", 40, 0, 1),
("2013-12-02", 30, 0, 3),
("2013-12-02", 30, 1, 1),
("2013-12-03", 10, 0, 1);
INSERT INTO charge_type (
`charge_type_id`,
`charge_type`,
`is_deleted`
)
VALUES
(1, "room charge", 0),
(2, "snack charge", 0),
(3, "deleted charge", 1);
INSERT INTO charge_type_tax_list (
`tax_type_id`,
`charge_type_id`
)
VALUES
(1, 1),
(2, 1),
(3, 1),
(1, 2),
(1, 3);
INSERT INTO tax_type (
`tax_type_id`,
`tax_type` ,
`percentage`,
`is_deleted`
)
VALUES
(1, "GST", 0.05, 0),
(2, "HRT", 0.04, 0),
(3, "DELETED TAX", 0.10, 1);
I want you to produce a query that lists all charges and tax-calculated totals
With the given schema, your query should produce:
Selling Date Charge Type Amount Tax
2013-12-01 Room Charge 50.00 4.50
2013-12-01 Snack Charge 20.00 1.00
2013-12-02 Room Charge 40.00 3.60
2013-12-03 Room Charge 10.00 0.90
Do not display charges that are deleted (is_deleted is flagged)
Do not display charges that have charge_types that are deleted (is_deleted is flagged)
Do not consider tax_type that is deleted (is_deleted), but the containing charge_type should still be displayed)
Please help me I need it urgently.Thanks
Please read on MySQL Join Made Easy to understand how joins are used. Here is a query that should give the result you want by joining the tables on the common attributes.
SELECT A.`selling_date` AS `Selling Date`, B.`charge_type` AS `Charge Type`
, A.`amount` AS `Amount`, (A.`amount` * D.`percentage`) AS `Tax` FROM
`charge` A LEFT JOIN
`charge_type` B ON A.`charge_type_id`=B.`charge_type_id` INNER JOIN
`charge_type_tax_list` C ON B.`charge_type_id`=C.`charge_type_id`
INNER JOIN `tax_type` D ON C.`tax_type_id`=D.`tax_type_id`
WHERE
A.`is_deleted`=0 AND B.`is_deleted`=0 AND
D.`is_deleted`=0;
Here is an SQL FIDDLE DEMO
This query below will get the result that you want
select charge.selling_date as SellingDate,
charge.amount as Amount,
charge_type.charge_type as ChargeType,
sum(charge.amount * tax_type.percentage) as Tax
From charge
inner join charge_type on charge.charge_type_id=charge_type.charge_type_id,
tax_type
where charge.is_deleted=0 and charge_type.is_deleted=0 and tax_type.is_deleted=0
group by charge.selling_date, charge.amount,charge_type.charge_type

Else is not running in mysql Query

I have below query. In this I have yes and no case.
yes is accessing but else part is not working . Please have a look on this.
SELECT SalesChannel.name , count(Transaction.category_id) as count, (case when (Transaction.no_of_units > 0 and Transaction.mop > 0) THEN 'yes' ELSE 'No' END) AS Is_Present from outlets Outlet inner join transactions Transaction on Outlet.id = Transaction.outlet_id inner join sale_channels SalesChannel on SalesChannel.id = Outlet.sale_channel_id group by SalesChannel.name
the output should be as below
KU Electrical
Yes 6 2
No 1 2
6 is counter of KU and Yes refers the presence,similarly No is non presence of KU
select SalesChannel.name ,
Transaction.category_id,
count(Transaction.category_id) as count,
from outlets Outlet inner join transactions Transaction on Outlet.id = Transaction.outlet_id inner join sale_channels SalesChannel on SalesChannel.id = Outlet.sale_channel_id group by SalesChannel.name
below are three tables which i used
1. transactions
CREATE TABLE IF NOT EXISTS `transactions` (
`id` int(11) NOT NULL,
`zone_id` int(11) NOT NULL,
`state_id` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`sub_category_id` int(11) NOT NULL,
`brand_id` int(11) NOT NULL,
`model_id` int(11) NOT NULL,
`outlet_id` int(11) NOT NULL,
`no_of_units` int(11) NOT NULL,
`mop` decimal(10,2) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `transactions`
--
INSERT INTO `transactions` (`id`, `zone_id`, `state_id`, `city_id`, `category_id`, `sub_category_id`, `brand_id`, `model_id`, `outlet_id`, `no_of_units`, `mop`) VALUES
(1, 2, 2, 2, 2, 1, 1, 1, 1, 3, '6.00'),
(2, 2, 2, 2, 2, 1, 1, 1, 1, 3, '6.00'),
(3, 1, 1, 1, 1, 1, 1, 1, 1, 4, '2.00'),
(4, 2, 2, 2, 1, 1, 1, 1, 2, 4, '2.00');
2.outlets
CREATE TABLE IF NOT EXISTS `outlets` (
`id` int(11) NOT NULL,
`outlet_code` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`zone_id` int(11) NOT NULL,
`state_id` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
`sale_channel_id` int(11) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `outlets`
--
INSERT INTO `outlets` (`id`, `outlet_code`, `name`, `zone_id`, `state_id`, `city_id`, `sale_channel_id`, `is_active`, `created`, `modified`) VALUES
(1, '1508', 'Ashok electricals', 2, 2, 2, 1, 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00'),
(2, '1233', 'vinayak electricals', 1, 1, 1, 2, 1, '2016-10-04 00:00:00', '2016-10-04 00:00:00');
3. sale_chennals
CREATE TABLE IF NOT EXISTS `sale_channels` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `sale_channels`
--
INSERT INTO `sale_channels` (`id`, `name`, `is_active`, `created`, `modified`) VALUES
(1, 'KU', 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00'),
(2, 'Electricals', 1, '2016-10-04 00:00:00', '2016-10-04 00:00:00');
There is no data in tables that match for the else condition. Your condition is that "Transaction.no_of_units >0 AND Transaction.mop >0" that is not match in table value of both fields are greater than 0.
Otherwise, else condition works fine.
You are aggregating your data so as to get one row per SalesChannel.name. There may be some transaction records that result in 'Yes' and others in 'No' for a SalesChannel.name, so what then is Is_Present supposed to be?
Another issue with your query is that the sale channels are in a table. There are currently two of them, but there could be three or four or thousands sometime. A SQL query doesn't produce a result with a variable number of columns. The columns must be known beforehand. So a possible result could look like this:
Name Yes No
KU 6 1
Electrical 2 2
because you know you want it to be Yes or No only, no matter how many channels.
The query:
select
sc.name,
count(case when t.no_of_units > 0 and t.mop > 0 then 1 end) as yes,
count(case when t.no_of_units <= 0 or t.mop <= 0 then 1 end) as no
from sale_channels sc
join outlet o on o.sale_channel_id = sc.id
join transactions t on t.outlet_id = o.id;

showing counter in mysql query

I have mysql query given below. I which counter has been used. if i enter category Id 1 for 3 times then counter is coming 3 which is correct but with this i want if i do not enter then different coloumn should come with NO.
output should be
KU Electrical
Yes 6 2
No 1 2
In this KU and Electrical are my sale channel name. Yes means counter of enteries of KU and No means which have not entered. Please help out in this. i am struggling
select
SalesChannel.name,
Transaction.category_id,
count(Transaction.category_id) as "count"
from outlets Outlet
inner join transactions Transaction on Outlet.id = Transaction.outlet_id
inner join sale_channels SalesChannel on SalesChannel.id = Outlet.sale_channel_id
group by Transaction.category_id;
below are three tables which I used
1) transactions
CREATE TABLE IF NOT EXISTS `transactions` (
`id` int(11) NOT NULL,
`zone_id` int(11) NOT NULL,
`state_id` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`sub_category_id` int(11) NOT NULL,
`brand_id` int(11) NOT NULL,
`model_id` int(11) NOT NULL,
`outlet_id` int(11) NOT NULL,
`no_of_units` int(11) NOT NULL,
`mop` decimal(10,2) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `transactions`
--
INSERT INTO `transactions` (`id`, `zone_id`, `state_id`, `city_id`, `category_id`, `sub_category_id`, `brand_id`, `model_id`, `outlet_id`, `no_of_units`, `mop`) VALUES
(1, 2, 2, 2, 2, 1, 1, 1, 1, 3, '6.00'),
(2, 2, 2, 2, 2, 1, 1, 1, 1, 3, '6.00'),
(3, 1, 1, 1, 1, 1, 1, 1, 1, 4, '2.00'),
(4, 2, 2, 2, 1, 1, 1, 1, 2, 4, '2.00');
2) outlets
CREATE TABLE IF NOT EXISTS `outlets` (
`id` int(11) NOT NULL,
`outlet_code` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`zone_id` int(11) NOT NULL,
`state_id` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
`sale_channel_id` int(11) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `outlets`
--
INSERT INTO `outlets` (`id`, `outlet_code`, `name`, `zone_id`, `state_id`, `city_id`, `sale_channel_id`, `is_active`, `created`, `modified`) VALUES
(1, '1508', 'Ashok electricals', 2, 2, 2, 1, 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00'),
(2, '1233', 'vinayak electricals', 1, 1, 1, 2, 1, '2016-10-04 00:00:00', '2016-10-04 00:00:00');
3) sale_chennals
CREATE TABLE IF NOT EXISTS `sale_channels` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `sale_channels`
--
INSERT INTO `sale_channels` (`id`, `name`, `is_active`, `created`, `modified`) VALUES
(1, 'KU', 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00'),
(2, 'Electricals', 1, '2016-10-04 00:00:00', '2016-10-04 00:00:00');
SQL fiddle: http://sqlfiddle.com/#!9/3f497/1
You are grouping by category. That means you get one result row per category. In each of these rows you show the count and a sale channel name. This sale channel name is just one of the names found in the records for the category arbitrarily chosen.
I suppose you want to count per category and sale channel. Hence your group by clause should be group by SalesChannel.name, Transaction.category_id:
select
SalesChannel.name,
Transaction.category_id,
count(Transaction.category_id) as "count"
from outlets Outlet
inner join transactions Transaction on Outlet.id = Transaction.outlet_id
inner join sale_channels SalesChannel on SalesChannel.id = Outlet.sale_channel_id
group by SalesChannel.name, Transaction.category_id;
SQL fiddle: http://sqlfiddle.com/#!9/3f497/2
This result, however, doesn't show an entry for Electricals / category 2, because there is no transaction for this combination in the table. If you want to show a zero count for this, you'd have to create the complete result set (i.e. all combinations of channel and category, whether they have a transaction or not) first. Then you'd outer join the transactions:
select
sc.name,
c.id as category_id,
count(t.id) as "count"
from sale_channels sc
cross join categories c
left join outlets o on o.sale_channel_id = sc.id
left join transactions t on t.outlet_id = o.id and t.category_id = c.id
group by sc.name, c.id;
SQL fiddle: http://sqlfiddle.com/#!9/60e998/5

MySQL Query - How do I get a SUM with GROUP BY and WHERE condition and use LEFT OUTER JOIN?

I'm not sure how to get the result that I expect and I've tried everything. I also need to be able to get the expected results using one query.
Here are the two tables with some sample data:
--
-- Table structure for table `accounts`
--
CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL,
`name` varchar(200) NOT NULL,
`type_id` int(11) NOT NULL,
`description` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Data for table `accounts`
--
INSERT INTO `accounts` (`id`, `name`, `type_id`, `description`) VALUES
(100, 'DUES', 0, NULL),
(101, 'NET WEEKLY PAYROLL', 0, NULL),
(111, 'FEDERAL TAX DEPOSITS', 0, 'tax stuff'),
(113, 'UNITED ASSOCIATION PAYMENTS', 0, NULL),
(114, 'OFFICERS MEETING ALLOWANCES', 0, NULL);
--
-- Table structure for table `checks`
--
CREATE TABLE IF NOT EXISTS `checks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`batch_id` int(11) DEFAULT NULL,
`entry_date` date NOT NULL,
`account_id` int(11) NOT NULL,
`amount` decimal(10,2) NOT NULL,
`description` varchar(200) DEFAULT NULL,
`posted` tinyint(4) NOT NULL DEFAULT '0',
`vendor_id` int(11) DEFAULT NULL,
`check_num` int(11) NOT NULL,
`voided` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `checks`
--
INSERT INTO `checks` (`id`, `batch_id`, `entry_date`, `account_id`, `amount`, `description`, `posted`, `vendor_id`, `check_num`, `voided`) VALUES
(1, NULL, '2013-01-21', 111, '77.44', 'Last Year', 0, 1, 100, 0),
(2, NULL, '2014-01-21', 111, '521.11', 'Test Stuff', 0, 1, 101, 0),
(3, NULL, '2014-01-20', 101, '121.11', 'More Tests', 0, 1, 222, 0),
(4, NULL, '2014-01-02', 101, '150.00', 'test', 0, 4, 213, 0);
I want to create a list of all accounts with a month-to-date sum as an added field. Here is the query to get the month-to-date sum without joining the accounts table:
SELECT *, SUM(amount) as mtd FROM `checks` WHERE `entry_date` > '2014-01-01' GROUP BY `account_id`
... and here is what i used to get all the accounts joined to checks table:
SELECT * FROM `accounts` LEFT OUTER JOIN `checks` ON `checks.account_id` = `accounts.id`
I just can't seem to combine these two correctly to get the expected results. Please help!
I think this solves your problem:
SELECT a.*, SUM(c.amount) as mtd
FROM accounts a left outer join
checks c
ON (a.id = c.account_id) and c.entry_date >= '2014-01-01'
GROUP BY a.account_id;
This will return all accounts, even those with no activity in January. I changed the date condition to >=, because that "feels" better as a month-to-date cutoff.