Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:02:01+00:00 2026-06-15T16:02:01+00:00

I have 2 queries: SELECT p.assetid, p.TagId, SUM(CASE WHEN p.isrepeat = 1 then 1

  • 0

I have 2 queries:

SELECT p.assetid, 
p.TagId, 
SUM(CASE WHEN p.isrepeat = 1  then 1 else 0 END) as 'Repeats',
SUM(CASE WHEN p.isrepeat = 0  then 1 else 0 END) as 'Non-Repeats',
CAST(SUM(CASE WHEN p.isrepeat =1 then 1 else 0 END) as DECIMAL)/COUNT(*)as 'Percent of Repeats'
from POSITION p
group by p.tagid, p.assetid
order by 1

and

SELECT p.AssetID, p.tagid, COUNT(*)
from POSITION p,
TEMP t
where t.beginning_X = p.X
and t.beginning_Y = p.y
and p.isrepeat = 1
and t.AssetID = p.AssetID
and t.Total_Distance_Traveled > 1
group by p.AssetID, p.tagid
order by 1

I’d like to combine their output into one table of results with the following columns:

AssetID,
TagID, 
Repeats (from the first query),
Non-Repeats (from the first query),
% of Repeats (from the first query),
Calc1 (difference of repeats in first query and count result from second query, grouped by asset id),
Calc1% (Calc1 result/repeats from the first query, grouped by assetid),
Calc2 (count result from the second query, grouped by assetid)
Cacl2%(Calc2 result/repeats from the first query, grouped by assetid)

I have started by creating a temp table to hold the results, and I can successfully insert the first query’s results, but I can’t figure out how to update the table with the second query and compute the percentage columns too. How can I get this to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:02:02+00:00Added an answer on June 15, 2026 at 4:02 pm

    You can do this using one query, see below

    ;WITH CTE AS (
    SELECT 
    --q1
    p.assetid, 
    p.TagId, 
    SUM(CASE WHEN p.isrepeat = 1  then 1 else 0 END) as 'Repeats',
    SUM(CASE WHEN p.isrepeat = 0  then 1 else 0 END) as 'Non-Repeats',
    CAST(SUM(CASE WHEN p.isrepeat =1 then 1 
             else 0 END) as DECIMAL)/COUNT(*)as 'Percent of Repeats',
    
    --q2
    COUNT(t.AssetID) CNT2,
    
    from POSITION p
    LEFT OUTER JOIN TEMP t ON t.beginning_X = p.X 
                           and t.beginning_Y = p.y and t.AssetID = p.AssetID
                           AND p.isrepeat = 1 and t.Total_Distance_Traveled > 1
    
    group by p.tagid, p.assetid
    )
    
    SELECT
    Assetid, TagId, Repeats, Non-Repeats, [Percent of Repeats],
    (repeats - cnt2) calc1, ((repeats - cnt2)/repeats [Per calc1],
    (cnt2) calc2, (cnt2/repeats) [Per calc2]
    FROM CTE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following 2 queries: select sum(cast(2666 as float)) * cast(.3
I have this two queries: SELECT `a`.*, `b`.`id` AS host_id, SUM(CASE WHEN c.event_id IS
I have the following queries: 1 SELECT TOP 1 CASE WHEN latency=-1 THEN 'Down'
Currently, I have 2 queries: SELECT dateadd(month, x.MonthOffset,0) as [Month], SUM(x.itemcount) as [Total] FROM
I have two select queries that I need to combine.... SELECT [DatapointID] ,[AssetID] ,[DatapointID]
I have a small sql question for you. I have two queries: SELECT tbl_CostPrevisions.Month,
I have two select queries to use in my project for getting data and
We have a lot of queries select * from tbl_message that get stuck on
I have the following two queries: select count(*) from segmentation_cycle_recipients scr , segmentation_instance si
I have two queries use [DatabaseAA] select * ,DATEDIFF(MINUTE,SomeDate,'3/28/2012 12:52:25 PM +00:00') This passes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.