Joining DISTINCT values from one table to Another - sql-server-2008

I have 7 Tables with the following structures:
tbl_Trucks | tbl_Driver | tbl_Clients
-----------------------|-----------------------------|--------------------
tr_ID - int | dr_ID - int | cl_ID - int
tr_Name - varchar(50) | dr_LName - varchar(50) | cl_Name - varchar(50)
| dr_FName - varchar(50) |
| dr_MName - varchar(50) |
tbl_ExpenseHead | tbl_ExpenseDiesels
---------------------------------|---------------------------
eh_ID - int | dsl_ID - int
eh_DateAdded - date | dsl_amt - float
eh_RouteStart - varchar(50) | dsl_Ltrs - float
eh_RouteEnd - varchar(50) | eh_ID - int
cl_ID - int |
dr_ID - int |
tr_ID - int |
eh_Status - varchar(50) |
eh_ERnumber - varchar(50) |
eh_InvoiceNumber - varchar(50) |
tbl_ExpenseTotal | tbl_Helpers
---------------------------------|---------------------------
tot_ID - int | help_ID - int
tot_OverallExpense- date | help_FName - varchar(50)
eh_ID - int | help_MName - varchar(50)
| help_LName - varchar(50)
| eh_ID - int
currently have this query
SELECT
h.eh_DateAdded as [TRIP_DATE],
t.tr_Name as [TRUCK_NAME],
d.dr_LName + ', ' + d.dr_FName + ' ' + d.dr_MName as DRIVER,
c.cl_Name as CLIENT,
h.eh_RouteStart + ' to ' + h.eh_RouteEnd as TRIP,
h.eh_InvoiceNumber as [INVOICE_NUMBER],
h.eh_ERnumber as [ER_NUMBER],
SUM(dsl.dsl_amt) as [DIESEL_AMOUNT],
SUM(dsl.dsl_Ltrs) as [DIESEL_LITERS],
tot.tot_OverallExpense as EXPENSE
FROM tbl_ExpenseHead h INNER JOIN
tbl_Trucks t ON h.tr_ID = t.tr_ID INNER JOIN
tbl_Driver d ON h.dr_ID = d.dr_ID INNER JOIN
tbl_Clients c ON h.cl_ID = c.cl_ID INNER JOIN
tbl_ExpDiesels dsl ON h.eh_ID = dsl.eh_ID INNER JOIN
tbl_ExpenseTotal tot ON h.eh_ID = tot.eh_ID
WHERE h.eh_Status = 'APPROVED'
GROUP BY
h.eh_DateAdded,
t.tr_Name,
d.dr_LName,
d.dr_FName,
d.dr_MName,
c.cl_Name,
h.eh_RouteStart,
h.eh_RouteEnd,
h.eh_InvoiceNumber,
h.eh_ERnumber,
dsl.dsl_amt,
dsl.dsl_Ltrs,
tot.tot_OverallExpense
that outputs a table like this
as you can see, the helpers are not in the table. Given that i have tbl_Helpers populated with values like this
i want the first table to turn out something like this
i want to show the helpers for each line and if it happens to have less than 2 helpers then it would be just left blank.. or NULL. I'm trying some codes here and i'm wondering if there's quick way around this. Helpers won't exceed to 2 since i restrict the user from doing so.
I tried using DISTINCT in tbl_Helpers to get each different helpers and filter them with their eh_ID's but i don't know how to get them attached to make it turn out like the third table

Your first problem is you can't decide which helper should be #1 and which should be #2. So first let's assign them a number.
Next, let's join them to your ExpenseHead:
WITH myHelpers AS (
SELECT eh_ID
, help_ID
, RANK() OVER (PARTITION BY eh_IDORDER BY help_ID) helperOrder
FROM tbl_Helpers
)
SELECT h.eh_ID
, hlp1.help_ID AS help1_ID
, hlp2.help_ID AS help2_ID
FROM tbl_ExpenseHead h LEFT JOIN
myHelpers hlp2 ON h.eh_ID = hlp2.eh_ID AND hlp2.helperOrder = 2 LEFT JOIN
myHelpers hlp1 ON h.eh_ID = hlp1.eh_ID AND hlp1.helperOrder = 1 AND hlp2.help_ID IS NOT NULL
Think of the WITH-statement as a temporary table which exists for the duration of the query, it's called a CTE.
Notice the use of LEFT JOIN since we don't know whether there are 2 helpers.
Notice that I only select hlp1 in case hlp2.help_ID IS NOT NULL, it's either 2 helpers or none.
Based on your example, I think you'll be able to apply this to your query.
Good luck!

Related

Is this query optimised? - Mutual follows

Is this query optimised?
I'm trying to get the list of people personA follows who follow personB.
Few thousand of rows right now in the table but growing fast.
Want to make sure the query is performant enough for mysql.
Query:
select
*
from
(
select
*
from
creator_followers cf
where
cf.follower_user_id = 'personA'
and cf.current_active = 1
and cf.current_following = 1
) as fo
join creator_followers cf
where
fo.creator_user_id = cf.follower_user_id
and cf.creator_user_id = 'personB'
and cf.current_following = 1
and cf.current_active = 1
order by
cf.created_at desc
limit
10 offset 0;
Schema:
create table social.creator_followers
(
creator_user_id varchar(16) charset utf8 null,
follower_user_id varchar(16) charset utf8 null,
current_following bit null,
current_active bit null,
created_at bigint null,
id bigint auto_increment
primary key
)
It seems that it must be
SELECT *
FROM creator_followers cf1
join creator_followers cf2 ON cf1.creator_user_id = cf2.follower_user_id
where cf1.follower_user_id = 'personA'
and cf2.creator_user_id = 'personB'
and cf1.current_active = 1
and cf1.current_following = 1
and cf2.current_following = 1
and cf2.current_active = 1
order by cf2.created_at desc
limit 10 offset 0;
Based on a Comment, this might provide another speedup. Replace
id bigint auto_increment
primary key
with
PRIMARY KEY(creator_user_id, follower_user_id)

MySQL select data on the basis of source type

I am working on MySQL. I have a table in which there are some records. Below is my table
CREATE TABLE `mdc_meters_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`msn` varchar(100) DEFAULT NULL,
`kwh_t` varchar(100) DEFAULT NULL,
`data_date_time` datetime DEFAULT NULL,
`s_type` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=52702 DEFAULT CHARSET=latin1;
/*Data for the table `mdc_meters_data` */
insert into `mdc_meters_data`(`id`,`msn`,`kwh_t`,`data_date_time`,`s_type`) values(49641,'4A60193390662','2068.3','2020-11-01 00:02:17','WAPDA'),
(49642,'00209701','1476.59','2020-11-01 00:02:47','Sync Meter'),(49643,'00209702','1389.79','2020-11-01 00:03:17','Sync Meter'),(49644,'4A60193390662','2068.3','2020-11-01 00:04:57','WAPDA'),(49645,'00209701','1476.6','2020-11-01 00:05:28','Sync Meter'),(49646,'00209702','1389.81','2020-11-01 00:05:58','Sync Meter'),(49647,'4A60193390662','2068.3','2020-11-01 00:07:38','WAPDA'),(49648,'00209701','1476.6','2020-11-01 00:08:08','Sync Meter'),(49649,'00209702','1389.81','2020-11-01 00:08:38','Sync Meter'),(49650,'4A60193390662','2068.3','2020-11-01 00:10:19','WAPDA'),(49651,'00209701','1476.6','2020-11-01 00:10:49','Sync Meter'),(49652,'00209702','1389.82','2020-11-01 00:11:19','Sync Meter'),(49653,'4A60193390662','2068.3','2020-11-01 00:12:59','Generator'),(49654,'00209701','1476.61','2020-11-01 00:13:30','Sync Meter'),(49655,'00209702','1389.83','2020-11-01 00:14:00','Sync Meter'),(49656,'4A60193390662','2068.3','2020-11-01 00:15:40','Generator'),(49657,'00209701','1476.61','2020-11-01 00:16:10','Sync Meter'),(49658,'00209702','1389.84','2020-11-01 00:16:40','Sync Meter'),(49659,'4A60193390662','2068.3','2020-11-01 00:18:20','Generator'),(49660,'00209701','1476.61','2020-11-01 00:18:51','Sync Meter'),(49661,'00209702','1389.84','2020-11-01 00:19:21','Sync Meter'),(49662,'4A60193390662','2068.3','2020-11-01 00:21:01','Generator'),(49663,'00209701','1476.61','2020-11-01 00:21:31','Sync Meter'),(49664,'00209702','1389.85','2020-11-01 00:22:01','Sync Meter'),(49665,'4A60193390662','2068.3','2020-11-01 00:23:42','WAPDA'),(49666,'00209701','1476.62','2020-11-01 00:24:12','Sync Meter'),(49667,'00209702','1389.86','2020-11-01 00:24:42','Sync Meter'),(49668,'4A60193390662','2068.3','2020-11-01 00:26:22','WAPDA'),(49669,'00209701','1476.63','2020-11-01 00:26:53','Sync Meter'),(49670,'00209702','1389.88','2020-11-01 00:27:23','Sync Meter'),(49671,'4A60193390662','2068.3','2020-11-01 00:29:03','WAPDA'),(49672,'00209701','1476.63','2020-11-01 00:29:33','Sync Meter'),(49673,'00209702','1389.88','2020-11-01 00:30:03','Sync Meter'),(49674,'4A60193390662','2068.3','2020-11-01 00:31:44','WAPDA');
Same is in SQL Fiddle
What I have done
I am able to carry out the start and end date time of a source named WAPDA and in that time I have carried of the MAX value of kwh_t. I want to check it for every hour in 24 hours span. So I have managed the query like that way.
SELECT
msn,
MAX(kwh_t),
MIN(data_date_time),
MAX(data_date_time)
FROM mdc_meters_data
WHERE s_type = 'WAPDA'
AND data_date_time >= DATE '2020-11-01'
AND data_date_time < DATE '2020-11-02'
GROUP BY msn, DATE(data_date_time), HOUR(data_date_time)
ORDER BY msn, DATE(data_date_time), HOUR(data_date_time);
The above query gives me
msn | MAX(kwh_t)| MIN(data_date_time) | MAX(data_date_time)
=======================================================================
4A60193390662| 2068.3 | 2020-11-01T00:02:17Z | 2020-11-01T00:31:44Z
What I want?
The above result is not correct as seen in Fiddle at 2020-11-01T00:02:17Z the s_type is WAPDA and at 2020-11-01T00:12:59Z the s_type is Generator. Then again at 2020-11-01T00:23:42Z the s_type is again WAPDA and so on. I want to set my query in a way that it will give proper information according to the s_type like below
For WAPDA
msn | MAX(kwh_t)| MIN(data_date_time) | MAX(data_date_time)
=======================================================================
4A60193390662| 2068.3 | 2020-11-01T00:02:17Z | 2020-11-01T00:10:19Z
4A60193390662| 2068.3 | 2020-11-01T00:23:42Z | 2020-11-01T00:31:44Z
For Generator
msn | MAX(kwh_t)| MIN(data_date_time) | MAX(data_date_time)
=======================================================================
4A60193390663| 1000.3 | 2020-11-01T00:12:59Z | 2020-11-01T00:21:01Z
As there is no record for the s_type = WAPDA after 2020-11-01T00:10:19Z and before 2020-11-01T00:22:01Z so the query must start from the value of that particular s_type from where it records began. Same is applied for s_type=Generator
How to achieve it?
Any help would be highly appreciated
This is a gaps and islands problem. To solve your problem, you need to also group your readings according to the s_type value, so that (in your sample data) you extract two distinct groups of WAPDA values (separated by the Generator values). Basically you need to keep an overall row number as well as a row number for each island (so counting restarts whenever s_type changes). Subtracting the latter from the former gives you a constant number for each island, on which you can then group.
This is a tricky problem to solve in MySQL 5.x because of the lack of the ROW_NUMBER function, however that functionality can be emulated using variables. This query should give the results you want:
SELECT msn,
s_type,
MAX(kwh_t) AS max_kwh,
MIN(data_date_time) AS min_date_time,
MAX(data_date_time) AS max_date_time
FROM (
SELECT md.*,
#rn := #rn + 1 AS rn,
#rst := CASE
WHEN #st = s_type THEN #rst + 1
WHEN #st := s_type THEN 1
ELSE 1
END AS rst
FROM (
SELECT *
FROM mdc_meters_data
WHERE s_type != 'Sync Meter'
AND data_date_time >= '2020-11-01'
AND data_date_time < '2020-11-02'
ORDER BY data_date_time
) md
CROSS JOIN (SELECT #rn := 0, #rst := 0, #st := '') init
) m
WHERE s_type = 'WAPDA'
GROUP BY msn, rn - rst, DATE(data_date_time), HOUR(data_date_time)
ORDER BY msn, min_date_time
Output (for your sample data):
msn s_type max_kwh min_date_time max_date_time
4A60193390662 WAPDA 2068.3 2020-11-01 00:02:17 2020-11-01 00:10:19
4A60193390662 WAPDA 2068.3 2020-11-01 00:23:42 2020-11-01 00:31:44
Demo (also showing results for s_type = 'Generator') on dbfiddle.

SELECT count(*) with where produces weird values

It seems when I am trying to make a query to get the users transaction sum, it does not return the proper value until I remove the filter on code, what is even more interesting is the filter on the bar code following works perfectly fine, it seems maybe there is an inconsistency between both the subqueries in the WHERE clause?
Explanation:
With the below query, when I remove the and code != "foo" AND code !="foobar" from the query, it returns the correct value, but I also tried changing it to code = "foo" or code = "foobar" to check if any of the results had these codes, and it returns null when I do this.
SELECT SUM(t.amount)
FROM transactions t
WHERE `t`.`deleted_at` IS NULL
AND `t`.`user_id` = 80
AND `t`.`user_id` IS NOT NULL
AND `manually_deleted_at` IS NULL
AND
(SELECT count(*)
FROM `transaction_subcategories` s
WHERE `t`.`transaction_subcategory_id` = `s`.`id`
AND `code` != "foo"
AND `code` != "foobar"
AND
(SELECT count(*)
FROM `transaction_categories` c
INNER JOIN `transaction_categories_transaction_subcategories` sc ON `c`.`id` = `sc`.`transaction_category_id`
WHERE `sc`.`transaction_subcategory_id` = `s`.`id`
AND `code` = "bar") >= 1) >= 1
AND `posted_date` BETWEEN "2016-04-01 00:00:00.000000" AND "2017-03-31 23:59:59.000000"
AND `parent_id` = 0;
While I do realize this is a mysql query issue, the laravel ORM code is a bit cleaner:
$income_transactions = \Auth::user ()->transactions ()
->notManuallyDeleted()
->whereHas('transactionSubcategory', function ($query) {
$query
->where('code', '!=', 'foo')
->where('code', '!=', 'foobar')
->whereHas('transactionCategories', function ($query2){
$query2->where('code', '=', 'bar');
});
})
->whereBetween ( 'posted_date', [$from,$to])
->where('parent_id', '=', 0)
->get ();
Update
Not sure if this helps, but I did a query to see which subcategories it is returning with the WHERE clause and it returns only ones where the code is NULL
mysql> SELECT * FROM transaction_subcategories WHERE id in ('1125', '630', '1395') AND code is null;
+------+-----------------------------------+------+---------+
| id | name | code | user_id |
+------+-----------------------------------+------+---------+
| 630 | foo | NULL | 80 |
| 1125 | foo | NULL | 80 |
| 1395 | foo | NULL | 80 |
+------+-----------------------------------+------+---------+
Update 2 Turns out it is the WHERE doing it, it seems that NULL values won't be compared against a string, in other words if I do a WHERE on a column that has nulls, the null value rows will disappear
Answer Turns out adding OR code is null in a group in the WHERE clause was all I needed. Closed :)

Converting access SQL to MYSQL, How to If(isnull(Max( a column on Insert

beginner here. I'm converting Access sql to MYSQL so I can run bash files and I ran into this 1 issue where 3 days of web searching as lead me no where.
I have a table with two primary fields, "SalesOrderId" and "SOItemID" So the table may look like this:
+--------------+----------+--------+
| SalesOrderid | SOItemId | PartId |
+--------------+----------+--------+
| 10001 | 1 | 147 |
| 10002 | 1 | 152 |
| 10003 | 1 | 152 |
| 10003 | 2 | 188 |
| 10004 | 1 | 105 |
| 10004 | 2 | 84 |
| 10004 | 3 | 209 |
| 10005 | 1 | 5 |
+--------------+----------+--------+
On insert, i need to check if the SalesOrderId exists and if so, +1 the SOItemId field and insert the new record. If not then insert SOItemId as 1. So if I were to insert another PartId to SalesOrderId# 10004, it'd insert as (10004, 4, 299) Here is the code in Access SQL that currently works.
SQL Code:
INSERT INTO SOItem ( SalesOrderId, SOItemId, PartId, CustDeliv, OnDate, Qty, UnitAmount, WebOrderProductId )
SELECT SalesOrder.SalesOrderId, If(IsNull(Max(`SOItemId`,"SOItem","SalesOrderId= " & [SalesOrderId] & " ")),1,DMax("[SOItemId]","SOItem","SalesOrderId= " & [SalesOrderId] & " ")+1) AS Expr1, ICS_Web_Parts_Link.PartId, SalesOrder.Date, SalesOrder.Date, order_product.quantity, order_product.price, order_product.order_product_id
FROM (order_product INNER JOIN ICS_Web_Parts_Link ON order_product.model = ICS_Web_Parts_Link.Model) INNER JOIN SalesOrder ON order_product.order_id = SalesOrder.WebOrderId;
Here's my MYSQL version:
INSERT INTO `SOItem` ( SalesOrderId, SOItemId, PartId, CustDeliv, OnDate, Qty, UnitAmount, WebOrderProductId )
SELECT `SalesOrder`.`SalesOrderId`, If(IsNull(Max(`SOItem`.`SOItemId`, `SOItemId`.`SalesOrderId` = `SalesOrder`.`SalesOrderId`)),1,Max(`SOItem`.`SOItemId`, `SOItemId`.`SalesOrderId` = SalesOrder`.`SalesOrderId`)+1) AS Expr1, ICS_Web_Parts_Link.PartId, SalesOrder.Date, SalesOrder.Date, order_product.quantity, order_product.price, order_product.order_product_id
FROM (order_product INNER JOIN ICS_Web_Parts_Link ON order_product.model = ICS_Web_Parts_Link.Model) INNER JOIN SalesOrder ON order_product.order_id = SalesOrder.WebOrderId;
I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' SOItemId.SalesOrderId = SalesOrder.SalesOrderId)),1,Max(SOItem.`SOIte' at line 2
Any help would be greatly appreciated. -Dan
The syntax SOItemId.SalesOrderId implies that you're referencing a table called SOItemId, which has a column called SalesOrderId. That's clearly not what you're doing (there's no table with that name, at least not joined to this query), so that's why it's throwing a syntax error at you.
It's not clear exactly what you are trying to join to what. I suggest you spend some more time looking at SQL exercises and examples of working code. While you're at it, make sure you understand how MAX and ISNULL work in MySQL. (Consider using IFNULL or COALESCE instead.)
Here's how to do this in MySQL. This is just an example using what you referenced above but you can execute it as-is to see how it works and then apply it to your use-case.
DROP TABLE IF EXISTS sales_test;
CREATE TABLE `sales_test` (
`SalesOrderid` int(11) DEFAULT NULL,
`SOItemId` int(11) DEFAULT NULL,
`PartId` int(11) DEFAULT NULL,
PRIMARY KEY (SalesOrderid, SOItemId),
UNIQUE KEY `SalesOrderid` (`SalesOrderid`,`SOItemId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO sales_test (SalesOrderid, SOItemId, PartId) VALUES
(10001, 1, 147),
(10002, 1, 152),
(10003, 1, 152),
(10003, 2, 188),
(10004, 1, 105),
(10004, 2, 84),
(10004, 3, 209),
(10005, 1, 5);
SELECT * FROM sales_test;
INSERT INTO sales_test (SalesOrderid, SOItemId, PartId)
VALUES (10005, 1, 5)
ON DUPLICATE KEY UPDATE SOItemId = SOItemId + 1;
SELECT * FROM sales_test;
I was finally able to figure it out. Thanks for all your help!
To anybody with the same problem, here is the mysql code that works.
INSERT INTO `SOItem` ( SalesOrderId, SOItemId, PartId, CustDeliv, OnDate, Qty, UnitAmount, WebOrderProductId )
SELECT `SalesOrder`.`SalesOrderId`, IF((SELECT (SOItem.SOItemId) FROM d1.SOItem WHERE SalesOrder.SalesOrderId = SOItem.SalesOrderId) IS NULL, 1, (SELECT MAX(SOItem.SOItemId)+1 FROM d1.SOItem WHERE SalesOrder.SalesOrderId = SOItem.SalesOrderId)) AS Expr1, ICS_Web_Parts_Link.PartId, SalesOrder.Date, SalesOrder.Date, order_product.quantity, order_product.price, order_product.order_product_id
FROM d2.order_product INNER JOIN d1.ICS_Web_Parts_Link ON order_product.model = ICS_Web_Parts_Link.Model INNER JOIN d1.SalesOrder ON order_product.order_id = SalesOrder.WebOrderId LEFT JOIN d1.SOItem ON order_product.order_product_id = SOItem.WebOrderProductId
WHERE NOT EXISTS (SELECT order_product_id FROM d2.order_product WHERE order_product.order_product_id = SOItem.WebOrderProductId);

How to achieve default value if column value is NULL?

I want to retrieve some column values from table with these conditions.
If value is NULL (or) Empty String , return some user defined value
If not above condition , return it's value.
How can I figure it out ?
Here is my Table query..
CREATE TABLE AUCTION_CAR_BID(
bid_seq bigint NOT NULL AUTO_INCREMENT,
auction_car_seq bigint NOT NULL,
bid_group_seq bigint NOT NULL,
bid_price int DEFAULT 0 NOT NULL,
over_bid_price int DEFAULT -1 NOT NULL,
result_id int DEFAULT 0 NOT NULL,
remark varchar(500),
PRIMARY KEY (bid_seq))
ENGINE = InnoDB DEFAULT CHARACTER SET utf8;
Here is my efforted codes to get it..
SELECT
COALESCE(OVER_BID_PRICE, -1)
FROM
AUCTION_CAR_BID
WHERE
BID_SEQ = 2354435345;
Another :
SELECT
CASE
WHEN OVER_BID_PRICE IS NULL
OR TRIM(OVER_BID_PRICE) = '' THEN -1
ELSE OVER_BID_PRICE
END OVER_BID_PRICE
FROM
AUCTION_CAR_BID
WHERE
BID_SEQ = 2354435345;
But I always get empty String value(not -1) if given id is not in my table.
Any suggestions would be really appreciated !
If you write this:
SELECT
COALESCE(OVER_BID_PRICE, -1)
FROM
AUCTION_CAR_BID
WHERE
BID_SEQ = 2354435345;
The results can be two types.
First result: Your query no returns rows! Your WHERE condition is unsatisfact so you'll read NULL
Second result: Your query returns rows but the value of your field is NULL, your COALESCE works fine in this case
To resolve you can try this:
SELECT COALESCE(
(SELECT
COALESCE(OVER_BID_PRICE, -1)
FROM AUCTION_CAR_BID
WHERE BID_SEQ = 2354435345)
,-1);
Tell me if it's OK
How about this:
select
case when price is null or id <> 1
then -1
else price
end price
from mytable
DROP TABLE prices;
CREATE TABLE prices (price_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,price INT NULL);
INSERT INTO prices (price) VALUES (' '),(''),(NULL);
SELECT * FROM prices;
+----------+-------+
| price_id | price |
+----------+-------+
| 1 | 0 |
| 2 | 0 |
| 3 | NULL |
+----------+-------+
SELECT price_id,COALESCE(price,-1) price FROM prices;
+----------+-------+
| price_id | price |
+----------+-------+
| 1 | 0 |
| 2 | 0 |
| 3 | -1 |
+----------+-------+
If there's no row for USER_SEQ = 2354435345 in your table there's no row returned. But aggregate functions always return a row even if the result is empty :-)
SELECT
COALESCE(MIN(OVER_BID_PRICE), -1)
FROM
USER_PARAM
WHERE
USER_SEQ = 2354435345;