How to get records from SQL

Imran Ali Khan

i have to SQL server 2008 tables as below

Table A

ID          int         Not Null (primary ID)
No          int         NULL
Value       int         NULL
Flag        nchar(10)   NULL

Table B

ID          int         Not Null (primary ID)
No          int         NULL
Value       int         NULL
Flag        nchar(10)   NULL

and i Have below data in table A

ID     No      Value   Flag
1      1        12      1         
2      1        12      1         
3      1        25      1         
4      2        120     1         
5      3        36      2         
6      2        120     2         
7      6        1       1         
8      2        10      1         
9      6        10      2         
10     1        25      2         
11     2        120     1        

and there no records in table B when i write below statement

SELECT     dbo.A.No, SUM(dbo.A.Value) AS [IN], SUM(ISNULL(dbo.B.Value, 0)) AS OUT
FROM         dbo.A LEFT OUTER JOIN
                      dbo.B ON dbo.A.NO = dbo.B.NO
WHERE     (dbo.A.Flag = N'1')
GROUP BY dbo.A.No

I am getting below result

No      IN      OUT
1       49       0
2       250      0
6       1        0

When I add WHERE (dbo.A.Flag = N'1') AND (dbo.B.Flag = N'1')

nothing is coming..

my question is How to get records from table B as 0 when B not contains records or not find B.Id

UPDATE : When i have data in table B Then records are coming.

Jithin Shaji

Imran,
I think the below sql will help you.

SELECT      A.No, 
            SUM(A.Value) AS [IN], 
            SUM(ISNULL(B.Value, 0)) AS [OUT]
FROM        dbo.A A 
LEFT JOIN   dbo.B B ON A.No = B.No AND B.Flag = N'1'
WHERE       A.Flag = N'1'
GROUP BY    A.No

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get records from SQL

From Dev

How to get records from different tables and ignoring duplicated dates in SQL?

From Dev

How To Get Records From Date To Date SQL Server

From Dev

how to get not matching records from same tables in sql?

From Dev

How to get all the records from midnight until now in Postgress sql

From Dev

How to get the first record from a group of records MS sql

From Dev

How To Get Count of the records in sql

From Dev

SQL get the latest records from a set of data

From Dev

SQL to get Team Records from Games Database

From Dev

SQL to get Team Records from Games Database

From Dev

SQL get the latest records from a set of data

From Dev

Get the latest date of records from a table, SQL

From Dev

get the count of records from two tables in sql

From Dev

SQL get records from specific day

From Dev

oracle sql how to get the unmatch positive records

From Dev

How to get the average of every three records in a column starting from first record in MS Access/SQL?

From Dev

How to get multiple records from Redis Hashes

From Dev

How to get all records from set in aerospike?

From Dev

How to get Last 10 records from OrmLite?

From Dev

How to get random records from a viewmodel

From Dev

How to get last records with conditions from SQLite?

From Dev

How to delete all duplicate records from sql

From Dev

How to delete multiple records from SQL table

From Dev

How to conditionally select records from SQL Server

From Dev

How to SELECT part of records from a SQL table

From Dev

SQL query for Informix to get all the records from previous month

From Dev

Oracle SQL get the first and last records from an ordered dataset

From Dev

SQL Query to get different records from two tables

From Dev

Get records count from sql server table in the most optimized way

Related Related

  1. 1

    How to get records from SQL

  2. 2

    How to get records from different tables and ignoring duplicated dates in SQL?

  3. 3

    How To Get Records From Date To Date SQL Server

  4. 4

    how to get not matching records from same tables in sql?

  5. 5

    How to get all the records from midnight until now in Postgress sql

  6. 6

    How to get the first record from a group of records MS sql

  7. 7

    How To Get Count of the records in sql

  8. 8

    SQL get the latest records from a set of data

  9. 9

    SQL to get Team Records from Games Database

  10. 10

    SQL to get Team Records from Games Database

  11. 11

    SQL get the latest records from a set of data

  12. 12

    Get the latest date of records from a table, SQL

  13. 13

    get the count of records from two tables in sql

  14. 14

    SQL get records from specific day

  15. 15

    oracle sql how to get the unmatch positive records

  16. 16

    How to get the average of every three records in a column starting from first record in MS Access/SQL?

  17. 17

    How to get multiple records from Redis Hashes

  18. 18

    How to get all records from set in aerospike?

  19. 19

    How to get Last 10 records from OrmLite?

  20. 20

    How to get random records from a viewmodel

  21. 21

    How to get last records with conditions from SQLite?

  22. 22

    How to delete all duplicate records from sql

  23. 23

    How to delete multiple records from SQL table

  24. 24

    How to conditionally select records from SQL Server

  25. 25

    How to SELECT part of records from a SQL table

  26. 26

    SQL query for Informix to get all the records from previous month

  27. 27

    Oracle SQL get the first and last records from an ordered dataset

  28. 28

    SQL Query to get different records from two tables

  29. 29

    Get records count from sql server table in the most optimized way

HotTag

Archive