Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to add a new code to an already existing code that join 3 tables, but I'm having trouble running it. The goal is to add a table to the code that is producing a table. Here is the original code:
I have this code WHICH IS JOIN OF 3 TABLES,
SQL
SELECT 
 [Provider] AS Publisher
,[Price_Rate]
,[Source_ID]
,Media.Date
,Media.Impressions
,Media.Clicks
,'0' AS [Opt-buyers]
,'0' AS [Completed buyers]
,Media.accounting
FROM [dbo].[budget]  AS BUDGET

LEFT JOIN 
(

  SELECT CASE WHEN [Ad_Set_Name] LIKE 'tw_%' 
  THEN'twitter' 
  WHEN [Ad_Set_Name] LIKE 'IN_%'
  THEN 'insta'
   ELSE '?'
      END AS Publisher
  ,CAST([Day] AS Date) AS Date
  ,SUM(CAST([Impressions] AS INT)) AS Impressions
  ,SUM(CAST([Link_Clicks] AS INT)) AS Clicks
  ,SUM(CAST([Amount_Spent__USD_] AS money)) AS Spend
  FROM [dbo].[twitter]
  Group by Day,[Ad_Set_Name]

   UNION ALL

    SELECT CASE WHEN [Site__PCM_] = 'acuits.com'
    THEN 'acqt'
     WHEN [Site__PCM_]= 'PulsePoint'
     THEN 'plpt'
    WHEN [Site__PCM_] = 'SRAX'
    THEN 'srax'
    ELSE [Site__PCM_]
    END AS Publisher
    ,CAST(Date AS Date) AS Date
    ,SUM(CAST(impressions AS INT)) AS Impressions
     ,SUM(CAST(clicks AS INT)) AS Clicks
    ,SUM(CAST(media_cost AS money)) AS Spend
   FROM [dbo] [pcm]
   Group by [Site__PCM_]
  ,Date

 ) AS new_sources_budget
 ON BUDGET.Source_ID = Media.Publisher
  WHERE source_id IS NOT NULL


and I'm trying to join another table **called Email** to what's this code is currently providing, but
I'm having tough time passing thus far. the goal is to add this code

SQL
SELECT 
  SUM(CAST(_Send2 AS INT)) AS [Email Sent]
 ,SUM(CAST(_Open2 AS INT)) AS [Email Open]
 ,SUM(CAST(Click2 AS INT)) AS [Email Click]
FROM [dbo].[behaviour]
Group by _Send2,_Open2,Click2


This code provide the following table
Publisher Source_ID Date Impression clicks optin Spend
accuea 1010 4/20/20 updown 10 yes 300

any help will be appreciated.

What I have tried:

I have tried to add this code to the current code but it doesn't work
 [Provider] AS Publisher
,[Price_Rate]
,[Source_ID]
,Media.Date
,Media.Impressions
,Media.Clicks
,'0' AS [Opt-buyers]
,'0' AS [Completed buyers]
,Media.accounting
FROM [dbo].[budget]  AS BUDGET

LEFT JOIN 
(
      SUM(CAST(_Send2 AS INT)) AS _Send2
      ,SUM(CAST([_Open2] AS INT)) AS _Open2
      ,SUM(CAST([Click2] AS money)) AS Click2
 FROM [dbo].[Email]
  Group by _Send2,_Open2,Click2

UNION ALL

  SELECT CASE WHEN [Ad_Set_Name] LIKE 'tw_%' 
  THEN'twitter' 
  WHEN [Ad_Set_Name] LIKE 'IN_%'
  THEN 'insta'
   ELSE '?'
      END AS Publisher
  ,CAST([Day] AS Date) AS Date
  ,SUM(CAST([Impressions] AS INT)) AS Impressions
  ,SUM(CAST([Link_Clicks] AS INT)) AS Clicks
  ,SUM(CAST([Amount_Spent__USD_] AS money)) AS Spend
  FROM [dbo].[twitter]
  Group by Day,[Ad_Set_Name]

   UNION ALL

    SELECT CASE WHEN [Site__PCM_] = 'acuits.com'
    THEN 'acqt'
     WHEN [Site__PCM_]= 'PulsePoint'
     THEN 'plpt'
    WHEN [Site__PCM_] = 'SRAX'
    THEN 'srax'
    ELSE [Site__PCM_]
    END AS Publisher
    ,CAST(Date AS Date) AS Date
    ,SUM(CAST(impressions AS INT)) AS Impressions
     ,SUM(CAST(clicks AS INT)) AS Clicks
    ,SUM(CAST(media_cost AS money)) AS Spend
   FROM [dbo] [pcm]
   Group by [Site__PCM_]
  ,Date

 ) AS new_sources_budget
 ON BUDGET.Source_ID = Media.Publisher
  WHERE source_id IS NOT NULL


and I'm trying to join another table **called Email** Highlighted in Bold, but
I'm having tough time passing thus far. the goal is to add this code
When adding that the code highlighted in bold, I get the following error
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

SQL
SELECT 
  SUM(CAST(_Send2 AS INT)) AS [Email Sent]
 ,SUM(CAST(_Open2 AS INT)) AS [Email Open]
 ,SUM(CAST(Click2 AS INT)) AS [Email Click]
FROM [dbo].[behaviour]
Group by _Send2,_Open2,Click2
Posted
Updated 21-Apr-20 2:51am
v3
Comments
CHill60 20-Apr-20 17:01pm    
"doesn't work" is not helpful. What happens? Or doesn't happen. If an error is produced then tell us what the error is

1 solution

You're trying to UNION a set with three columns - [_Send2], [_Open2], and [Click2] - with two sets containing five columns - [Publisher], [Date], [Impressions], [Clicks], and [Spend].

That's not how UNION works. As the error message tells you, all sets in the UNION must have the same number of columns.
UNION (Transact-SQL) - SQL Server | Microsoft Docs[^]

It's also not clear why you're summing the three columns, when you're also grouping by those columns. I don't think that's going to produce the result you're expecting.
GROUP BY (Transact-SQL) - SQL Server | Microsoft Docs[^]

You need to examine the structure of your behaviour table to see how it relates to the other tables before you can work out how to combine it with your existing query.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900