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 3630870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:19:29+00:00 2026-05-19T00:19:29+00:00

I have three tables as below. tbl1 eid qnt pb 12 3 1 13

  • 0

I have three tables as below.

tbl1

eid qnt   pb
12     3   1
13     1   1
23     3   1

tbl2

tid   eid  fx   so
1      12    0    1
2      13    0    1
3      23    1    1
4      23    0    1

tbl3

tlid   tid  eid  fx
1      1    12    0
2      1    12    0
3      2    13    0
4      3    23    1
5      4    23    0

Need an update query that counts the number of ‘tlid’ in ‘tbl3’

who’s ‘pb’ in ‘tb1’ is ‘1’

who’s ‘eid’ in ‘tbl2’ does not have ‘1’ as one if it’s ‘fx’

and update ‘so’ in ‘tbl2’ to ‘0’ if the total counted ‘tlid’ in ‘tbl3’ is ‘LESS’ than the ‘qnt’ in ‘tbl1’

The final result will be that ‘so’ for ‘tid 1’ in ‘tbl2’ will be ‘0’

The others will not get updated because:

The sum of ‘tlid’ in ‘tbl3’ for ‘tid 2’ or ‘eid 13’ in ‘tble3’ = ‘qnt’ in ‘tbl1’

and

‘tlid’ for ‘eid 23’ in ‘tbl3’ will not be counted becuse ‘eid 23’ has ‘1’ in one of the ‘fx’ in ‘tbl2’

Appreciate your help

drop table if exists tbl1;
create table tbl1 (eid integer, qnt integer, pb integer);
insert into tbl1 values (12,3,1),(13,1,1),(23,3,1);

drop table if exists tbl2;
create table tbl2 (tid integer, eid integer, fx integer, so integer);
insert into tbl2 values (1,12,0,1),(2,13,0,1),(3,23,1,1),(4,23,0,1);

drop table if exists tbl3;
create table tbl3 (tlid integer, tid integer, eid integer, fx integer);
insert into tbl3 values (1,1,12,0),(2,1,12,0),(3,2,13,0),(4,3,23,1),(5,4,23,0) ;
  • 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-05-19T00:19:30+00:00Added an answer on May 19, 2026 at 12:19 am

    Try this:

    UPDATE tbl2, 
           (SELECT * 
            FROM   (SELECT tbl1.eid, 
                           tbl1.qnt, 
                           tbl3.tid, 
                           COUNT(DISTINCT tlid) cnt 
                    FROM   tbl1, 
                           tbl3, 
                           tbl2 
                    WHERE  tbl1.eid = tbl3.eid 
                           AND tbl2.eid = tbl3.eid 
                           AND pb = 1 
                           AND tbl2.eid NOT IN (SELECT tbl2.eid 
                                                FROM   tbl2 
                                                WHERE  fx = 1) 
                    GROUP  BY tbl1.eid, 
                              tbl3.tid, 
                              tbl1.qnt) a 
            WHERE  cnt < qnt) b 
    SET    so = 0 
    WHERE  tbl2.tid = b.tid  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables in an SQL 2005 database, that I need to query
I have three tables specifying important columns below Users(Id, username) Groups(Id, groupname, creator) (creator
I have three tables as shown below Emp ---- empID int empName deptID empDetails
I have Three Tables in SqlServer (equivalent to image in link below in SQL
Okay I have three tables that all communicate with each other. ForumTopic t ForumMessage
I have three simple tables as shown below: +----+-------------+ +----+-----------+ +----+---------------+ | artists |
I have three tables: adapters, connectors, components I need to be able to search
I have three tables (definitions below). My question is there can be multiple bar
I have three tables kinda like the example below: TABLE CARS carId carName -----
I have three tables with a one to many relationship as shown below. What

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.