Are unions able to be queried?

  • select distinct firstname,lastname,address,Null as address2,city,state,zipcode,country,phone,email,'ms' as datagroup

    from marketscout02.dbo.users

    where username <> 'CICintegration' and (cic_username is null or cic_username = '') and (email is not null and email <> '')

    UNION

    select distinct firstname,lastname,address,Null as address2,city,state,zipcode,country,phone,email,'cic' as datagroup

    from marketscout02.dbo.users

    where (username = 'CICintegration' or (cic_username is not null or cic_username <> '')) and (email is not null and email <> '')

    With the union statement above...is there anyway to do a select count(*) on the unioned dataset?

    I have a union statement similar to this but want to run some counts on columns in the combined/frankendata set.

    Thanks in advance!

  • SELECT COUNT(*) FROM (

    select distinct firstname,lastname,address,Null as address2,city,state,zipcode,country,phone,email,'ms' as datagroup

    from marketscout02.dbo.users

    where username <> 'CICintegration' and (cic_username is null or cic_username = '') and (email is not null and email <> '')

    UNION

    select distinct firstname,lastname,address,Null as address2,city,state,zipcode,country,phone,email,'cic' as datagroup

    from marketscout02.dbo.users

    where (username = 'CICintegration' or (cic_username is not null or cic_username <> '')) and (email is not null and email <> '')

    ) dtName

  • Awesome...sometimes things in SQL are too simple to comprehend off the bat..lol

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply