473,382 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

SQL Query with Self Join

shahjapan
HI,
I have a problem to make SQL query which describe below
Problem
Table : TbData



AutoID | ID1 | ID2 | Status
1 10 1 1
2 10 2 0
3 11 1 1
411 2 1

I want output like this :

ID1 | Count(Status(1)) | Count(Status(0))
10 | 1| 1
11| 2 | 0


How to make query for this ?

I have to solve this problem very urgent so please if u hav any idea regarding this then guide me
thanks in advance
Feb 18 '08 #1
1 2355
Dave44
153 100+
I dont think this is a self join. looks like you are simply grouping by ID1 and getting a count of the status.

Expand|Select|Wrap|Line Numbers
  1. [155]dave@> CREATE TABLE t (autoid NUMBER, id1 NUMBER, id2 NUMBER, status NUMBER);
  2.  
  3. Table created.
  4.  
  5. Elapsed: 00:00:00.09
  6. [155]dave@> 
  7. [155]dave@> INSERT INTO t
  8.   2       VALUES (1, 10, 1, 1);
  9.  
  10. 1 row created.
  11.  
  12. Elapsed: 00:00:00.00
  13. [155]dave@> INSERT INTO t
  14.   2       VALUES (1, 10, 2, 0);
  15.  
  16. 1 row created.
  17.  
  18. Elapsed: 00:00:00.00
  19. [155]dave@> INSERT INTO t
  20.   2       VALUES (1, 11, 1, 1);
  21.  
  22. 1 row created.
  23.  
  24. Elapsed: 00:00:00.00
  25. [155]dave@> INSERT INTO t
  26.   2       VALUES (1, 11, 2, 1);
  27.  
  28. 1 row created.
  29.  
  30. Elapsed: 00:00:00.01
  31. [155]dave@> 
  32. [155]dave@> SELECT   id1,
  33.   2           SUM( DECODE(status,
  34.   3                       1, 1,
  35.   4                       0) ) status_1,
  36.   5           SUM( DECODE(status,
  37.   6                       0, 1,
  38.   7                       0) ) status_0
  39.   8  FROM     t
  40.   9  GROUP BY id1
  41.  10  ORDER BY id1;
  42.  
  43.        ID1   STATUS_1   STATUS_0
  44. ---------- ---------- ----------
  45.         10          1          1
  46.         11          2          0
  47.  
  48. Elapsed: 00:00:00.01
  49.  
Feb 19 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
4
by: Shahzad | last post by:
dear respected gurus, I would like to knew how to apply append,insert query for a self table where no primary keys issues. i do have problem say there are 5 rows of single record, this is data...
4
by: deko | last post by:
I'm trying to update the address record of an existing record in my mdb with values from another existing record in the same table. In pseudo code it might look like this: UPDATE tblAddress SET...
14
by: Jim Andersen | last post by:
I have a problem with this standard employee-supervisor scenario For pictures: http://www.databasedev.co.uk/self-join_query.html I want to show all employees "belonging" to a specific...
30
by: Paul H | last post by:
I seem to end up with loads of append and update queries just because it's quick and easy to build queries or make a new ones based on an existing query. But I end up with loads of queries with...
3
by: Richard Hollenbeck | last post by:
I hope this isn't too confusing. The following query runs pretty fast by itself, but when I want to use it in a report (pasted below the query), it takes at least fifteen seconds to run! Then I...
3
by: Genalube | last post by:
All right I am new at access but why would my calculated fields in my query self destruct all of a sudden? I tested these expressions, saved the query and closed it. I reopened them and tested it. ...
1
by: write2ashokkumar | last post by:
hi... i have the table like this, Table Name : sample Total Records : 500000 (Consider like this) Sample Records: id ------------ name
1
by: write2ashokkumar | last post by:
hi... i have the table like this, Table Name : sample Total Records : 500000 (Consider like this) Sample Records: ----------------
1
by: write2ashokkumar | last post by:
hi... i have the table like this, Table Name : sample Total Records : 500000 (Consider like this) Sample Records: ----------------
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.