English 中文(简体)
单 页: 1
原标题:Single SQL Server Result Set from Query

请就如何将两项成果合并到2005年使用SQ服务器的一项成果提出咨询意见。

我有以下情形:<代码>Account >可达两条<编码>重新定居指令,并仿照:

The slimeddown schema:

Account
---------------------
Id
AccountName
PrimarySettlementId (nullable)
AlternateSettlementId (nullable)


SettlementInstruction
----------------------
Id
Name

我所希望的产出是<条码>环形结果:,其中附有一份内容大致如此的选定声明,使我能够在我的春季浏览中构筑一些 j物体:

select
  Account.Id as accountId, 
  Account.AccountName as accountName, 

  s1.Id as primarySettlementId, 
  s1.Name as primarySettlementName, 

  s2.Id as alternateSettlementId, 
  s2.Name as alternateSettlementName

我尝试了各种事情,但找不到一种办法,可以把结果合并到初级和候补科索沃党没有被废除的地方。

最后,我搜索了论坛,但似乎与我需要的东西完全吻合。

最佳回答

页: 1

select
  Account.Id as accountId, 
  Account.AccountName as accountName, 

  s1.Id as primarySettlementId, 
  s1.Name as primarySettlementName, 

  s2.Id as alternateSettlementId, 
  s2.Name as alternateSettlementName
from Account
     left join SettlementInstruction s1 ON s1.Id = Account.PrimarySettlementId
     left join SettlementInstruction s2 ON s2.Id = Account.SecondarySettlementId
where /* ... */
问题回答

你们可以尝试:

SELECT
  a.Id as accountId, 
  a.AccountName as accountName, 
   Primary  as Settlementtype
  s1.Name as primarySettlementName, 
FROM 
   Account a
INNER JOIN
   SettlementInstruction s1 ON a.PrimarySettlementId = s1.Id
UNION
SELECT
  a.Id as accountId, 
  a.AccountName as accountName, 
   Alternate  as Settlementtype
  s2.Name as  AlternateName , 
FROM 
   Account a
INNER JOIN
   SettlementInstruction s2 ON a.AlternateSettlementId = s2.Id

这对你来说是否可行?

这使你得到一份清单,列出所有账户/结算指示,说明账户有基本结算的情况,然后列出第二个账户/结算清单,而候补账户不作废。 这两者都将被忽略。 某些<代码>Account的事例可能重复(如果两个工地都有效,且不无效)。

或者,如果不是:你重新寻找什么?





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签