href link within email from ms sql server - sql-server
I am sending daily an email with order numbers and parcels together with tracking numbers, which looks like this:
Order | Parcel | Tracking url
00022 | 312597 | https://url/track/312597
00023 | 226825 | https://url/track/226825
00023 | 142532 | https://url/track/142532
00023 | 182576 | https://url/track/182576
00025 | 515869 | https://url/track/515869
00026 | 712368 | https://url/track/712368
I need to put the parcels together so they dont appear multiple times to achieve the email to look like this:
Order | Parcel (click for tracking)
00022 | 312597
00023 | 226825 / 142532 / 182576
00025 | 515869
00026 | 712368
After seaching and trying to make it work i have come up with following code:
(select t1.Order,
stuff((select distinct ' / ' + cast(Parcel as varchar(max))
from TABLE t2
where t2.Order = t1.Order for XML PATH('a')), 1 , 3, '') as Parcel
from TABLE t1
group by t1.Order)
resulting in / 06086075865478</a><a> / 06086075865479</a>
How can i implement a "Clickable link text" and / into code to get the result?
Related
Replacing placeholder with another table's data (without knowing in advance the substitutions)
I need to replace placeholders in a text, reading from a query matched to that specific message. Table Template_Messages | ID | String | Query | PICKUP_MSG|Your {vehicle_name} will be ready for pick-up on {pickup_date}|SELECT * FROM vehicles WHERE ID = ? If I take the query I find in the 'Query' column, I will find the following table: Table Vehicles | ID | vehicle_name | plate | pickup_date | ... | | P981| BMW X5 | AA014CC| 2022-09-20 | ... | | Z323| Ford Focus | HH000JJ| 2022-10-21 | ... | Then with the following query: SELECT * FROM vehicles WHERE ID = 'Z323' By making the appropriate substitutions I should obtain this output: Your Ford Focus will be ready for pick-up on 2022-10-21 How can I achieve this? And since the 'query' column of the first table does not only refer to the 'vehicles' table, can it work dynamically on any placeholder/query?
How to get data into a CTE and do an update
Using MS SQL Server, I want to: Get data from my application Perform preprocessing Update a table Steps 1 and 3 are giving me problems. Simplifying the problem to its essence, my existing data looks like: +--------+-------+ | Item | Usage | +--------+-------+ | Part A | 10 | | Part B | 15 | | Part C | 8 | +--------+-------+ and an example of the source data is: +--------+ | Item | +--------+ | Part A | | Part B | | Part B | | Part B | | Part A | | Part A | +--------+ My over all plan is to import the data into a CTE, do the preprocessing, then do an update. Regarding getting the data, since "INTO" is not allowed in a CTE, how can I get the source data into the CTE. Or some other approach not using a CTE better? Preprocessing is straightforward. Here is my SQL: WITH MyData (Item, NewUsage) AS ( <Somehow get the data> SELECT Item, Count(*) as NewUsage FROM Items GROUP BY Item ) UPDATE Items SET Usage = Usage + b.NewUsage FROM Items as a JOIN MyData as b ON a.Item = b.Item; The update is updating all the rows in Items by 1 instead of using the NewUsage column. How do I get my data (into the CTE?) and how to write the SQL so it works properly?
Create/Update table in MS Access dynamically
EDIT: Here's what I have: An Access database made up of 3 tables linked from SQL server. I need to create a new table in this database by querying the 3 source tables. Here are examples of the 3 tables I'm using: PlanTable1 +------+------+------+------+---------+---------+ | Key1 | Key2 | Key3 | Key4 | PName | MainKey | +------+------+------+------+---------+---------+ | 53 | 1 | 5 | -1 | Bikes | 536681 | | 53 | 99 | -1 | -1 | Drinks | 536682 | | 53 | 66 | 68 | -1 | Balls | 536683 | +------+------+------+------+---------+---------+ SpTable +----+---------+---------+ | ID | MainKey | SpName | +----+---------+---------+ | 10 | 536681 | Wing1 | | 11 | 536682 | Wing2 | | 12 | 536683 | Wing3 | +----+---------+---------+ LocTable +-------+-------------+--------------+ | LocID | CenterState | CenterCity | +--- ---+-------------+--------------+ | 10 | IN | Indianapolis | | 11 | OH | Columbus | | 12 | IL | Chicago | +-------+-------------+--------------+ You can see the relationships between the tables. The NewMasterTable I need to create based off of these will look something like this: NewMasterTable +-------+--------+-------------+------+--------------+-------+-------+-------+ | LocID | PName | CenterState | Key4 | CenterCity | Wing1 | Wing2 | Wing3 | +-------+--------+-------------+------+--------------+-------+-------+-------+ | 10 | Bikes | IN | -1 | Indianapolis | 1 | 0 | 0 | | 11 | Drinks | OH | -1 | Columbus | 0 | 1 | 0 | | 12 | Balls | IL | -1 | Chicago | 0 | 0 | 1 | +-------+--------+-------------+------+--------------+-------+-------+-------+ The hard part for me is making this new table dynamic. In the future, rows may be added to the source tables. I need my NewMasterTable to reflect any changes/additions to the source. How do I go about building the NewMasterTable as described? Does this make any sort of sense?
Since an Access table is a necessary requirement, then probably the only way to go about it is to create a set of Update and Insert queries that are executed periodically. There is no built-in "dynamic" feature of Access that will monitor and update the table. First, create the table. You could either 1) do this manually from scratch by defining the columns and constraints yourself, or 2) create a make-table query (i.e. SELECT... INTO) that generates most of the schema, then add any additional columns, edit necessary details and add appropriate indexes. Define and save Update and Insert (and optional Delete) queries to keep the table synced. I'm not sharing actual code here, because that goes beyond your primary issue I think and requires specifics that you need to define. Due to some ambiguity with your key values (the field names and sample data still are not sufficient to reveal precise relationships and constraints), it is likely that you'll need multiple Update statements. In particular, the "Wing" columns will likely require a transform statement. You may not be able to update all columns appropriately using a single query. I recommend not trying to force such an "artificial" requirement. Multiple queries can actually be easier to understand and maintain. In the event that you experience "query is not updateable" errors, you may need to define other "temporary" tables with appropriate indexes, into which you do initial inserts from the linked tables, then subsequent queries to update your master table from those. Finally, and I think this is the key to solving your problem, you need to define some Access form (or other code) that periodically runs your set of "sync" queries. Access forms have a [Timer Interval] property and corresponding Timer event that fires periodically. Add VBA code in the Form_Timer sub that runs all your queries. I would suggest "wrapping" such VBA in a transaction and adding appropriate error handling and error logging, etc.
How do you select from a reference table with exclusivity?
I've got two tables (threads and user_threads). Essentially, a thread is an object with a name, and then a user_thread links a user to a thread. This was to illustrate a many-to-many relationship. Given this setup, Im trying to figure out how to get threads between exclusively two users. Threads looks like this |------------------------| | id | name | | 1 | group1 | | 2 | test group | |------------------------| user_threads looks like this |---------------------------------| | id | user | thread | |---------------------------------| | 1 | 1 | 1 | | 2 | 2 | 1 | | 3 | 1 | 2 | | 4 | 2 | 2 | | 5 | 3 | 2 | |---------------------------------| So the issue that I'm running into is this - Given user 1 and user 2, I would like to return the mutual thread that is exclusive to them. Querying with 1 and 2 should return thread 1. I've tried using a self join and mixing exclude, but SQL is not in my primary skill set. Is there any way to do this or do I need to restructure my tables?
One way is to select the threads that have both users using a JOIN and then excluding all those that have other users in them also. SELECT ut1.thread FROM user_threads ut1 JOIN user_threads ut2 ON ut1.thread=ut2.thread WHERE ut1."user" = 1 AND ut2."user" = 2 AND NOT EXISTS (SELECT 1 FROM user_threads WHERE thread=ut1.thread AND "user" NOT IN (ut1."user", ut2."user")) SQL Fiddle
Trying to to do a Sum(MAX) calculation in SQL Server
Thanks for all the help that you all provided and though it was an eye opener unfortunately it did not produce the expected results I was looking for. In an effort to better get the help I'm looking for I will try to explain what I'm looking to achieve. I think the main columns of focus are "IN", "AA_Now", "STF_Now", "dbo.Sheet1$.LOB_name", "dbo.Sheet1$.LifeCycleName" and "dbo.Sheet1$.AreaOfBusiness". Each "IN" have an "AA_Now" and "STF_Now". A group of "IN" rolls up under "dbo.Sheet1$.LOB_name". Under "dbo.Sheet1$.LOB_name" I just want the max value of the Group of "IN" that is rolled up. Now "dbo.Sheet1$.LOB_name" is rolled up under "dbo.Sheet1$.LifeCycleName" and what I want is the sum of of the max values that are rolled up under "dbo.Sheet1$.LOB_name" to show in the rollup of "dbo.Sheet1$.LifeCycleName". Finally "dbo.Sheet1$.LifeCycleName" rolls up to "dbo.Sheet1$.AreaOfBusiness". As before what I'm looking for is the sum of "dbo.Sheet1$.LifeCycleName" to show. These are only for the columns of "AA_Now" and "STF_Now" I tried doing it from a Pivot table but to no avail and figured that it would be best to sort it out in the raw data. I'm trying to to do a SUM(MAX) calculation in SQL server and getting the follow error when executing the command Msg 130, Level 15, State 1, Line 6 Cannot perform an aggregate function on an expression containing an aggregate or a subquery. I'm sure the error is caused by both ,SUM(MAX(convert(float,replace([AA_Now], 'N/A','0')))) As [AA2_Now] and ,SUM(MAX(convert(float,replace([STF_Now], 'N/A','0')))) As [STF2_Now] but have no idea how to rewrite it without causing an error. Below is the full code. SELECT dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN ,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG ,dbo.CCA_Merged.regionId, dbo.CCA_Merged.sgcc ,convert(float,replace([SLC_Today],'N/A','0')) As [SLC_Today] ,convert(float,replace([AA_Now],'N/A','0')) As [AA_Now] ,SUM(MAX(convert(float,replace([AA_Now],'N/A','0')))) As [AA2_Now] ,convert(float,replace([SLCO_Today],'N/A','0')) As [SLCO_Today] ,convert(float,replace([CABN_Today],'N/A','0')) As [CABN_Today] ,convert(float,replace([COF_Today],'N/A','0')) As [COF_Today] ,convert(float,replace([HT_Today],'N/A','0')) As [HT_Today] ,convert(float,replace(replace([CH_Today],'N/A','0'),'-','0')) As [CH_Today] ,convert(float,replace([SLC_Now],'N/A','0')) As [SLC_Now] ,convert(float,replace([SLCO_Now],'N/A','0')) As [SLCO_Now] ,convert(float,replace([SLC_Thirty],'N/A','0')) As [SLC_Thirty] ,convert(float,replace(replace([SLCO_Thirty],'N/A','0'),'-','0')) As [SLCO_Thirty] ,convert(float,replace([ACWT_Today],'N/A','0')) As [ACWT_Today] ,convert(float,replace([CQ_Now],'N/A','0')) As [CQ_Now] ,convert(float,replace([LCQ_Now],'N/A','0')) As [LCQ_Now] ,convert(float,replace([SLCH_Now],'N/A','0')) As [SLCH_Now] ,convert(float,replace([STF_Now],'N/A','0')) As [STF_Now] ,SUM(MAX(convert(float,replace([STF_Now],'N/A','0')))) As [STF2_Now] ,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name FROM dbo.Sheet1$ RIGHT OUTER JOIN dbo.CCA_Merged ON dbo.Sheet1$.Skill_Name = dbo.CCA_Merged.lN Group by ROLLUP (stf_now) ,dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN ,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG ,dbo.CCA_Merged.regionId ,dbo.CCA_Merged.sgcc,AA_Now,SLC_Today,SLCO_Today,CABN_Today,COF_Today,HT_Today,CH_Today ,SLC_Now,SLCO_Now,SLC_Thirty,SLCO_Thirty,ACWT_Today,CQ_Now,LCQ_Now,SLCH_Now ,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name I'm relatively new with SQL Server and any help would be greatly appreciated. Thanks in advance Updated Stripped down script SELECT dbo.CCA_Merged.lN ,convert(float,replace([STF_Now],'N/A','0')) As [STF_Now] ,dbo.Sheet1$.LOB_name, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.AreaOfBusiness FROM dbo.Sheet1$ RIGHT OUTER JOIN dbo.CCA_Merged ON dbo.Sheet1$.Skill_Name = dbo.CCA_Merged.lN Group by stf_now ,AA_Now,dbo.CCA_Merged.lN,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name Order by AreaOfBusiness DESC +----+---------+----------+---------------+----------------+ | LN | STF_Now | LOB_name | LifeCycleName | AreaOfBusiness | +----+---------+----------+---------------+----------------+ | A | 46 | BSW | BS | Business | | B | 46 | BSW | BS | Business | | C | 0 | BOSS | BS | Business | | D | 112 | MSD | BS | Business | | E | 112 | MSD | BS | Business | | F | 42 | BHV | BR | Business | | G | 23 | BCR | BR | Business | | H | 23 | BHV | BR | Business | | I | 55 | BSW2 | BS | Business | | J | 1 | BSW2 | BS | Business | | K | 46 | BSW | BS | Business | | L | 112 | MSD | BS | Business | | M | 112 | MSD | BS | Business | | N | 57 | BSW | BS | Business | | O | 0 | BOSS | BS | Business | | P | 38 | MSD | BS | Business | | Q | 38 | MSD | BS | Business | | R | 19 | BHV | BR | Business | | S | 0 | BCR | BR | Business | | T | 19 | BHV | BR | Business | | U | 2 | BSW | BS | Business | | V | 1 | BSW | BS | Business | | W | 57 | BSW | BS | Business | | X | 38 | MSD | BS | Business | | Y | 38 | MSD | BS | Business | +----+---------+----------+---------------+----------------+ Below is the the expected results in 3 added columns LOB_Name2 (This is the Max of STF_Now resulting from LN) 57 BSW 0 BOSS 112 MSD 42 BHV 23 BCR 55 BSW2 LifeCycleName2 (This is the Sum of the Max of the Rollup of LOB_Name2) 224 BS 65 BR AreaOfBusiness2 (This is the Sum of the Rollup of LifeCycleName2) 289 Business
You can't sum a max because it would be the same amount anyhow, if you have the same group by. You probably need to have an inner and outer parts with different group by, something like: select product_group, sum(max_cost) from ( select product, product_group, max(cost) as max_cost from orders group by product_group,product ) X group by product_group This imaginary SQL will fetch maximum cost for each product, and them sum them up to the product group level. That's the only way I can figure out you'd actually need to sum a max
You're having issues because you're trying to do two nested aggregates. This will roughly give you what you're after, if SUM(MAX) is actually what you're trying to do. However, as James pointed out, You'll need some group by logic that pulls this together. SELECT ... , SUM(AA2_Now_Max) ... , SUM(STF2_Now_Max) FROM( SELECT dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN ,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG ,dbo.CCA_Merged.regionId, dbo.CCA_Merged.sgcc ,convert(float,replace([SLC_Today],'N/A','0')) As [SLC_Today] ,convert(float,replace([AA_Now],'N/A','0')) As [AA_Now] ,MAX(convert(float,replace([AA_Now],'N/A','0'))) As [AA2_Now_Max] ,convert(float,replace([SLCO_Today],'N/A','0')) As [SLCO_Today] ,convert(float,replace([CABN_Today],'N/A','0')) As [CABN_Today] ,convert(float,replace([COF_Today],'N/A','0')) As [COF_Today] ,convert(float,replace([HT_Today],'N/A','0')) As [HT_Today] ,convert(float,replace(replace([CH_Today],'N/A','0'),'-','0')) As [CH_Today] ,convert(float,replace([SLC_Now],'N/A','0')) As [SLC_Now] ,convert(float,replace([SLCO_Now],'N/A','0')) As [SLCO_Now] ,convert(float,replace([SLC_Thirty],'N/A','0')) As [SLC_Thirty] ,convert(float,replace(replace([SLCO_Thirty],'N/A','0'),'-','0')) As [SLCO_Thirty] ,convert(float,replace([ACWT_Today],'N/A','0')) As [ACWT_Today] ,convert(float,replace([CQ_Now],'N/A','0')) As [CQ_Now] ,convert(float,replace([LCQ_Now],'N/A','0')) As [LCQ_Now] ,convert(float,replace([SLCH_Now],'N/A','0')) As [SLCH_Now] ,convert(float,replace([STF_Now],'N/A','0')) As [STF_Now] ,MAX(convert(float,replace([STF_Now],'N/A','0'))) As [STF2_Now_Max] ,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name FROM dbo.Sheet1$ RIGHT OUTER JOIN dbo.CCA_Merged ON dbo.Sheet1$.Skill_Name = dbo.CCA_Merged.lN Group by ROLLUP (stf_now) ,dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN ,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG ,dbo.CCA_Merged.regionId ,dbo.CCA_Merged.sgcc,AA_Now,SLC_Today,SLCO_Today,CABN_Today,COF_Today,HT_Today,CH_Today ,SLC_Now,SLCO_Now,SLC_Thirty,SLCO_Thirty,ACWT_Today,CQ_Now,LCQ_Now,SLCH_Now ,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name ) x GROUP BY ... This other question is similar- follow the pattern! SQL: SUM the MAX values of results returned
You need to add another query level to SUM your MAX values. The idea would be to MAX in one select and then SUM the results using a outer query. I use AVG and MAX in the example below, however, any aggregate function could be used. SELECT LocationID, MaxAverageSalePriceByLocation=MAX(AvgerageSalePriceByUserLocation) FROM ( SELECT UserID, AvgerageSalePriceByUserLocation=AVG(SalePrice) FROM MyTable GROUP BY UserID,LocationID )AS A GROUP BY LocationID
If you want to sum all the rows Max values then use OVER() Sum(Max(CONVERT(FLOAT, Replace([STF_Now], 'N/A', '0'))))OVER() AS [STF2_Now] If you want to sum all the rows Max values for each group then use OVER(Partition by) Sum(Max(CONVERT(FLOAT, Replace([STF_Now], 'N/A', '0'))))OVER(partition by grp1,grp2,..) AS [STF2_Now] Note Converting the numeric data to float could lead to approximation issues.. Use Numeric with precision and scale