First of all sorry for putting like a code dump here... I'll try to explain it as clearly as possible, but given my inexperience with SQL (years and years without touching it) I think it is better if I post the two queries completely rather than asking a generic question.
I have two SQL queries that work well.
I need to mix them and I don't know how to do it.
The first one returns me all the details of the Invoice and the customer name, and the second one gives me the invoice cost after making the right selections and calculations (more on this after).
SELECT tInvoices.*, tCustomers.name FROM tInvoices INNERJOIN tCustomers ON CustomerId = tCustomers.id;
That way I get all the invoice details and the customer name in each row to show it in an HTML table.
But I also want to put the invoice price in that table and this is much more complicated:
I have a structure in which there is a reference price per customer, project and user... this means that depending on what's agreed with each customer for each project, each user will have a cost...
If I go to a customer company to program something I could get X€ and other guy from the company could get Y€. And those proces could be V€ and W€ for another project for the same customer...
Appart of that I've set in each task a special cost field. If that cost is filled (not NULL) then I'll use that cost as an exception (again something pacted with the customer).
All this said... this happens with the tasks (work done in a project by a human being) and with the expenses (hotel, gasoline, meals... tickets in general).
And the second query is not that simple, but seems to work well:
SELECT SUM(subTotal) AS total FROM
CASEWHEN tTasks.taskUseId = 1THEN
CASEWHEN price ISNULLTHEN
WHERE tReferencePricesForTasks.taskTypeId = tTasks.taskTypeId AND
tReferencePricesForTasks.projectId = tTasks.projectId AND
tReferencePricesForTasks.userId = tTasks.userId
ROUND(TIME_TO_SEC(TIMEDIFF(endTime, startTime)) / 3600,2)
LEFTJOIN tTasksTypes ON tTasks.taskTypeId = tTasksTypes.id
WHERE tTasks.taskUseId <> 3AND
tTasks.invoiceId = '.$PHP_Invoice_ID_VALUE.'UNIONALLSELECT
CASEWHEN price ISNULLTHEN
WHERE tReferencePricesForExpenses.expenseTypeId = tExpenses.expenseTypeId AND
tReferencePricesForExpenses.projectId = tExpenses.projectId AND
tReferencePricesForExpenses.userId = tExpenses.userId
LEFTJOIN tExpensesTypes ON tExpenses.expenseTypeId = tExpensesTypes.id
WHERE tExpenses.invoiceId = '.$PHP_Invoice_ID_VALUE.'
As you can see in my second query, I'm using $PHP_Invoice_ID_VALUE which is a PHP variable that I update in each row of the first query to get the Invoice ID.
I'd like to mix both queries, but I don't know how to do it.
The idea is not needing to call the second query in each row... that way everything should be faster and I would be able to apply filters and orders given the price too...
I would like to substitute the PHP variables for each invoice listed in the first query.
Using Mysql and MariaDB.
Sorry for the code dump, but I truly don't know how to ask it generically without missing any detail.
First of all, thank you very much for your answer!
I've seen the CTE concept in the Internet... it would make this a little bit easier... the problem is that PHPMyAdmin doesn't like the clause "WITH" and therefore I can't test it...
I'm thinking of storing the total price value into a new column of the invoice table and recalculate it after creating it or each time I modify it... It probably will be much faster when getting all the data and I can't see why it would be a bad design... but I'm super novice... Would it be something terrible?
And I'm curious... why would you look at the roundings?
I'm trying to avoid the need to do two queries while populating a table...
Would it be a bad design to add that totalInvoicePrice into the table tInvoices? that way at the moment of creating the invoice I would do what it's working now... and when I would need to get the data it would be extremely easy...
So I started my quest to learn Angular V6, and I have about a year into it now and doing quite well with it. I've made the move to Angular V7 and will go V8 pretty soon. I choose MongoDB for the database and I really like it, but have yet to do anything advanced with it. Well I just don't know the limitations of MongoDb since it's a NoSQL or document based database.
I'm going to add a store to my project so I can sell things. I've read the right way to do it is to use SQL Server for storing transactions because it's faster. Like run MongoDb to store product information and images, perhaps the cart; and use SQL Server to store the purchase transaction. But then their is FireBase and CosmoDB out there as well. I don't expect to sell much the first year, and I'm considering going Mongo all the way.
Just looking for opinions, help, guidance on this.
I really don't want to go back to SQL Server again.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
Well I just don't know the limitations of MongoDb since it's a NoSQL or document based database.
I've read the right way to do it is to use SQL Server for storing transactions because it's faster.
Faster in which case? Add a bunch of JOIN statements and heavy on index INSERT INTO queries and you will easily see how MongoDb performs better in most cases, and SQL Server slows down due to housekeeping.
The answer depends entirely on how you want to store the data, do you want to store the transactions as records and then pull out all from the tables one by one? Or do you want to have a single document of everything that a user has done in the system and be returned in a single go? I'll let you answer this.
With SQL Server—or any other relational database—your content is stored as a record, and you have to query the data using several JOIN clauses to prepare a single report. If you do not do this, then you are not following normalization techniques and are wasting money paid for relational features. In NoSQL—especially MongoDb, or other databases or same dialect—you store the data in a fashion that makes it easier to access, yes a bit slower on insert, but querying is better (this statement again of course can be debated upon).
FireBase and CosmoDB
Oh boy, don't read everything on the internet if you have to develop a product. Pick one and go with it!
I did it like this, and after spending some time with a couple of DBA's they couldn't improve on it:
;WITH cte AS
'.9.', '.09.') AS TREE
, cte2 AS
'.9.', '.09.') AS TREE
SELECT * FROM cte2;
Is there a better way?
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
Last Visit: 24-Nov-20 6:33 Last Update: 24-Nov-20 6:33