E
E
Eugene2015-10-21 18:45:26
SQL
Eugene, 2015-10-21 18:45:26

How to combine horizontally the results of two queries?

A meaningless, but simple pseudo-language example
SELECT contract AS ct, contractDate, subscriber, (SELECT cost FROM upload WHERE contract = ct)
FROM upload
That is, I want to somehow pass the value of the contract inside to use it in the condition there. Is it possible to do this?
PS The client can have international and long-distance calls, it is necessary that the request displays the cost of both in one line. If you use normal grouping, the output will be two lines. And my question assumes something like UNION, but only horizontally.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2015-10-21
@NeiroNx

LEFT JOIN - to include fields from other tables
GROUP BY to join rows on selected fields

SELECT contract , contractDate, subscriber, SUM( cost ) AS 'cost'
FROM upload
GROUP BY contract , contractDate, subscriber

Adds cost for identical contract , contractDate, subscriber fields

Y
Yuri, 2015-10-27
@naidzionik

Select ct,
  contractDate,
        subscriber,
  'Междугордние',
  [Междугордние],
  'Международные',
  [Международные]
from (SELECT [contract] AS ct, contractDate, subscriber,[тип_звонка],cost  from upload ) t
  pivot (sum([cost]) for [тип_звонка] in ([Междугордние],[Международные])) pvt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question