0

My select queries, i have remove t0.brandID = null its not that important just need to get the result of query in one table.

SELECT 
    t0.brandID, t0.brandName, t0.cdt, t0.udt, t0.brandstatus, t0.AddedBy  
FROM 
    brands t0 
WHERE
    t0.brandName = 'budwieser'

SELECT
    AVG(CAST (brandID AS bigint)) AS brandID_AVERAGE,
    MIN(CAST (brandID AS bigint)) as branid_min,
    MAX(CAST (brandID AS bigint)) as brandid_max,
    COUNT(CAST (brandID AS bigint)) as brandid_count  
FROM
    (SELECT 
         t0.brandID, t0.brandName, t0.cdt, t0.udt, t0.brandstatus, t0.AddedBy  
     FROM 
         brands t0 
     WHERE
         t0.brandID = null OR t0.brandName = 'budwieser') temptable 

The result of the above query is in two different tables as below picture:

brandid brandname       cdt                       udt       brandstatus added by  

    8   budwieser   2013-11-14 16:26:43.913 2014-02-12 19:26:43.913 1   8
    18  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1
    23  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1
    37  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1
    63  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1
    82  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1
    92  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1

and

brandid_average  brandid_min   brandid_max   brandid_count
 46              8             92             7

I want to show result as below:

  brandid brandname   cdt               udt  brandstatus addedby branid_average brandid_min   brandid_max   branid_count
     8   budwieser 2013-11-14      2014-02-12   1    8          46             8               92             7
    18  budwieser  2013-11-15      2013-11-15   1    1         null           null            null          null
    ........................................................       null           null            null          null
    --------------------------------------------------------       null           null            null          null
3
  • You cannot check for NULL by using the regular equality operator - you need to use to.BrandID IS NULL (use IS NULL or IS NOT NULL - not the = null) .
    – marc_s
    Mar 21, 2016 at 16:06
  • can you explain why your expected output has the calculated values for the 8 Budweiser record and not the 18 Budweiser record? Reading your question I was expecting the same values to be in all the Budweiser record. How do you want to relate/pair the results of the 2 queries?
    – JMariña
    Mar 21, 2016 at 16:25
  • thanks, i have to calculate the average, minimum, max, count of selected records. in my table i have budwiser entries at branid's (8,18,23,...,92) the average is (8+18+23+..)/7=46.07 near to 46. the minimum brandid of the selected records is 8, the maximum brandid of selected records is 92 and number of records is 7. i have to show these values along with selected values of branid,brandname, ..,brandid_average,branid_min,brandid_max,brandid_count. this is test scenario, the actual use will be for calculating voltage, across different inverters. this will be exported as report to excel.
    – Tan
    Mar 21, 2016 at 16:36

2 Answers 2

0

In this scenario where two table results need to be combined it can be simple done by selecting from both the tables. for example table1, table2.

Table1 has columns id,name,age
table2 has columns someid,group,gender

simple query for the result to show the below format is:

select t1.*,t2.* from table1 t1,table2 t2

id name age someid group gender
1  one  1    10     5     M

similarly for the above question as there is no physical table exists we have to declare a table and save the values to that temporary table and then simply select from both the tables that will return you the desired result as in the question.

     drop table #mytemptable --- droping the temporary table if exists 
//select query
      select AVG(CAST (brandID AS bigint)) AS brandID_AVERAGE,
       min(CAST (brandID AS bigint)) as branid_min,
       MAX(CAST (brandID AS bigint)) as brandid_max,
       COUNT(CAST (brandID AS bigint)) as brandid_count
 into #mytemptable ---//Here inserting the selected values from below query to temporary table
  from
      ( SELECT t0.brandID, t0.brandName, t0.cdt, t0.udt, t0.brandstatus, t0.AddedBy  

       FROM brands t0 
       Where  t0. brandID=null OR  t0. brandName='budwieser'

       )   temptable 
       //simple select from both tables
       SELECT t0.brandID, t0.brandName, t0.cdt, t0.udt, t0.brandstatus, t0.AddedBy ,t2.* 
       from brands t0,#mytemptable t2
       where t0.brandName='budwieser'

The expected result will be:

brandID brandName   cdt udt brandstatus AddedBy brandID_AVERAGE branid_min  brandid_max brandid_count
8   budwieser   2013-11-14 16:26:43.913 2014-02-12 19:26:43.913 1   8   46  8   92  7
18  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
23  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
37  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
63  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
82  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
92  budwieser   2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1   1   46  8   92  7
0
SELECT t0.brandID,
       t0.brandName,
       t0.cdt,
       t0.udt,
       t0.brandstatus,
       t0.AddedBy,
       AVG(CAST(brandID AS BIGINT)) OVER(PARTITION BY t0.brandName) AS brandID_AVERAGE,
       MIN(CAST(brandID AS BIGINT)) OVER(PARTITION BY t0.brandName) AS branid_min,
       MAX(CAST(brandID AS BIGINT)) OVER(PARTITION BY t0.brandName) AS brandid_max,
       COUNT(CAST(brandID AS BIGINT)) OVER(PARTITION BY t0.brandName) AS brandid_count
FROM   brands t0
WHERE  t0.brandName = 'budwieser'
1
  • if i select between two dates instead of "Where t0.brandname='budwieser'" to "where t0.cdt >= '2013-11-14' and t0.cdt <='2013-11-15'" it will give brandid_average always its branid, brandid_min as brandid, brandid_max as brandid, brandid_count will always be 1 which is incorrect. if you check the answer provided by me above you change the where clause to anything you will get perfect result of avg,min,max and count...this is just for your info hope you agree, please correct me if i am wrong
    – Tan
    Mar 21, 2016 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.