|
It is.
The purpose of a CTE is to make the code more readable.
when the query is analyzed by the optimizer all CTEs are inlined.
|
|
|
|
|
First of all thank you very much for your answer!
PHPMyAdmin doesn't know the word "WITH"...
Can't even try it...
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...
|
|
|
|
|
Try inserting a semicolon just before WITH keyword:
;WITH ctePrices AS...
while (!(success = Try()));
|
|
|
|
|
Exactly the same...
|
|
|
|
|
That was a long shot; and I think it's MSSQL syntax anyway, so I'm afraid I was off-topic on this one. Sorry for that.
while (!(success = Try()));
|
|
|
|
|
The WITH clause appears in MySQL manuals... so it should definitely work... but it doesn't who knows why...
Thank you very much for trying it!
|
|
|
|
|
what!
no no no lets not start that one
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
|
|
|
Why not create a view in the database that includes the sum of the items, no need to update the value as it will calc every time you reference the view.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
I thought of using a view, but at the end I think it will even be better storing the value inside the same invoices table...
I don't need recalculating the value...
It is only calculated once the invoice is made.
Thank you very much for your answer!
|
|
|
|
|
Well, that's a problem with php, not mySQL.
|
|
|
|
|
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
|
|
|
|
|
Quote: Well I just don't know the limitations of MongoDb since it's a NoSQL or document based database. Yep, that's true, being NoSQL it provides a great amount of flexibility. But most of the flexibility is lost when you take it out of a JavaScript-based environment, such as Node.js. So, consider using it with Node.js web app and then see for yourself.
Quote: 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).
Quote: 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!
Read here how Pinterest used old school MySQL and scaled their hyperactive Pinterest service on the internet; https://medium.com/pinterest-engineering/sharding-pinterest-how-we-scaled-our-mysql-fleet-3f341e96ca6f. There did not break a sweat for SQL Server or MongoDb, or Neo4j or Apache Cassandra (food for thought! ), all they did was use the infrastructure they have, better optimize it and done.
Quote: I really don't want to go back to SQL Server again. Okay, how about Apache Cassandra?
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
You got me thinking now.
I'll continue using Mongo and try to come up with a solid compact design.
Thanks!
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Compress and fix access data in C#, I have to look for examples on google network but it's not running examples, how to compress and fix data in C# ?
|
|
|
|
|
I've just answered it in CG Forum.
Choose something from Google
|
|
|
|
|
Using SQL Server 2016 - I have a table with a TREE column (type is varchar ) with data such as the following:
1.0
1.0.1
1.0.2
1.0.2.1
1.0.2.2
1.10.1.35
0) The number of "octets" (values between the periods) is an unknown quantity (it could be anywhere from 2 to 10 levels deep).
2) The number of digits within a given octet will be at least one, but never more than 2.
Desired output: I want all single-digit octets to be 0-padded. So given the sample above, the output would look like this:
01.00
01.00.01
01.00.02
01.00.02.01
01.00.02.02
01.10.01.35
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
(
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE('.'+TREE+'.',
'.0.', '.00.'),
'.1.', '.01.'),
'.2.', '.02.'),
'.3.', '.03.'),
'.4.', '.04.'),
'.5.', '.05.'),
'.6.', '.06.'),
'.7.', '.07.'),
'.8.', '.08.'),
'.9.', '.09.') AS TREE
FROM MYTABLE
)
, cte2 AS
(
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE('.'+TREE+'.',
'.0.', '.00.'),
'.1.', '.01.'),
'.2.', '.02.'),
'.3.', '.03.'),
'.4.', '.04.'),
'.5.', '.05.'),
'.6.', '.06.'),
'.7.', '.07.'),
'.8.', '.08.'),
'.9.', '.09.') AS TREE
FROM cte
)
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
|
|
|
|
|
Depends what you mean by "better".
You're using SQL 2016, so STRING_AGG[^] is out - that was added in 2017.
But you can use STRING_SPLIT[^], TRY_PARSE[^], and FORMAT[^]. And there are ways[^] to concatenate row values in 2016 and earlier.
I've assumed a table without a primary key; if your table has one, use that instead of the generated ROW_NUMBER :
WITH cteRN As
(
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) As RN,
tree
FROM
MyTable
)
SELECT
STUFF(
(
SELECT '.' + IsNull(Format(Try_Parse(P.value As int), 'D2'), P.value)
FROM cteRN As T2
CROSS APPLY string_split(T2.tree, '.') As P
WHERE T2.RN = T.RN
FOR XML PATH(''), TYPE
).value('.', 'varchar(max)')
, 1, 1, '') As tree
FROM
cteRN As T
GROUP BY
T.RN
; It's not pretty, but it works:
01.00
01.00.01
01.00.02
01.00.02.01
01.00.02.02
01.10.01.35
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That is almost unreadable!
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
That's part of what makes it beautiful.
".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
|
|
|
|
|
I did say it wasn't pretty!
STRING_AGG would probably make it slightly better, but that needs SQL Server 2017.
And if the source table has a primary key already, you can ditch the CTE.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Wanna hear something funny?
Our dev/test boxes are on SQL 2008R2, but our production and pre-production databases are 2016. I don't have access to the prod DBs, so I can't play with the code. :/
".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
|
|
|
|
|
At least it's not the other way round.
You can play with it here: SQL Fiddle[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|