4

Merge the result of two queries in SQL Server

 2 years ago
source link: https://www.codesd.com/item/merge-the-result-of-two-queries-in-sql-server.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Merge the result of two queries in SQL Server

advertisements

I have two different queries:

SELECT
    PB_BANK_CODE,
    PB_BANK_NAME
FROM GLAS_PDC_BANKS
WHERE PB_COMP_CODE='1'
AND PB_BANK_CODE='025'
AND PB_BANK_CODE IN (
    SELECT DISTINCT PC_BANK_FROM
    FROM GLAS_PDC_CHEQUES
    WHERE PC_BANK_FROM ='025'
    AND ISNULL(PC_DISCD,'X') != 'C'
    AND PC_DUE_DATETIME BETWEEN '05/05/2008' AND '05/06/2008'
)

SELECT ISNULL(SUM(PC_AMOUNT),0)
FROM GLAS_PDC_CHEQUES
WHERE PC_BANK_FROM ='025'
AND ISNULL(PC_DISCD,'X') != 'C'
AND PC_DUE_DATETIME BETWEEN '05/05/2008' AND '05/06/2008'

I'm trying to merge these two so that I can get PB_BANK_CODE, PB_BANK_NAME, and ISNULL(SUM(PC_AMOUNT),0) in a single dataset.

How can I merge these two queries in SQL Server 2005?


this should do it...

I think?

SELECT
   PB_BANK_CODE,
   PB_BANK_NAME,
   ISNULL(SUM(PC_AMOUNT),0)
FROM GLAS_PDC_BANKS inner join GLAS_PDC_CHEQUES
    on GLAS_PDC_BANKS.PB_BANK_CODE = GLAS_PDC_CHEQUES.PC_BANK_FROM
WHERE  PB_COMP_CODE='1'
   AND PB_BANK_CODE='025'
   AND ISNULL(PC_DISCD,'X') != 'C'
   AND PC_DUE_DATETIME BETWEEN '05/05/2008' AND '05/06/2008'

Related Articles

How can we merge the results of two queries have the group by and two conditions

I have these two tables: (games) and (rounds) table: I would like to get the game id's, there round count, and the count of how many rounds that player1 wins. I created two sql queries like this: but I want to get all results in one table. How can I

How to merge the results of two independent xpath queries?

I have two xpath queries query1 and query2 result of execution query1 is row11 row12 result of execution query2 is row21 row22 row23 row24 I want to write single query with following result: row11 row12 row21 row22 row23 row24 Can you help me?I would

Merge the results of two active record requests

I want to merge the results from the following queries: Dispenser.includes(:reviews).where.not(reviews: { id: nil }) and Dispenser.includes(:dispenser_reviews).where.not(dispenser_reviews: { id: nil }) I have tried simply placing a || in the middle o

We must split the data for two columns in sql server

We need to split data for two columns in sql server. CREATE TABLE #t (Id int,Number varchar(32),Name varchar(32)); Insert Into #t(Id, Number,Name) Values(123, '1,2,3','a~b~c') , (456, '1,2,3,4','a~b~c~d'), (789, '1,2','a~b') SELECT *FROM #t Input: Id

SQL Server CE: How can I combine the results of two queries in one line?

I have two queries that return a single value, from different tables (and not joined via a relationship in any way), and I'm trying to combine both queries' outputs onto a single row, however I'm getting a syntax error. This is what I'm trying: SELEC

How do I get the result of two queries in the same result in SQL Server?

I have two queries that give me the result. What I am trying to achieve is to get the result of the first query on the top (as the first value), and then the results of the second query under that, so the exact result will come first (Inlagg.Categori

Merge the result of two internal joins in the same table into a single temporary table in SQL Server

I am trying to get all key-value pairs from a document which you can search for with a list of key-value pairs yourself. For this I create a temporary table and a SELECT statement for each key-value pair provided like this: DECLARE @DocMetaTemp TABLE

How to merge the results of two divergent xpaths for Microsoft SQL Server

I'm new at this so please bear with me. I have two select statements containing all the information that I want from an XML document in Microsoft SQL Server 2016. I want combine the contents of these two select statements and then turn it into a tabl

merge the result of two joins

I have 3 tables: Book, Invoice, Download where the last two have foreign key to Book. I need to know number of successful invoices and downloads for each book. To get the result, I have 2 queries. The first one displays number invoices for each book:

Coldfusion combines the results of two queries

I have two coldfusion queries, like below which return different columns along with a date column. <cfquery name="qry1" datasource="test"> select title, name, id, test_date from table1 , table2 </cfquery> <cfquery name=&

How to combine the results of two queries into a single dataset

I have two queries : Queries Simplified excluding Joins Query 1 : select ProductName,NumberofProducts (in inventory) from Table1.....; Query 2 : select ProductName, NumberofProductssold from Table2......; I would like to know how I can get an output

Passes the result of two queries to see in the codeigniteur

i have two query $q1 and $q2. from the query1 i get multiple records and each record having id and i want use this id for second query in where condition. i am doing this in controller. i have tried following code. In the foreach i am trying to store

Merge the results of 2 queries where the joined column may miss data in one of the tables

I have the feeling this has a really simple solution and I'm just suffering a brain freeze, but I hope you can help. I have 2 tables that have identical layouts but data from separate systems and I want to combine them in a query for reporting purpos

Multiple result sets, different queries, same SQL SERVER database

Using JDBC driver, how can I use multiple result sets from different queries without continually opening and closing the connections, as it stand im extracting w.e I need and passing it to another method. Each time opening a new conn,statement and re

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK